diff --git a/README.md b/README.md index e0b8784..f5abdea 100755 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ module categories below the root level: - Auth - authentication/authorization modules that do not integrate with Nibbler's user model (source of truth is not Nibbler). - Database - connect to databases and expose mechanisms to create, query, etc. - Mail - outbound email/sms/etc -- Session - session storage and retreival +- Session - session storage and retrieval - Storage - block/blob storage - User - the Nibbler user model, and various integrations that can operate with it. These will tend to be auth integrations. @@ -65,11 +65,11 @@ extensions := []nibbler.Extension{ &C{}, &BC{}, } -exts, err := nibbler.AutoWireExtensions(&extensions, &logger) +exts, err := nibbler.AutoWireExtensions(extensions, logger) // check error -err = app.Init(config, &logger, &extensions) +err = app.Init(config, logger, extensions) // check error ``` @@ -102,7 +102,7 @@ The following properties are available by default, but custom properties can be password to it in the Init() method. For example: ``` -(*app.GetConfiguration().Raw).Get("some", "property") +app.GetConfiguration().Raw.Get("some", "property") ``` The core properties (all optional) are: diff --git a/application.go b/application.go index 3613480..f9ae88f 100755 --- a/application.go +++ b/application.go @@ -28,7 +28,7 @@ type Logger interface { type Configuration struct { HeaderConfiguration HeaderConfiguration Port int - Raw *config.Config + Raw config.Config StaticDirectory string } @@ -40,27 +40,25 @@ type HeaderConfiguration struct { type Application struct { config *Configuration - extensions *[]Extension - logger *Logger + extensions []Extension + logger Logger router *mux.Router } -func (ac *Application) Init(config *Configuration, logger *Logger, extensions *[]Extension) error { +func (ac *Application) Init(config *Configuration, logger Logger, extensions []Extension) error { ac.config = config ac.logger = logger ac.extensions = extensions // dereference parameters for ease-of-use - extensionValue := *extensions configValue := *config - loggerValue := *logger // prepare a general-use error variable var err error = nil // initialize all extensions // if any error occurred, return the error and stop processing - for _, x := range extensionValue { + for _, x := range extensions { if err = x.Init(ac); err != nil { return err } @@ -71,7 +69,7 @@ func (ac *Application) Init(config *Configuration, logger *Logger, extensions *[ // init extension routes // if any error occurred, return the error and stop processing - for _, x := range extensionValue { + for _, x := range extensions { if err = x.AddRoutes(ac); err != nil { return err } @@ -80,7 +78,7 @@ func (ac *Application) Init(config *Configuration, logger *Logger, extensions *[ // set up the static directory routing ac.router.PathPrefix("/").Handler(http.FileServer(http.Dir(configValue.StaticDirectory))) - loggerValue.Info("Starting server") + logger.Info("Starting server") http.Handle("/", ac.router) @@ -89,11 +87,8 @@ func (ac *Application) Init(config *Configuration, logger *Logger, extensions *[ func (ac *Application) Run() error { - // dereference parameters for ease-of-use - loggerValue := *ac.logger - // get the configured app mode, accounting for the default value - mode := (*ac.config.Raw).Get("nibbler", "mode").String("web") + mode := ac.config.Raw.Get("nibbler", "mode").String("web") // allocate and prep signal channel signals := make(chan os.Signal, 1) @@ -114,12 +109,12 @@ func (ac *Application) Run() error { <-signals // shut down the server - loggerValue.Info("shutting down the server") + ac.logger.Info("shutting down the server") shutdownError = h.Shutdown(context.Background()) // log a shutdown error (factor into return value later) if shutdownError != nil { - loggerValue.Error(shutdownError.Error()) + ac.logger.Error(shutdownError.Error()) } } else if mode == "worker" { @@ -130,16 +125,13 @@ func (ac *Application) Run() error { return errors.New("unknown nibbler mode detected: " + mode) } - loggerValue.Info("shutting down the application") - - // dereference parameters for ease-of-use - extensionValue := *ac.extensions + ac.logger.Info("shutting down the application") // destroy extensions in reverse order var err error var destroyError error - for i := range extensionValue { - x := extensionValue[len(extensionValue)-i-1] + for i := range ac.extensions { + x := ac.extensions[len(ac.extensions)-i-1] destroyError = x.Destroy(ac) if destroyError != nil { @@ -158,22 +150,19 @@ func (ac *Application) Run() error { func startServer(h *http.Server, ac *Application) error { - // dereference parameters for ease-of-use - loggerValue := *ac.logger - // log that we're listening and state the port - loggerValue.Info("Listening on " + strconv.Itoa((*ac.config).Port)) + ac.logger.Info("Listening on " + strconv.Itoa((*ac.config).Port)) // listen (this blocks) - log an error if it happened if err := h.ListenAndServe(); err != nil { - loggerValue.Error("Failed to initialize server: " + err.Error()) + ac.logger.Error("Failed to initialize server: " + err.Error()) return err } return nil } -func (ac *Application) GetLogger() *Logger { +func (ac *Application) GetLogger() Logger { return ac.logger } diff --git a/auth/auth0/extension.go b/auth/auth0/extension.go index 8af8f0a..0502d46 100644 --- a/auth/auth0/extension.go +++ b/auth/auth0/extension.go @@ -84,12 +84,12 @@ func (s *Extension) Destroy(app *nibbler.Application) error { } func (s *Extension) CallbackHandler(w http.ResponseWriter, r *http.Request) { - domain := (*s.config.Raw).Get("auth0", "domain").String("") + domain := s.config.Raw.Get("auth0", "domain").String("") conf := &oauth2.Config{ - ClientID: (*s.config.Raw).Get("auth0", "client", "id").String(""), - ClientSecret: (*s.config.Raw).Get("auth0", "client", "secret").String(""), - RedirectURL: (*s.config.Raw).Get("auth0", "callback", "url").String(""), + ClientID: s.config.Raw.Get("auth0", "client", "id").String(""), + ClientSecret: s.config.Raw.Get("auth0", "client", "secret").String(""), + RedirectURL: s.config.Raw.Get("auth0", "callback", "url").String(""), Scopes: []string{"openid", "profile"}, // TODO: make configurable Endpoint: oauth2.Endpoint{ AuthURL: "https://" + domain + "/authorize", @@ -172,14 +172,13 @@ func (s *Extension) CallbackHandler(w http.ResponseWriter, r *http.Request) { } func (s *Extension) LoginHandler(w http.ResponseWriter, r *http.Request) { - rawConfig := *s.config.Raw - domain := rawConfig.Get("auth0", "domain").String("") - aud := rawConfig.Get("auth0", "audience").String("") + domain := s.config.Raw.Get("auth0", "domain").String("") + aud := s.config.Raw.Get("auth0", "audience").String("") conf := &oauth2.Config{ - ClientID: rawConfig.Get("auth0", "client", "id").String(""), - ClientSecret: rawConfig.Get("auth0", "client", "secret").String(""), - RedirectURL: rawConfig.Get("auth0", "callback", "url").String(""), + ClientID: s.config.Raw.Get("auth0", "client", "id").String(""), + ClientSecret: s.config.Raw.Get("auth0", "client", "secret").String(""), + RedirectURL: s.config.Raw.Get("auth0", "callback", "url").String(""), Scopes: []string{"openid", "profile"}, // TODO: make configurable Endpoint: oauth2.Endpoint{ AuthURL: "https://" + domain + "/authorize", diff --git a/auth/auth0/sample/sample.application.go b/auth/auth0/sample/sample.application.go index f339ae3..076a48f 100644 --- a/auth/auth0/sample/sample.application.go +++ b/auth/auth0/sample/sample.application.go @@ -28,7 +28,6 @@ func (s *SampleExtension) ProtectedRoute(w http.ResponseWriter, r *http.Request) func main() { // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} config, err := nibbler.LoadConfiguration(nil) // allocate session extension @@ -45,18 +44,15 @@ func main() { LoggedInRedirectUrl: "/", } - // prepare extensions for initialization - extensions := []nibbler.Extension{ + // initialize the application, provide config, logger, extensions + appContext := nibbler.Application{} + if err := appContext.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ &sessionExtension, &auth0Extension, &SampleExtension{ Auth0Extension: &auth0Extension, }, - } - - // initialize the application - appContext := nibbler.Application{} - if err := appContext.Init(config, &logger, &extensions); err != nil { + }); err != nil { log.Fatal(err.Error()) } diff --git a/configuration.go b/configuration.go index 1a8d737..8d0a0c9 100755 --- a/configuration.go +++ b/configuration.go @@ -65,7 +65,7 @@ func LoadConfiguration(sources *[]source.Source) (*Configuration, error) { } return &Configuration{ - Raw: &conf, + Raw: conf, Port: primaryPort, StaticDirectory: conf.Get("nibbler", "directory", "static").String("./public/"), HeaderConfiguration: HeaderConfiguration{ diff --git a/database/elasticsearch/extension.go b/database/elasticsearch/extension.go index b9babe8..f31376f 100644 --- a/database/elasticsearch/extension.go +++ b/database/elasticsearch/extension.go @@ -22,11 +22,10 @@ func (s *Extension) Init(app *nibbler.Application) error { // if the Url attribute isn't set, find the config in environment variables if len(s.Url) == 0 { - configPtr := app.GetConfiguration().Raw - s.Url = (*configPtr).Get("elastic", "url").String("") + s.Url = app.GetConfiguration().Raw.Get("elastic", "url").String("") if len(s.Url) == 0 { - s.Url = (*configPtr).Get("database", "url").String("http://localhost:9200") + s.Url = app.GetConfiguration().Raw.Get("database", "url").String("http://localhost:9200") } } diff --git a/database/elasticsearch/sample/sample.application.go b/database/elasticsearch/sample/sample.application.go index 590c3b3..68fd515 100644 --- a/database/elasticsearch/sample/sample.application.go +++ b/database/elasticsearch/sample/sample.application.go @@ -9,21 +9,17 @@ import ( func main() { // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} config, err := nibbler.LoadConfiguration(nil) if err != nil { log.Fatal(err.Error()) } - // prepare extension(s) for initialization - extensions := []nibbler.Extension{ - &elasticsearch.Extension{}, - } - - // initialize the application + // initialize the application, provide config, logger, extensions appContext := nibbler.Application{} - if err = appContext.Init(config, &logger, &extensions); err != nil { + if err = appContext.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &elasticsearch.Extension{}, + }); err != nil { log.Fatal(err.Error()) } diff --git a/database/mongo/extension.go b/database/mongo/extension.go index 7bd8d9c..7f4ab40 100644 --- a/database/mongo/extension.go +++ b/database/mongo/extension.go @@ -17,10 +17,10 @@ func (s *Extension) Init(app *nibbler.Application) error { // if the Url attribute isn't set, find the config in environment variables if len(s.Url) == 0 { - s.Url = (*app.GetConfiguration().Raw).Get("mongo", "url").String("") + s.Url = app.GetConfiguration().Raw.Get("mongo", "url").String("") if len(s.Url) == 0 { - s.Url = (*app.GetConfiguration().Raw).Get("database", "url").String("") + s.Url = app.GetConfiguration().Raw.Get("database", "url").String("") } } diff --git a/database/mongo/sample/sample.application.go b/database/mongo/sample/sample.application.go index 3777f33..ee61367 100644 --- a/database/mongo/sample/sample.application.go +++ b/database/mongo/sample/sample.application.go @@ -15,22 +15,20 @@ type Animal struct { func main() { // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} config, err := nibbler.LoadConfiguration(nil) if err != nil { log.Fatal(err.Error()) } - // prepare extensions for initialization + // allocate the mongo extension mongoExtension := mongo.Extension{} - extensions := []nibbler.Extension{ - &mongoExtension, - } - // initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &mongoExtension, + }); err != nil { log.Fatal(err.Error()) } diff --git a/database/redis/extension.go b/database/redis/extension.go index 439341c..20e3631 100644 --- a/database/redis/extension.go +++ b/database/redis/extension.go @@ -17,14 +17,14 @@ type Extension struct { func (s *Extension) Init(app *nibbler.Application) error { if len(s.Url) == 0 { - s.Url = (*app.GetConfiguration().Raw).Get("redis", "url").String("") + s.Url = app.GetConfiguration().Raw.Get("redis", "url").String("") if len(s.Url) == 0 { - s.Url = (*app.GetConfiguration().Raw).Get("rediscloud", "url").String("") + s.Url = app.GetConfiguration().Raw.Get("rediscloud", "url").String("") } if len(s.Url) == 0 { - s.Url = (*app.GetConfiguration().Raw).Get("database", "url").String("") + s.Url = app.GetConfiguration().Raw.Get("database", "url").String("") } if len(s.Url) > 0 { @@ -46,10 +46,10 @@ func (s *Extension) Init(app *nibbler.Application) error { } if len(s.Password) == 0 { - s.Password = (*app.GetConfiguration().Raw).Get("redis", "password").String("") + s.Password = app.GetConfiguration().Raw.Get("redis", "password").String("") if len(s.Password) == 0 { - s.Password = (*app.GetConfiguration().Raw).Get("database", "password").String("") + s.Password = app.GetConfiguration().Raw.Get("database", "password").String("") } } diff --git a/database/redis/sample/sample.application.go b/database/redis/sample/sample.application.go index 8b95b86..ace91f5 100644 --- a/database/redis/sample/sample.application.go +++ b/database/redis/sample/sample.application.go @@ -10,7 +10,6 @@ import ( func main() { // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} config, err := nibbler.LoadConfiguration(nil) if err != nil { @@ -19,13 +18,12 @@ func main() { // prepare extensions for initialization redisExtension := redis.Extension{} - extensions := []nibbler.Extension{ - &redisExtension, - } - // initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &redisExtension, + }); err != nil { log.Fatal(err.Error()) } diff --git a/database/sql/extension.go b/database/sql/extension.go index e04106c..dab4c5d 100644 --- a/database/sql/extension.go +++ b/database/sql/extension.go @@ -63,7 +63,7 @@ func (s *Extension) Init(app *nibbler.Application) error { configuration.Port, configuration.Username, configuration.Path, - configuration.Password, + *configuration.Password, sslMode, )) } else if configuration.Scheme == "sqlite3" { @@ -147,18 +147,15 @@ func (s *Extension) getBestConfiguration(app *nibbler.Application) (*Configurati return nil, errors.New("sql extension could not get configuration") } - configPtr := app.GetConfiguration().Raw - // if the root config is available, attempt to get the SQL URL from it - if configPtr != nil { - config := *configPtr - dbUrl := config.Get("sql", "url").String("") + if app.GetConfiguration().Raw != nil { + dbUrl := app.GetConfiguration().Raw.Get("sql", "url").String("") // parse the url if able, fall back to database.url if len(dbUrl) > 0 { urlParsed, _ = url.Parse(dbUrl) } else { - dbUrl = config.Get("database", "url").String("") + dbUrl = app.GetConfiguration().Raw.Get("database", "url").String("") } // if we still don't have a URL, parse the url if able, fall back to db.url @@ -166,7 +163,7 @@ func (s *Extension) getBestConfiguration(app *nibbler.Application) (*Configurati if len(dbUrl) > 0 { urlParsed, _ = url.Parse(dbUrl) } else { - dbUrl = config.Get("db", "url").String("") + dbUrl = app.GetConfiguration().Raw.Get("db", "url").String("") } if len(dbUrl) > 0 { @@ -195,11 +192,10 @@ func (s *Extension) getBestConfiguration(app *nibbler.Application) (*Configurati // apply fallback user/password if urlParsed.User == nil { - if configPtr != nil { - config := *configPtr + if app.GetConfiguration().Raw != nil { urlParsed.User = url.UserPassword( - config.Get("db", "user").String(""), - config.Get("db", "password").String(""), + app.GetConfiguration().Raw.Get("db", "user").String(""), + app.GetConfiguration().Raw.Get("db", "password").String(""), ) } else { urlParsed.User = url.UserPassword("", "") @@ -218,20 +214,17 @@ func (s *Extension) getBestConfiguration(app *nibbler.Application) (*Configurati newHostParts[0] = hostParts[0] hostParts = newHostParts - if configPtr != nil { - config := *configPtr - + if app.GetConfiguration().Raw != nil { if len(hostParts[0]) == 0 { - hostParts[0] = config.Get("db", "host").String("") + hostParts[0] = app.GetConfiguration().Raw.Get("db", "host").String("") } - hostParts[1] = config.Get("db", "port").String("") + hostParts[1] = app.GetConfiguration().Raw.Get("db", "port").String("") } } // apply fallback path/name parameter if needed - if len(urlParsed.Path) == 0 && configPtr != nil { - config := *configPtr - urlParsed.Path = config.Get("db", "dbname").String("") + if len(urlParsed.Path) == 0 && app.GetConfiguration().Raw != nil { + urlParsed.Path = app.GetConfiguration().Raw.Get("db", "dbname").String("") } if schemeAcceptsLeadingSlashInPath(urlParsed.Scheme) { diff --git a/database/sql/sample/sample.application.go b/database/sql/sample/sample.application.go index 205a953..5f2da76 100644 --- a/database/sql/sample/sample.application.go +++ b/database/sql/sample/sample.application.go @@ -9,7 +9,6 @@ import ( func main() { // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} config, err := nibbler.LoadConfiguration(nil) if err != nil { @@ -20,13 +19,12 @@ func main() { sqlExtension := sql.Extension{ //models = ... } - extensions := []nibbler.Extension{ - &sqlExtension, - } - // initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &sqlExtension, + }); err != nil { log.Fatal(err.Error()) } diff --git a/mail/outbound/mandrill/extension.go b/mail/outbound/mandrill/extension.go index 6b925b1..b0a5cd0 100644 --- a/mail/outbound/mandrill/extension.go +++ b/mail/outbound/mandrill/extension.go @@ -21,7 +21,7 @@ type Extension struct { func (s *Extension) Init(app *nibbler.Application) error { if len(s.Key) == 0 { - s.Key = (*app.GetConfiguration().Raw).Get("mandrill", "api", "key").String("") + s.Key = app.GetConfiguration().Raw.Get("mandrill", "api", "key").String("") } if len(s.Key) == 0 { diff --git a/mail/outbound/mandrill/sample/sample.application.go b/mail/outbound/mandrill/sample/sample.application.go index 068fb9c..67ddb31 100644 --- a/mail/outbound/mandrill/sample/sample.application.go +++ b/mail/outbound/mandrill/sample/sample.application.go @@ -9,9 +9,6 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration (from env vars, files, etc) config, err := nibbler.LoadConfiguration(nil) @@ -22,14 +19,11 @@ func main() { // allocate the sparkpost extension mandrillExtension := nibMandrill.Extension{} - // prepare extensions for initialization - extensions := []nibbler.Extension{ - &mandrillExtension, - } - - // create and initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &mandrillExtension, + }); err != nil { log.Fatal(err.Error()) } diff --git a/mail/outbound/sendgrid/extension.go b/mail/outbound/sendgrid/extension.go index bc6c0a4..1172643 100644 --- a/mail/outbound/sendgrid/extension.go +++ b/mail/outbound/sendgrid/extension.go @@ -17,13 +17,11 @@ type Extension struct { } func (s *Extension) Init(app *nibbler.Application) error { - config := app.GetConfiguration() - if config == nil || config.Raw == nil { + if app.GetConfiguration() == nil || app.GetConfiguration().Raw == nil { return errors.New("sendgrid extension could not get app config") } - configValue := *config.Raw - s.apiKey = configValue.Get("sendgrid", "api", "key").String("") + s.apiKey = app.GetConfiguration().Raw.Get("sendgrid", "api", "key").String("") if len(s.apiKey) == 0 { return errors.New("sendgrid extension could not get API key") diff --git a/mail/outbound/sendgrid/sample/sample.application.go b/mail/outbound/sendgrid/sample/sample.application.go index 8fdb351..8c58904 100644 --- a/mail/outbound/sendgrid/sample/sample.application.go +++ b/mail/outbound/sendgrid/sample/sample.application.go @@ -9,9 +9,6 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration (from env vars, files, etc) config, err := nibbler.LoadConfiguration(nil) @@ -22,14 +19,11 @@ func main() { // allocate the sendgrid extension sendgridExtension := NibSendGrid.Extension{} - // prepare extensions for initialization - extensions := []nibbler.Extension{ - &sendgridExtension, - } - - // create and initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &sendgridExtension, + }); err != nil { log.Fatal(err.Error()) } diff --git a/mail/outbound/sparkpost/extension.go b/mail/outbound/sparkpost/extension.go index ab06843..04cc7d8 100644 --- a/mail/outbound/sparkpost/extension.go +++ b/mail/outbound/sparkpost/extension.go @@ -16,13 +16,11 @@ type Extension struct { } func (s *Extension) Init(app *nibbler.Application) error { - config := app.GetConfiguration() - if config == nil || config.Raw == nil { + if app.GetConfiguration() == nil || app.GetConfiguration().Raw == nil { return errors.New("sparkpost extension could not get app config") } - configValue := *config.Raw - s.apiKey = configValue.Get("sparkpost", "api", "key").String("") + s.apiKey = app.GetConfiguration().Raw.Get("sparkpost", "api", "key").String("") if len(s.apiKey) == 0 { return errors.New("sparkpost extension could not get API key") diff --git a/mail/outbound/sparkpost/sample/sample.application.go b/mail/outbound/sparkpost/sample/sample.application.go index caf9617..1d62b09 100644 --- a/mail/outbound/sparkpost/sample/sample.application.go +++ b/mail/outbound/sparkpost/sample/sample.application.go @@ -9,9 +9,6 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration (from env vars, files, etc) config, err := nibbler.LoadConfiguration(nil) @@ -22,14 +19,11 @@ func main() { // allocate the sparkpost extension sparkpostExtension := sparkpost.Extension{} - // prepare extensions for initialization - extensions := []nibbler.Extension{ - &sparkpostExtension, - } - - // create and initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &sparkpostExtension, + }); err != nil { log.Fatal(err.Error()) } diff --git a/response.go b/response.go index 863bafd..43db48a 100644 --- a/response.go +++ b/response.go @@ -18,4 +18,4 @@ func Write500Json(w http.ResponseWriter, message string) { w.WriteHeader(http.StatusInternalServerError) w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"result": "` + message + `"}`)) -} \ No newline at end of file +} diff --git a/sample/sample.application.go b/sample/sample.application.go index 423169c..cb9a7a6 100755 --- a/sample/sample.application.go +++ b/sample/sample.application.go @@ -21,9 +21,6 @@ func (s *SampleExtension) AddRoutes(context *nibbler.Application) error { func main() { - // allocate logger - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration config, err := nibbler.LoadConfiguration(nil) @@ -32,14 +29,11 @@ func main() { log.Fatal(err.Error()) } - // prepare extensions for initialization - extensions := []nibbler.Extension{ - &SampleExtension{}, - } - - // initialize the application + // initialize the application, provide config, logger, extensions appContext := nibbler.Application{} - if err := appContext.Init(config, &logger, &extensions); err != nil { + if err := appContext.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &SampleExtension{}, + }); err != nil { log.Fatal(err.Error()) } diff --git a/session/sample/sample.application.go b/session/sample/sample.application.go index d1fae08..2fd9a5d 100644 --- a/session/sample/sample.application.go +++ b/session/sample/sample.application.go @@ -13,9 +13,6 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration config, err := nibbler.LoadConfiguration(nil) @@ -49,16 +46,13 @@ func main() { StoreConnector: sessionConnector, } - // prepare extensions for initialization - extensions := []nibbler.Extension{ + // initialize the application, provide config, logger, extensions + appContext := nibbler.Application{} + if err = appContext.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ sqlController.SqlExtension, &userExtension, &sessionExtension, - } - - // initialize the application - appContext := nibbler.Application{} - if err = appContext.Init(config, &logger, &extensions); err != nil { + }); err != nil { log.Fatal(err.Error()) } diff --git a/socket/socket.io/sample/sample.application.go b/socket/socket.io/sample/sample.application.go index b353baa..6fe5bbf 100644 --- a/socket/socket.io/sample/sample.application.go +++ b/socket/socket.io/sample/sample.application.go @@ -9,9 +9,6 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration config, err := nibbler.LoadConfiguration(nil) if err != nil { @@ -22,19 +19,16 @@ func main() { Port: 8000, } - // prepare extensions for initialization - extensions := []nibbler.Extension{ - &socketIoExtension, - } - - // initialize the application + // initialize the application, provide config, logger, extensions appContext := nibbler.Application{} - if err = appContext.Init(config, &logger, &extensions); err != nil { + if err = appContext.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &socketIoExtension, + }); err != nil { log.Fatal(err.Error()) } socketIoExtension.RegisterConnectHandler("test", "test", func(s socketio.Conn) error { - logger.Debug("socket connect") + log.Println("socket connect") return nil }) diff --git a/storage/azure/blob/extension.go b/storage/azure/blob/extension.go index 0005f87..ffb39a7 100644 --- a/storage/azure/blob/extension.go +++ b/storage/azure/blob/extension.go @@ -18,8 +18,8 @@ type Extension struct { } func (s *Extension) Init(app *nibbler.Application) error { - s.accountName = (*app.GetConfiguration().Raw).Get("azure", "blob", "account", "name").String("") - s.accountKey = (*app.GetConfiguration().Raw).Get("azure", "blob", "account", "key").String("") + s.accountName = app.GetConfiguration().Raw.Get("azure", "blob", "account", "name").String("") + s.accountKey = app.GetConfiguration().Raw.Get("azure", "blob", "account", "key").String("") if s.accountName == "" || s.accountKey == "" { return errors.New("azure blob extension requires both account name and account key") diff --git a/storage/azure/blob/sample/sample.application.go b/storage/azure/blob/sample/sample.application.go index 81ad4da..a55aafc 100644 --- a/storage/azure/blob/sample/sample.application.go +++ b/storage/azure/blob/sample/sample.application.go @@ -13,9 +13,6 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration (from env vars, files, etc) config, err := nibbler.LoadConfiguration(nil) @@ -26,14 +23,11 @@ func main() { // allocate the blob extension blobExt := blob.Extension{} - // prepare extensions for initialization - extensions := []nibbler.Extension{ - &blobExt, - } - - // create and initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &blobExt, + }); err != nil { log.Fatal(err.Error()) } diff --git a/storage/google/drive/extension.go b/storage/google/drive/extension.go index 2649073..ebb9c9b 100644 --- a/storage/google/drive/extension.go +++ b/storage/google/drive/extension.go @@ -28,11 +28,11 @@ type Extension struct { func (s *Extension) Init(app *nibbler.Application) error { if s.CredentialsPath == "" { - s.CredentialsPath = (*app.GetConfiguration().Raw).Get("google.drive.credentials.path").String("drive-credentials.json") + s.CredentialsPath = app.GetConfiguration().Raw.Get("google.drive.credentials.path").String("drive-credentials.json") } if s.TokenFilePath == "" { - s.TokenFilePath = (*app.GetConfiguration().Raw).Get("google.drive.tokenfile.path").String("drive-token.json") + s.TokenFilePath = app.GetConfiguration().Raw.Get("google.drive.tokenfile.path").String("drive-token.json") } b, err := ioutil.ReadFile(s.CredentialsPath) diff --git a/storage/google/drive/sample/sample.application.go b/storage/google/drive/sample/sample.application.go index 06be93a..d7fbfb9 100644 --- a/storage/google/drive/sample/sample.application.go +++ b/storage/google/drive/sample/sample.application.go @@ -9,9 +9,6 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration (from env vars, files, etc) config, err := nibbler.LoadConfiguration(nil) @@ -22,14 +19,11 @@ func main() { // allocate the S3 extension driveExtension := drive.Extension{} - // prepare extensions for initialization - extensions := []nibbler.Extension{ - &driveExtension, - } - - // create and initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &driveExtension, + }); err != nil { log.Fatal(err.Error()) } diff --git a/storage/s3/extension.go b/storage/s3/extension.go index 7eb02c6..5762150 100644 --- a/storage/s3/extension.go +++ b/storage/s3/extension.go @@ -7,7 +7,6 @@ import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" "github.com/markdicksonjr/nibbler" - "log" ) type Extension struct { @@ -25,13 +24,11 @@ func (s S3CredentialProvider) Retrieve() (credentials.Value, error) { return credentials.Value{}, errors.New("app configuration not found by s3 extension") } - configValue := *s.Config.Raw - return credentials.Value{ - configValue.Get("s3", "accesskey").String(""), - configValue.Get("s3", "secret").String(""), - configValue.Get("s3", "session", "key").String(""), - "local", + AccessKeyID: s.Config.Raw.Get("s3", "accesskey").String(""), + SecretAccessKey: s.Config.Raw.Get("s3", "secret").String(""), + SessionToken:s.Config.Raw.Get("s3", "session", "key").String(""), + ProviderName: "local", }, nil } @@ -41,23 +38,20 @@ func (s S3CredentialProvider) IsExpired() bool { } func (s *Extension) Init(app *nibbler.Application) error { - config := app.GetConfiguration() - if config == nil || config.Raw == nil { + if app.GetConfiguration() == nil || app.GetConfiguration().Raw == nil { return errors.New("app configuration not found by s3 extension") } - configValue := *config.Raw - creds := credentials.NewCredentials(&S3CredentialProvider{ - Config: config, - }) sess, err := session.NewSession(&aws.Config{ - Credentials: creds, - Endpoint: aws.String(configValue.Get("s3", "endpoint").String("")), - Region: aws.String(configValue.Get("s3", "region").String("")), + Credentials: credentials.NewCredentials(&S3CredentialProvider{ + Config: app.GetConfiguration(), + }), + Endpoint: aws.String(app.GetConfiguration().Raw.Get("s3", "endpoint").String("")), + Region: aws.String(app.GetConfiguration().Raw.Get("s3", "region").String("")), }) if err != nil { - log.Fatal(err) + return err } // create S3 service client diff --git a/storage/s3/sample/sample.application.go b/storage/s3/sample/sample.application.go index 0d7f90a..98d5c1a 100644 --- a/storage/s3/sample/sample.application.go +++ b/storage/s3/sample/sample.application.go @@ -10,9 +10,6 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration (from env vars, files, etc) config, err := nibbler.LoadConfiguration(nil) @@ -23,14 +20,11 @@ func main() { // allocate the S3 extension s3Extension := s3.Extension{} - // prepare extensions for initialization - extensions := []nibbler.Extension{ - &s3Extension, - } - - // create and initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &s3Extension, + }); err != nil { log.Fatal(err.Error()) } diff --git a/storage/sftp/client/extension.go b/storage/sftp/client/extension.go index 74d8c45..5ab61d9 100644 --- a/storage/sftp/client/extension.go +++ b/storage/sftp/client/extension.go @@ -40,15 +40,15 @@ func (s *Extension) Connect() error { } if len(s.Username) == 0 { - s.Username = (*s.app.GetConfiguration().Raw).Get("sftp", "client", "username").String("") + s.Username = s.app.GetConfiguration().Raw.Get("sftp", "client", "username").String("") } if len(s.Password) == 0 { - s.Password = (*s.app.GetConfiguration().Raw).Get("sftp", "client", "password").String("") + s.Password = s.app.GetConfiguration().Raw.Get("sftp", "client", "password").String("") } if len(s.Host) == 0 { - s.Host = (*s.app.GetConfiguration().Raw).Get("sftp", "client", "host").String("") + s.Host = s.app.GetConfiguration().Raw.Get("sftp", "client", "host").String("") } callback := ssh.InsecureIgnoreHostKey() diff --git a/storage/sftp/client/sample/sample.application.go b/storage/sftp/client/sample/sample.application.go index eda40e5..c359444 100644 --- a/storage/sftp/client/sample/sample.application.go +++ b/storage/sftp/client/sample/sample.application.go @@ -9,9 +9,6 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration (from env vars, files, etc) config, err := nibbler.LoadConfiguration(nil) @@ -22,14 +19,11 @@ func main() { // allocate the SFTP client extension sftpExtension := client.Extension{} - // prepare extensions for initialization - extensions := []nibbler.Extension{ - &sftpExtension, - } - - // create and initialize the application + // initialize the application, provide config, logger, extensions app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ + &sftpExtension, + }); err != nil { log.Fatal(err.Error()) } diff --git a/user/auth/auth0/sample/sample.application.go b/user/auth/auth0/sample/sample.application.go index 4c6cffe..1396dd4 100644 --- a/user/auth/auth0/sample/sample.application.go +++ b/user/auth/auth0/sample/sample.application.go @@ -14,8 +14,7 @@ import ( func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} + // configuration config, err := nibbler.LoadConfiguration(nil) if err != nil { log.Fatal(err) @@ -54,8 +53,9 @@ func main() { UserExtension: &userExtension, } - // prepare extensions for initialization - extensions := []nibbler.Extension{ + // initialize the application, provide config, logger, extensions + appContext := nibbler.Application{} + if err = appContext.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ &sqlExtension, &userExtension, &sessionExtension, @@ -63,11 +63,7 @@ func main() { &SampleExtension{ Auth0Extension: &auth0Extension, }, - } - - // initialize the application - appContext := nibbler.Application{} - if err = appContext.Init(config, &logger, &extensions); err != nil { + }); err != nil { log.Fatal(err.Error()) } diff --git a/user/auth/local/login.go b/user/auth/local/login.go index 950beec..bc9c991 100644 --- a/user/auth/local/login.go +++ b/user/auth/local/login.go @@ -13,7 +13,7 @@ func (s *Extension) EnforceLoggedIn(routerFunc func(http.ResponseWriter, *http.R caller, err := s.SessionExtension.GetCaller(r) if err != nil { - (*s.app.GetLogger()).Error("while enforcing login, an error occurred: " + err.Error()) + s.app.GetLogger().Error("while enforcing login, an error occurred: " + err.Error()) nibbler.Write404Json(w) return } @@ -34,7 +34,7 @@ func (s *Extension) EnforceEmailValidated(routerFunc func(http.ResponseWriter, * caller, err := s.SessionExtension.GetCaller(r) if err != nil { - (*s.app.GetLogger()).Error("while enforcing email validated, an error occurred: " + err.Error()) + s.app.GetLogger().Error("while enforcing email validated, an error occurred: " + err.Error()) nibbler.Write404Json(w) return } diff --git a/user/auth/local/password.go b/user/auth/local/password.go index fda1a93..7feb7ee 100644 --- a/user/auth/local/password.go +++ b/user/auth/local/password.go @@ -63,7 +63,7 @@ func (s *Extension) ResetPasswordTokenHandler(w http.ResponseWriter, r *http.Req errUpdate := s.UserExtension.Update(userValue) if errUpdate != nil { - (*s.app.GetLogger()).Error("failed to update user record: " + errUpdate.Error()) + s.app.GetLogger().Error("failed to update user record: " + errUpdate.Error()) nibbler.Write500Json(w, "failed to update user record") return } diff --git a/user/auth/local/registration.go b/user/auth/local/registration.go index d764055..0126bb0 100644 --- a/user/auth/local/registration.go +++ b/user/auth/local/registration.go @@ -118,7 +118,7 @@ func (s *Extension) RegisterFormHandler(w http.ResponseWriter, r *http.Request) ) if err != nil { - (*s.app.GetLogger()).Error("while sending email verification, " + err.Error()) + s.app.GetLogger().Error("while sending email verification, " + err.Error()) } }() } @@ -150,7 +150,7 @@ func (s *Extension) EmailTokenVerifyHandler(w http.ResponseWriter, r *http.Reque // if an error happened during the lookup if err != nil { - (*s.app.GetLogger()).Error("while verifying email token: " + err.Error()) + s.app.GetLogger().Error("while verifying email token: " + err.Error()) nibbler.Write200Json(w, `{"result": false}`) return } diff --git a/user/auth/local/sample/sample.application.go b/user/auth/local/sample/sample.application.go index a1c6866..c734ac5 100644 --- a/user/auth/local/sample/sample.application.go +++ b/user/auth/local/sample/sample.application.go @@ -29,9 +29,6 @@ func (s *SampleExtension) ProtectedRoute(w http.ResponseWriter, r *http.Request) func main() { - // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} - // allocate configuration config, err := nibbler.LoadConfiguration(nil) @@ -83,8 +80,9 @@ func main() { EmailVerificationRedirect: "http://localhost:3000/verify", } - // prepare extensions for initialization - extensions := []nibbler.Extension{ + // initialize the application, provide config, logger, extensions + app := nibbler.Application{} + if err = app.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ &sqlExtension, &userExtension, &sessionExtension, @@ -93,11 +91,7 @@ func main() { &SampleExtension{ AuthExtension: &userLocalAuthExtension, }, - } - - // initialize the application - app := nibbler.Application{} - if err = app.Init(config, &logger, &extensions); err != nil { + }); err != nil { log.Fatal(err.Error()) } diff --git a/user/database/elastic/extension.go b/user/database/elastic/extension.go index eee3689..a2249ce 100644 --- a/user/database/elastic/extension.go +++ b/user/database/elastic/extension.go @@ -29,7 +29,7 @@ func (s *Extension) Init(app *nibbler.Application) error { } if !createIndex.Acknowledged { - (*app.GetLogger()).Info("in user extension, user index in elastic not acknowledged after creation") + app.GetLogger().Info("in user extension, user index in elastic not acknowledged after creation") } } diff --git a/user/sample/sample.application.go b/user/sample/sample.application.go index 657cebc..99a96bf 100644 --- a/user/sample/sample.application.go +++ b/user/sample/sample.application.go @@ -55,21 +55,17 @@ func allocateEsExtensions() UserAndDbExtensions { func main() { // allocate logger and configuration - var logger nibbler.Logger = nibbler.DefaultLogger{} config, err := nibbler.LoadConfiguration(nil) sqlExtensions := allocateSqlExtensions() - // prepare extension(s) for initialization - extensions := []nibbler.Extension{ + // initialize the application, provide config, logger, extensions + appContext := nibbler.Application{} + if err = appContext.Init(config, nibbler.DefaultLogger{}, []nibbler.Extension{ sqlExtensions.DbExtension, sqlExtensions.UserPersistenceExtension, sqlExtensions.UserExtension, - } - - // initialize the application context - appContext := nibbler.Application{} - if err = appContext.Init(config, &logger, &extensions); err != nil { + }); err != nil { log.Fatal(err.Error()) }