Skip to content

Commit

Permalink
Improved interface usage throughout repository
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Apr 5, 2019
1 parent b74fac7 commit e9729eb
Show file tree
Hide file tree
Showing 38 changed files with 157 additions and 275 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 16 additions & 27 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Logger interface {
type Configuration struct {
HeaderConfiguration HeaderConfiguration
Port int
Raw *config.Config
Raw config.Config
StaticDirectory string
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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" {

Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
19 changes: 9 additions & 10 deletions auth/auth0/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
12 changes: 4 additions & 8 deletions auth/auth0/sample/sample.application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}

Expand Down
2 changes: 1 addition & 1 deletion configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 2 additions & 3 deletions database/elasticsearch/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
12 changes: 4 additions & 8 deletions database/elasticsearch/sample/sample.application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
4 changes: 2 additions & 2 deletions database/mongo/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
}
}

Expand Down
12 changes: 5 additions & 7 deletions database/mongo/sample/sample.application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
10 changes: 5 additions & 5 deletions database/redis/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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("")
}
}

Expand Down
10 changes: 4 additions & 6 deletions database/redis/sample/sample.application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
}

Expand Down
Loading

0 comments on commit e9729eb

Please sign in to comment.