Skip to content

Commit

Permalink
Reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Mar 25, 2019
1 parent 97625b9 commit 7323134
Show file tree
Hide file tree
Showing 45 changed files with 213 additions and 276 deletions.
35 changes: 16 additions & 19 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

type Extension interface {
Init(app *Application) error
AddRoutes(app *Application) error
Destroy(app *Application) error
Init(app *Application) error
AddRoutes(app *Application) error
Destroy(app *Application) error
}

type Logger interface {
Expand All @@ -33,16 +33,16 @@ type Configuration struct {
}

type HeaderConfiguration struct {
AccessControlAllowHeaders string
AccessControlAllowMethods string
AccessControlAllowOrigin string
AccessControlAllowHeaders string
AccessControlAllowMethods string
AccessControlAllowOrigin string
}

type Application struct {
config *Configuration
extensions *[]Extension
logger *Logger
router *mux.Router
config *Configuration
extensions *[]Extension
logger *Logger
router *mux.Router
}

func (ac *Application) Init(config *Configuration, logger *Logger, extensions *[]Extension) error {
Expand Down Expand Up @@ -83,7 +83,6 @@ 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")

http.Handle("/", ac.router)
Expand Down Expand Up @@ -143,7 +142,7 @@ func (ac *Application) Run() error {
var err error
var destroyError error
for i := range extensionValue {
x := extensionValue[len(extensionValue) - i - 1]
x := extensionValue[len(extensionValue)-i-1]
destroyError = x.Destroy(ac)

if destroyError != nil {
Expand All @@ -168,15 +167,13 @@ func startServer(h *http.Server, ac *Application) error {
// log that we're listening and state the port
loggerValue.Info("Listening on " + strconv.Itoa((*ac.config).Port))

// listen (this blocks)
err := h.ListenAndServe()

// log an error if it happened
if err != nil {
// listen (this blocks) - log an error if it happened
if err := h.ListenAndServe(); err != nil {
loggerValue.Error("Failed to initialize server: " + err.Error())
return err
}

return err
return nil
}

func (ac *Application) GetLogger() *Logger {
Expand Down Expand Up @@ -204,4 +201,4 @@ func (s *NoOpExtension) Destroy(app *Application) error {

func (s *NoOpExtension) AddRoutes(app *Application) error {
return nil
}
}
8 changes: 4 additions & 4 deletions auth/auth0/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"golang.org/x/oauth2"
"net/http"
"github.com/markdicksonjr/nibbler"
"github.com/markdicksonjr/nibbler/session"
"golang.org/x/oauth2"
"net/http"
)

type Extension struct {
Expand All @@ -33,7 +33,7 @@ type Extension struct {

config *nibbler.Configuration

OnLoginComplete func(s *Extension, w http.ResponseWriter, r *http.Request) (allowRedirect bool, err error)
OnLoginComplete func(s *Extension, w http.ResponseWriter, r *http.Request) (allowRedirect bool, err error)
OnLogoutComplete func(s *Extension, w http.ResponseWriter, r *http.Request) error
}

Expand Down Expand Up @@ -252,4 +252,4 @@ func (s *Extension) EnforceLoggedIn(routerFunc func(http.ResponseWriter, *http.R

routerFunc(w, r)
}
}
}
8 changes: 4 additions & 4 deletions auth/auth0/sample/sample.application.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"github.com/markdicksonjr/nibbler/session/connectors"
"log"
"net/http"
"github.com/markdicksonjr/nibbler"
"github.com/markdicksonjr/nibbler/auth/auth0"
"github.com/markdicksonjr/nibbler/session"
"github.com/markdicksonjr/nibbler/session/connectors"
"log"
"net/http"
)

type SampleExtension struct {
Expand Down Expand Up @@ -41,7 +41,7 @@ func main() {

// allocate auth0 extension
auth0Extension := auth0.Extension{
SessionExtension: &sessionExtension,
SessionExtension: &sessionExtension,
LoggedInRedirectUrl: "/",
}

Expand Down
12 changes: 6 additions & 6 deletions autowire.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

type dependency struct {
parents []*dependency
extension *Extension
typeName string // mostly here for debugging, at the moment
parents []*dependency
extension *Extension
typeName string // mostly here for debugging, at the moment
}

var interfaceWiringEnabled = true
Expand All @@ -35,7 +35,7 @@ func AutoWireExtensions(extensions *[]Extension, logger *Logger) ([]Extension, e
typeName := typeVal.String()
treeMap[typeVal] = &dependency{
extension: &thisExt,
typeName: typeName,
typeName: typeName,
}
}

Expand All @@ -47,7 +47,7 @@ func AutoWireExtensions(extensions *[]Extension, logger *Logger) ([]Extension, e
thisExtensionDependency := treeMap[reflect.TypeOf(ext)]

// loop through the fields for this extension
for i:=0; i<fieldCount; i++ {
for i := 0; i < fieldCount; i++ {
fieldTypeAssignable := extensionType.Elem().Field(i)
fieldValue := extensionValue.Field(i)

Expand Down Expand Up @@ -206,4 +206,4 @@ func isDescendant(candidateChild *dependency, candidateAncestor *dependency) boo

// no ancestor was found
return false
}
}
4 changes: 2 additions & 2 deletions autowire_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package nibbler

import (
"testing"
"reflect"
"testing"
)

type A struct {
Expand Down Expand Up @@ -158,4 +158,4 @@ func SliceIndex(limit int, predicate func(i int) bool) int {
}
}
return -1
}
}
2 changes: 1 addition & 1 deletion configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func LoadConfiguration(sources *[]source.Source) (*Configuration, error) {
Port: primaryPort,
StaticDirectory: conf.Get("nibbler", "directory", "static").String("./public/"),
HeaderConfiguration: HeaderConfiguration{
AccessControlAllowOrigin: conf.Get("nibbler", "ac", "allow", "origin").String("*"),
AccessControlAllowOrigin: conf.Get("nibbler", "ac", "allow", "origin").String("*"),
AccessControlAllowMethods: conf.Get("nibbler", "ac", "allow", "methods").String("GET, POST, OPTIONS, PUT, PATCH, DELETE"),
AccessControlAllowHeaders: conf.Get("nibbler", "ac", "allow", "headers").String("Origin, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Response-Time, X-PINGOTHER, X-CSRF-Token, Authorization"),
},
Expand Down
6 changes: 3 additions & 3 deletions database/elasticsearch/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
type Extension struct {
nibbler.NoOpExtension

Client *elastic.Client
Url string
Client *elastic.Client
Url string
}

func (s *Extension) Init(app *nibbler.Application) error {
Expand Down Expand Up @@ -69,4 +69,4 @@ func (s *Extension) NewBulkUpdateRequest() *elastic.BulkUpdateRequest {

func (s *Extension) NewIdsQuery(types ...string) *elastic.IdsQuery {
return elastic.NewIdsQuery()
}
}
10 changes: 3 additions & 7 deletions database/elasticsearch/sample/sample.application.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"log"
"github.com/markdicksonjr/nibbler"
"github.com/markdicksonjr/nibbler/database/elasticsearch"
"log"
)

func main() {
Expand All @@ -23,15 +23,11 @@ func main() {

// initialize the application
appContext := nibbler.Application{}
err = appContext.Init(config, &logger, &extensions)

if err != nil {
if err = appContext.Init(config, &logger, &extensions); err != nil {
log.Fatal(err.Error())
}

err = appContext.Run()

if err != nil {
if err = appContext.Run(); err != nil {
log.Fatal(err.Error())
}
}
8 changes: 3 additions & 5 deletions database/mongo/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

type Extension struct {
nibbler.NoOpExtension
Client *mongo.Client
Url string
Client *mongo.Client
Url string
}

// e.g. mongodb://foo:bar@localhost:27017
Expand All @@ -31,7 +31,5 @@ func (s *Extension) Init(app *nibbler.Application) error {
return err
}

err = s.Client.Connect(context.TODO())

return err
return s.Client.Connect(context.TODO())
}
16 changes: 6 additions & 10 deletions database/mongo/sample/sample.application.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main

import (
"log"
"context"
"github.com/markdicksonjr/nibbler"
"github.com/markdicksonjr/nibbler/database/mongo"
"context"
"log"
)

type Animal struct {
Name string `json:"name" bson:"name"`
Type string `json:"type" bson:"type"`
Name string `json:"name" bson:"name"`
Type string `json:"type" bson:"type"`
}

func main() {
Expand All @@ -30,9 +30,7 @@ func main() {

// initialize the application
app := nibbler.Application{}
err = app.Init(config, &logger, &extensions)

if err != nil {
if err = app.Init(config, &logger, &extensions); err != nil {
log.Fatal(err.Error())
}

Expand All @@ -47,9 +45,7 @@ func main() {

log.Println(insertResult.InsertedID)

err = app.Run()

if err != nil {
if err = app.Run(); err != nil {
log.Fatal(err.Error())
}
}
15 changes: 7 additions & 8 deletions database/redis/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package redis

import (
"github.com/go-redis/redis"
_ "github.com/go-redis/redis"
"github.com/markdicksonjr/nibbler"
"net/url"
)

type Extension struct {
nibbler.NoOpExtension

Url string
Password string
DB int
Client *redis.Client
Url string
Password string
DB int
Client *redis.Client
}

func (s *Extension) Init(app *nibbler.Application) error {
Expand All @@ -29,7 +28,7 @@ func (s *Extension) Init(app *nibbler.Application) error {
}

if len(s.Url) > 0 {
parsedUrl, err := url.Parse(s.Url);
parsedUrl, err := url.Parse(s.Url)
if err != nil {
return nil
}
Expand Down Expand Up @@ -61,10 +60,10 @@ func (s *Extension) Connect() error {
s.Client = redis.NewClient(&redis.Options{
Addr: s.Url,
Password: s.Password,
DB: s.DB, // 0 = use default DB
DB: s.DB, // 0 = use default DB
})

_, err := s.Client.Ping().Result()

return err
}
}
9 changes: 2 additions & 7 deletions database/redis/sample/sample.application.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ func main() {

// initialize the application
app := nibbler.Application{}
err = app.Init(config, &logger, &extensions)

if err != nil {
if err = app.Init(config, &logger, &extensions); err != nil {
log.Fatal(err.Error())
}

Expand All @@ -43,10 +41,7 @@ func main() {

log.Println(strCmd.Val() == "sd")

err = app.Run()

if err != nil {
if err = app.Run(); err != nil {
log.Fatal(err.Error())
}
}

Loading

0 comments on commit 7323134

Please sign in to comment.