github.com/resonatecoop/id@v1.1.0-43/services/services.go (about)

     1  package services
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/gorilla/sessions"
     7  	"github.com/resonatecoop/id/config"
     8  	"github.com/resonatecoop/id/health"
     9  	"github.com/resonatecoop/id/oauth"
    10  	"github.com/resonatecoop/id/session"
    11  	"github.com/resonatecoop/id/web"
    12  	"github.com/resonatecoop/id/webhook"
    13  	"github.com/uptrace/bun"
    14  )
    15  
    16  func init() {
    17  
    18  }
    19  
    20  var (
    21  	// HealthService ...
    22  	HealthService health.ServiceInterface
    23  
    24  	// OauthService ...
    25  	OauthService oauth.ServiceInterface
    26  
    27  	// WebService ...
    28  	WebService web.ServiceInterface
    29  
    30  	// WebHookService ...
    31  	WebHookService webhook.ServiceInterface
    32  
    33  	// SessionService ...
    34  	SessionService session.ServiceInterface
    35  )
    36  
    37  // UseHealthService sets the health service
    38  func UseHealthService(h health.ServiceInterface) {
    39  	HealthService = h
    40  }
    41  
    42  // UseOauthService sets the oAuth service
    43  func UseOauthService(o oauth.ServiceInterface) {
    44  	OauthService = o
    45  }
    46  
    47  // UseWebHookService sets the web service
    48  func UseWebHookService(w webhook.ServiceInterface) {
    49  	WebHookService = w
    50  }
    51  
    52  // UseWebService sets the web service
    53  func UseWebService(w web.ServiceInterface) {
    54  	WebService = w
    55  }
    56  
    57  // UseSessionService sets the session service
    58  func UseSessionService(s session.ServiceInterface) {
    59  	SessionService = s
    60  }
    61  
    62  // Init starts up all services
    63  func Init(cnf *config.Config, db *bun.DB) error {
    64  	if nil == reflect.TypeOf(HealthService) {
    65  		HealthService = health.NewService(db)
    66  	}
    67  
    68  	if nil == reflect.TypeOf(OauthService) {
    69  		OauthService = oauth.NewService(cnf, db)
    70  	}
    71  
    72  	if nil == reflect.TypeOf(SessionService) {
    73  		// note: default session store is CookieStore
    74  		store := sessions.NewCookieStore([]byte(cnf.Session.Secret))
    75  
    76  		store.Options = &sessions.Options{
    77  			Path:     cnf.Session.Path,
    78  			MaxAge:   cnf.Session.MaxAge,
    79  			Secure:   cnf.Session.Secure,
    80  			HttpOnly: cnf.Session.HTTPOnly,
    81  		}
    82  
    83  		SessionService = session.NewService(cnf, store)
    84  	}
    85  
    86  	if nil == reflect.TypeOf(WebService) {
    87  		WebService = web.NewService(cnf, OauthService, SessionService)
    88  	}
    89  
    90  	if nil == reflect.TypeOf(WebHookService) {
    91  		WebHookService = webhook.NewService(cnf, db, OauthService)
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  // Close closes any open services
    98  func Close() {
    99  	HealthService.Close()
   100  	OauthService.Close()
   101  	WebHookService.Close()
   102  	WebService.Close()
   103  	SessionService.Close()
   104  }