github.com/jxgolibs/go-oauth2-server@v1.0.1/services/services.go (about)

     1  package services
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/RichardKnop/go-oauth2-server/config"
     7  	"github.com/RichardKnop/go-oauth2-server/health"
     8  	"github.com/RichardKnop/go-oauth2-server/oauth"
     9  	"github.com/RichardKnop/go-oauth2-server/session"
    10  	"github.com/RichardKnop/go-oauth2-server/web"
    11  	"github.com/gorilla/sessions"
    12  	"github.com/jinzhu/gorm"
    13  )
    14  
    15  func init() {
    16  
    17  }
    18  
    19  var (
    20  	// HealthService ...
    21  	HealthService health.ServiceInterface
    22  
    23  	// OauthService ...
    24  	OauthService oauth.ServiceInterface
    25  
    26  	// WebService ...
    27  	WebService web.ServiceInterface
    28  
    29  	// SessionService ...
    30  	SessionService session.ServiceInterface
    31  )
    32  
    33  // UseHealthService sets the health service
    34  func UseHealthService(h health.ServiceInterface) {
    35  	HealthService = h
    36  }
    37  
    38  // UseOauthService sets the oAuth service
    39  func UseOauthService(o oauth.ServiceInterface) {
    40  	OauthService = o
    41  }
    42  
    43  // UseWebService sets the web service
    44  func UseWebService(w web.ServiceInterface) {
    45  	WebService = w
    46  }
    47  
    48  // UseSessionService sets the session service
    49  func UseSessionService(s session.ServiceInterface) {
    50  	SessionService = s
    51  }
    52  
    53  // Init starts up all services
    54  func Init(cnf *config.Config, db *gorm.DB) error {
    55  	if nil == reflect.TypeOf(HealthService) {
    56  		HealthService = health.NewService(db)
    57  	}
    58  
    59  	if nil == reflect.TypeOf(OauthService) {
    60  		OauthService = oauth.NewService(cnf, db)
    61  	}
    62  
    63  	if nil == reflect.TypeOf(SessionService) {
    64  		// note: default session store is CookieStore
    65  		SessionService = session.NewService(cnf, sessions.NewCookieStore([]byte(cnf.Session.Secret)))
    66  	}
    67  
    68  	if nil == reflect.TypeOf(WebService) {
    69  		WebService = web.NewService(cnf, OauthService, SessionService)
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  // Close closes any open services
    76  func Close() {
    77  	HealthService.Close()
    78  	OauthService.Close()
    79  	WebService.Close()
    80  	SessionService.Close()
    81  }