diff --git a/application.go b/application.go index cf25f26..53c0864 100755 --- a/application.go +++ b/application.go @@ -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 { @@ -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 { @@ -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) @@ -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 { @@ -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 { @@ -204,4 +201,4 @@ func (s *NoOpExtension) Destroy(app *Application) error { func (s *NoOpExtension) AddRoutes(app *Application) error { return nil -} \ No newline at end of file +} diff --git a/auth/auth0/extension.go b/auth/auth0/extension.go index 8551317..5212737 100644 --- a/auth/auth0/extension.go +++ b/auth/auth0/extension.go @@ -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 { @@ -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 } @@ -252,4 +252,4 @@ func (s *Extension) EnforceLoggedIn(routerFunc func(http.ResponseWriter, *http.R routerFunc(w, r) } -} \ No newline at end of file +} diff --git a/auth/auth0/sample/sample.application.go b/auth/auth0/sample/sample.application.go index ac391fd..f339ae3 100644 --- a/auth/auth0/sample/sample.application.go +++ b/auth/auth0/sample/sample.application.go @@ -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 { @@ -41,7 +41,7 @@ func main() { // allocate auth0 extension auth0Extension := auth0.Extension{ - SessionExtension: &sessionExtension, + SessionExtension: &sessionExtension, LoggedInRedirectUrl: "/", } diff --git a/autowire.go b/autowire.go index c0ad016..20de357 100644 --- a/autowire.go +++ b/autowire.go @@ -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 @@ -35,7 +35,7 @@ func AutoWireExtensions(extensions *[]Extension, logger *Logger) ([]Extension, e typeName := typeVal.String() treeMap[typeVal] = &dependency{ extension: &thisExt, - typeName: typeName, + typeName: typeName, } } @@ -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 0 { - parsedUrl, err := url.Parse(s.Url); + parsedUrl, err := url.Parse(s.Url) if err != nil { return nil } @@ -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 -} \ No newline at end of file +} diff --git a/database/redis/sample/sample.application.go b/database/redis/sample/sample.application.go index 7b38b24..8b95b86 100644 --- a/database/redis/sample/sample.application.go +++ b/database/redis/sample/sample.application.go @@ -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()) } @@ -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()) } } - diff --git a/database/sql/extension.go b/database/sql/extension.go index 09dffdb..e04106c 100644 --- a/database/sql/extension.go +++ b/database/sql/extension.go @@ -3,22 +3,22 @@ package sql import ( "errors" "fmt" - "net/url" - "os" - "strings" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" _ "github.com/jinzhu/gorm/dialects/sqlite" _ "github.com/lib/pq" "github.com/markdicksonjr/nibbler" + "net/url" + "os" "strconv" + "strings" ) type Extension struct { nibbler.Extension Models []interface{} - Db *gorm.DB + Db *gorm.DB } func NullifyField(db *gorm.DB, field string) *gorm.DB { @@ -26,13 +26,13 @@ func NullifyField(db *gorm.DB, field string) *gorm.DB { } type Configuration struct { - Scheme string - Host string - Port string + Scheme string + Host string + Port string Username string Password *string - Path string - Query url.Values + Path string + Query url.Values } func (s *Extension) Init(app *nibbler.Application) error { @@ -294,4 +294,4 @@ func (s *Extension) validateConfiguration(configuration *Configuration) error { } return nil -} \ No newline at end of file +} diff --git a/database/sql/sample/sample.application.go b/database/sql/sample/sample.application.go index 68f82a8..205a953 100644 --- a/database/sql/sample/sample.application.go +++ b/database/sql/sample/sample.application.go @@ -26,15 +26,11 @@ 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()) } - err = app.Run() - - if err != nil { + if err = app.Run(); err != nil { log.Fatal(err.Error()) } } diff --git a/loggers.go b/loggers.go index 5d6fb40..d7e9f2e 100755 --- a/loggers.go +++ b/loggers.go @@ -2,7 +2,7 @@ package nibbler import "log" -type DefaultLogger struct {} +type DefaultLogger struct{} func (logger DefaultLogger) Error(message string) { log.Println(message) @@ -20,7 +20,7 @@ func (logger DefaultLogger) Debug(message string) { log.Println(message) } -type SilentLogger struct {} +type SilentLogger struct{} func (logger SilentLogger) Error(message string) { } diff --git a/mail/outbound/mandrill/extension.go b/mail/outbound/mandrill/extension.go index f80aa86..6b925b1 100644 --- a/mail/outbound/mandrill/extension.go +++ b/mail/outbound/mandrill/extension.go @@ -39,7 +39,6 @@ func (s *Extension) Connect() error { return nil } - func (s *Extension) SendMail(from *outbound.Email, subject string, to []*outbound.Email, plainTextContent string, htmlContent string) (*outbound.Response, error) { if !s.initialized { return nil, errors.New("send grid extension used for sending without initialization") diff --git a/mail/outbound/mandrill/sample/sample.application.go b/mail/outbound/mandrill/sample/sample.application.go index ce58268..068fb9c 100644 --- a/mail/outbound/mandrill/sample/sample.application.go +++ b/mail/outbound/mandrill/sample/sample.application.go @@ -29,7 +29,7 @@ func main() { // create and initialize the application app := nibbler.Application{} - if app.Init(config, &logger, &extensions); err != nil { + if err = app.Init(config, &logger, &extensions); err != nil { log.Fatal(err.Error()) } diff --git a/mail/outbound/sender.go b/mail/outbound/sender.go index 7b3491f..65e0aec 100644 --- a/mail/outbound/sender.go +++ b/mail/outbound/sender.go @@ -13,4 +13,4 @@ type Response struct { StatusCode int Body string Headers map[string][]string -} \ No newline at end of file +} diff --git a/mail/outbound/sendgrid/extension.go b/mail/outbound/sendgrid/extension.go index 5376016..bc6c0a4 100644 --- a/mail/outbound/sendgrid/extension.go +++ b/mail/outbound/sendgrid/extension.go @@ -2,17 +2,17 @@ package sendgrid import ( "errors" - "github.com/sendgrid/sendgrid-go/helpers/mail" - "github.com/sendgrid/sendgrid-go" "github.com/markdicksonjr/nibbler" "github.com/markdicksonjr/nibbler/mail/outbound" + "github.com/sendgrid/sendgrid-go" + "github.com/sendgrid/sendgrid-go/helpers/mail" ) type Extension struct { nibbler.NoOpExtension outbound.Sender - apiKey string + apiKey string initialized bool } @@ -47,12 +47,12 @@ func (s *Extension) SendMail(from *outbound.Email, subject string, to []*outboun } fromSg := mail.Email{ - Name: (*from).Name, + Name: (*from).Name, Address: (*from).Address, } toSg := mail.Email{ - Name: (*to[0]).Name, + Name: (*to[0]).Name, Address: (*to[0]).Address, } @@ -60,7 +60,7 @@ func (s *Extension) SendMail(from *outbound.Email, subject string, to []*outboun for i, v := range to { if i > 0 { toList = append(toList, &mail.Email{ - Name: (*v).Name, + Name: (*v).Name, Address: (*v).Address, }) } @@ -78,11 +78,11 @@ func (s *Extension) SendMail(from *outbound.Email, subject string, to []*outboun if res != nil { return &outbound.Response{ - Body: res.Body, - Headers: res.Headers, + Body: res.Body, + Headers: res.Headers, StatusCode: res.StatusCode, }, err } return nil, err -} \ No newline at end of file +} diff --git a/mail/outbound/sendgrid/sample/sample.application.go b/mail/outbound/sendgrid/sample/sample.application.go index 0297d2f..8fdb351 100644 --- a/mail/outbound/sendgrid/sample/sample.application.go +++ b/mail/outbound/sendgrid/sample/sample.application.go @@ -1,10 +1,10 @@ package main import ( - "log" "github.com/markdicksonjr/nibbler" - NibSendGrid "github.com/markdicksonjr/nibbler/mail/outbound/sendgrid" "github.com/markdicksonjr/nibbler/mail/outbound" + NibSendGrid "github.com/markdicksonjr/nibbler/mail/outbound/sendgrid" + "log" ) func main() { @@ -29,9 +29,7 @@ func main() { // create and 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()) } @@ -53,9 +51,7 @@ func main() { } // start the app - err = app.Run() - - if err != nil { + if err = app.Run(); err != nil { log.Fatal(err.Error()) } } diff --git a/mail/outbound/sparkpost/extension.go b/mail/outbound/sparkpost/extension.go index d9285b1..ab06843 100644 --- a/mail/outbound/sparkpost/extension.go +++ b/mail/outbound/sparkpost/extension.go @@ -2,16 +2,16 @@ package sparkpost import ( "errors" + "github.com/SparkPost/gosparkpost" "github.com/markdicksonjr/nibbler" "github.com/markdicksonjr/nibbler/mail/outbound" - "github.com/SparkPost/gosparkpost" ) type Extension struct { nibbler.NoOpExtension outbound.Sender - apiKey string + apiKey string initialized bool } @@ -38,9 +38,9 @@ func (s *Extension) SendMail(from *outbound.Email, subject string, to []*outboun } cfg := &gosparkpost.Config{ - BaseUrl: "https://api.sparkpost.com", - ApiKey: s.apiKey, - ApiVersion: 1, + BaseUrl: "https://api.sparkpost.com", + ApiKey: s.apiKey, + ApiVersion: 1, } var sp gosparkpost.Client err := sp.Init(cfg) @@ -68,8 +68,8 @@ func (s *Extension) SendMail(from *outbound.Email, subject string, to []*outboun if res != nil { return &outbound.Response{ - Body: "", // TODO: res.Body or res.HTTP.Body via Reader interface - Headers: res.HTTP.Header, + Body: "", // TODO: res.Body or res.HTTP.Body via Reader interface + Headers: res.HTTP.Header, StatusCode: res.HTTP.StatusCode, }, err } diff --git a/mail/outbound/sparkpost/sample/sample.application.go b/mail/outbound/sparkpost/sample/sample.application.go index 0aea1a1..caf9617 100644 --- a/mail/outbound/sparkpost/sample/sample.application.go +++ b/mail/outbound/sparkpost/sample/sample.application.go @@ -29,9 +29,7 @@ func main() { // create and 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()) } @@ -53,9 +51,7 @@ func main() { } // start the app - err = app.Run() - - if err != nil { + if err = app.Run(); err != nil { log.Fatal(err.Error()) } } diff --git a/sample/sample.application.go b/sample/sample.application.go index a3d3a1a..423169c 100755 --- a/sample/sample.application.go +++ b/sample/sample.application.go @@ -11,7 +11,7 @@ type SampleExtension struct { } func (s *SampleExtension) AddRoutes(context *nibbler.Application) error { - context.GetRouter().HandleFunc("/api/ok", func (w http.ResponseWriter, _ *http.Request) { + context.GetRouter().HandleFunc("/api/ok", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"result": "OK"}`)) diff --git a/session/connectors/sql.go b/session/connectors/sql.go index 5fa53fd..b8efc47 100644 --- a/session/connectors/sql.go +++ b/session/connectors/sql.go @@ -9,12 +9,12 @@ import ( ) type SqlStoreConnector struct { - SqlExtension *sql.Extension - Secret string - MaxAgeSeconds int + SqlExtension *sql.Extension + Secret string + MaxAgeSeconds int - connectionInfo string - db *gorm.DB + connectionInfo string + db *gorm.DB } func (s SqlStoreConnector) Connect() (error, sessions.Store) { diff --git a/session/extension.go b/session/extension.go index 6b72943..dccbc1b 100644 --- a/session/extension.go +++ b/session/extension.go @@ -17,8 +17,8 @@ type SessionStoreConnector interface { type Extension struct { nibbler.NoOpExtension SessionName string - StoreConnector SessionStoreConnector // creates cookie store if not provided - store *sessions.Store // created by this extension + StoreConnector SessionStoreConnector // creates cookie store if not provided + store *sessions.Store // created by this extension } func (s *Extension) Init(app *nibbler.Application) error { diff --git a/session/sample/sample.application.go b/session/sample/sample.application.go index 00206b5..53ce0f6 100644 --- a/session/sample/sample.application.go +++ b/session/sample/sample.application.go @@ -24,8 +24,9 @@ func main() { } // prepare models for initialization - var models []interface{} - models = append(models, user.User{}) + var models = []interface{}{ + user.User{}, + } // allocate an SQL controller, providing an sql extension sqlController := NibUserSql.Extension{ @@ -57,16 +58,12 @@ 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()) } // start the app - err = appContext.Run() - - if err != nil { + if err = appContext.Run(); err != nil { log.Fatal(err.Error()) } } diff --git a/storage/azure/blob/extension.go b/storage/azure/blob/extension.go index 7aa6bb4..0005f87 100644 --- a/storage/azure/blob/extension.go +++ b/storage/azure/blob/extension.go @@ -4,17 +4,17 @@ import ( "context" "errors" "fmt" - "net/url" "github.com/Azure/azure-storage-blob-go/azblob" "github.com/markdicksonjr/nibbler" + "net/url" ) type Extension struct { nibbler.Extension accountName string - accountKey string - credential *azblob.SharedKeyCredential + accountKey string + credential *azblob.SharedKeyCredential } func (s *Extension) Init(app *nibbler.Application) error { @@ -67,4 +67,3 @@ func (s *Extension) GetContainerURL(ctx context.Context, name string) (*azblob.C containerUrl := serviceURL.NewContainerURL(name) return &containerUrl, nil } - diff --git a/storage/azure/blob/sample/sample.application.go b/storage/azure/blob/sample/sample.application.go index b662c97..81ad4da 100644 --- a/storage/azure/blob/sample/sample.application.go +++ b/storage/azure/blob/sample/sample.application.go @@ -4,11 +4,11 @@ import ( "bytes" "context" "fmt" - "log" - "strings" "github.com/Azure/azure-storage-blob-go/azblob" - "github.com/markdicksonjr/nibbler/storage/azure/blob" "github.com/markdicksonjr/nibbler" + "github.com/markdicksonjr/nibbler/storage/azure/blob" + "log" + "strings" ) func main() { @@ -33,9 +33,7 @@ func main() { // create and 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()) } @@ -60,7 +58,7 @@ func main() { // create the blob with string (plain text) content. data := "Hello World!" - _, err = blobURL.AppendBlock(ctx, strings.NewReader(data), azblob.AppendBlobAccessConditions{}, nil)//..PutBlob(ctx, strings.NewReader(data), azblob.BlobHTTPHeaders{ContentType: "text/plain"}, azblob.Metadata{}, azblob.BlobAccessConditions{}) + _, err = blobURL.AppendBlock(ctx, strings.NewReader(data), azblob.AppendBlobAccessConditions{}, nil) //..PutBlob(ctx, strings.NewReader(data), azblob.BlobHTTPHeaders{ContentType: "text/plain"}, azblob.Metadata{}, azblob.BlobAccessConditions{}) if err != nil { log.Fatal(err) } @@ -110,11 +108,7 @@ func main() { } // start the app - err = app.Run() - - if err != nil { + if err = app.Run(); err != nil { log.Fatal(err.Error()) } } - - diff --git a/storage/google/drive/extension.go b/storage/google/drive/extension.go index 654ac25..2649073 100644 --- a/storage/google/drive/extension.go +++ b/storage/google/drive/extension.go @@ -16,14 +16,14 @@ import ( type Extension struct { nibbler.NoOpExtension - ConnectServiceOnInit bool - CredentialsPath string - TokenFilePath string + ConnectServiceOnInit bool + CredentialsPath string + TokenFilePath string - Srv *drive.Service - Client *http.Client + Srv *drive.Service + Client *http.Client - config *oauth2.Config + config *oauth2.Config } func (s *Extension) Init(app *nibbler.Application) error { diff --git a/storage/s3/extension.go b/storage/s3/extension.go index 388c6c7..7eb02c6 100644 --- a/storage/s3/extension.go +++ b/storage/s3/extension.go @@ -2,12 +2,12 @@ package s3 import ( "errors" - "log" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" "github.com/markdicksonjr/nibbler" + "log" ) type Extension struct { @@ -52,8 +52,8 @@ func (s *Extension) Init(app *nibbler.Application) error { }) sess, err := session.NewSession(&aws.Config{ Credentials: creds, - Endpoint: aws.String(configValue.Get("s3", "endpoint").String("")), - Region: aws.String(configValue.Get("s3", "region").String("")), + Endpoint: aws.String(configValue.Get("s3", "endpoint").String("")), + Region: aws.String(configValue.Get("s3", "region").String("")), }) if err != nil { @@ -64,4 +64,4 @@ func (s *Extension) Init(app *nibbler.Application) error { s.S3 = s3.New(sess) return nil -} \ No newline at end of file +} diff --git a/storage/s3/sample/sample.application.go b/storage/s3/sample/sample.application.go index 33388ea..0d7f90a 100644 --- a/storage/s3/sample/sample.application.go +++ b/storage/s3/sample/sample.application.go @@ -1,11 +1,11 @@ package main import ( - "log" "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/markdicksonjr/nibbler" "github.com/markdicksonjr/nibbler/storage/s3" + "log" ) func main() { @@ -30,9 +30,7 @@ func main() { // create and 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()) } @@ -50,9 +48,7 @@ func main() { } // start the app - err = app.Run() - - if err != nil { + if err = app.Run(); err != nil { log.Fatal(err.Error()) } } diff --git a/storage/sftp/client/extension.go b/storage/sftp/client/extension.go index d3f9fab..9d5d878 100644 --- a/storage/sftp/client/extension.go +++ b/storage/sftp/client/extension.go @@ -9,12 +9,12 @@ import ( type Extension struct { nibbler.NoOpExtension conn *ssh.Client - app *nibbler.Application + app *nibbler.Application Username string Password string - Host string - HostKey *ssh.PublicKey + Host string + HostKey *ssh.PublicKey Client *sftp.Client } diff --git a/storage/sftp/client/sample/sample.application.go b/storage/sftp/client/sample/sample.application.go index 1c31031..eda40e5 100644 --- a/storage/sftp/client/sample/sample.application.go +++ b/storage/sftp/client/sample/sample.application.go @@ -37,9 +37,7 @@ func main() { log.Println(strconv.Itoa(len(filesInfo))) // start the app - err = app.Run() - - if err != nil { + if err = app.Run(); err != nil { log.Fatal(err.Error()) } } diff --git a/user/auth/auth0/extension.go b/user/auth/auth0/extension.go index 5184911..d1335be 100644 --- a/user/auth/auth0/extension.go +++ b/user/auth/auth0/extension.go @@ -1,10 +1,10 @@ package auth0 import ( - "net/http" "github.com/markdicksonjr/nibbler" "github.com/markdicksonjr/nibbler/auth/auth0" "github.com/markdicksonjr/nibbler/user" + "net/http" ) type Extension struct { @@ -59,25 +59,21 @@ func (s *Extension) Init(app *nibbler.Application) error { return false, err } - err = s.SessionExtension.SetCaller(w, r, userValue) - - if err != nil { + if err = s.SessionExtension.SetCaller(w, r, userValue); err != nil { nibbler.Write500Json(w, err.Error()) return false, err } w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`{"result": "ok"}`)) + _, err = w.Write([]byte(`{"result": "ok"}`)) - return false, nil + return false, err } s.OnLogoutComplete = func(a *auth0.Extension, w http.ResponseWriter, r *http.Request) error { - s.SessionExtension.SetCaller(w, r, nil) - return nil + return s.SessionExtension.SetCaller(w, r, nil) } return nil } - diff --git a/user/auth/auth0/sample/sample.application.go b/user/auth/auth0/sample/sample.application.go index 8f1bb4a..4c6cffe 100644 --- a/user/auth/auth0/sample/sample.application.go +++ b/user/auth/auth0/sample/sample.application.go @@ -75,7 +75,7 @@ func main() { emailVal := "someone@example.com" password := "" _, errCreate := userExtension.Create(&user.User{ - Email: &emailVal, + Email: &emailVal, Password: &password, }) diff --git a/user/auth/auth0/sample/sample.extension.go b/user/auth/auth0/sample/sample.extension.go index e3325e7..e6fc195 100644 --- a/user/auth/auth0/sample/sample.extension.go +++ b/user/auth/auth0/sample/sample.extension.go @@ -34,4 +34,3 @@ func (s *SampleExtension) ProtectedRoute(w http.ResponseWriter, r *http.Request) nibbler.Write200Json(w, `{"result": "authorized"}`) } - diff --git a/user/auth/local/extension.go b/user/auth/local/extension.go index d2ae9a7..9b2e9ac 100644 --- a/user/auth/local/extension.go +++ b/user/auth/local/extension.go @@ -19,28 +19,28 @@ type Extension struct { Sender outbound.Sender // for password reset - PasswordResetEnabled bool - PasswordResetFromName string - PasswordResetFromEmail string - PasswordResetRedirect string // a UI or other service to handle the redirect from email (will have ?token=X or &token=X appended) - PasswordResetTokenExpirationDays *int + PasswordResetEnabled bool + PasswordResetFromName string + PasswordResetFromEmail string + PasswordResetRedirect string // a UI or other service to handle the redirect from email (will have ?token=X or &token=X appended) + PasswordResetTokenExpirationDays *int // for email verification - RegistrationEnabled bool - EmailVerificationEnabled bool // whether email verification is available (doesn't mean it's required) - EmailVerificationRequired bool // whether email verification is required before logging in - EmailVerificationTokenExpirationDays *int - EmailVerificationRedirect string - EmailVerificationFromName string - EmailVerificationFromEmail string + RegistrationEnabled bool + EmailVerificationEnabled bool // whether email verification is available (doesn't mean it's required) + EmailVerificationRequired bool // whether email verification is required before logging in + EmailVerificationTokenExpirationDays *int + EmailVerificationRedirect string + EmailVerificationFromName string + EmailVerificationFromEmail string // callbacks (for extending default behavior) - OnLoginSuccessful *func(loggedInUser user.User, sessionMaxAgeMinutes int) - OnLogoutSuccessful *func(loggedOutUser user.User) - OnRegistrationSuccessful *func(registeredUser user.User) - OnEmailVerificationSuccessful *func(registeredUser user.User) + OnLoginSuccessful *func(loggedInUser user.User, sessionMaxAgeMinutes int) + OnLogoutSuccessful *func(loggedOutUser user.User) + OnRegistrationSuccessful *func(registeredUser user.User) + OnEmailVerificationSuccessful *func(registeredUser user.User) - app *nibbler.Application + app *nibbler.Application } func (s *Extension) Init(app *nibbler.Application) error { @@ -131,4 +131,4 @@ func (s *Extension) GetCurrentUserHandler(w http.ResponseWriter, r *http.Request } nibbler.Write200Json(w, jsonString) -} \ No newline at end of file +} diff --git a/user/auth/local/login.go b/user/auth/local/login.go index 02a7b86..950beec 100644 --- a/user/auth/local/login.go +++ b/user/auth/local/login.go @@ -90,8 +90,8 @@ func (s *Extension) LoginFormHandler(w http.ResponseWriter, r *http.Request) { (*s.OnLoginSuccessful)(safeUser, s.SessionExtension.StoreConnector.MaxAge()) } - nibbler.Write200Json(w, `{"user": ` + jsonString + - `, "sessionAgeSeconds":`+ strconv.Itoa(s.SessionExtension.StoreConnector.MaxAge()) + `}`) + nibbler.Write200Json(w, `{"user": `+jsonString+ + `, "sessionAgeSeconds":`+strconv.Itoa(s.SessionExtension.StoreConnector.MaxAge())+`}`) } func (s *Extension) LogoutHandler(w http.ResponseWriter, r *http.Request) { @@ -118,7 +118,6 @@ func (s *Extension) LogoutHandler(w http.ResponseWriter, r *http.Request) { func (s *Extension) Login(email string, password string) (*user.User, error) { u, err := s.UserExtension.GetUserByEmail(email); - if err != nil { return u, err } @@ -128,7 +127,6 @@ func (s *Extension) Login(email string, password string) (*user.User, error) { } validPassword, err := ValidatePassword(password, *u.Password); - if err != nil { return nil, err } @@ -143,4 +141,4 @@ func (s *Extension) Login(email string, password string) (*user.User, error) { } return u, nil -} \ No newline at end of file +} diff --git a/user/auth/local/password.go b/user/auth/local/password.go index 2d02b03..9cbbdbd 100644 --- a/user/auth/local/password.go +++ b/user/auth/local/password.go @@ -92,13 +92,13 @@ func (s *Extension) ResetPasswordTokenHandler(w http.ResponseWriter, r *http.Req // send the email _, err = s.Sender.SendMail( &outbound.Email{ - Name: s.PasswordResetFromName, + Name: s.PasswordResetFromName, Address: s.PasswordResetFromEmail, }, "Password Reset", // TODO: make configurable toList, - "Please go to " + link + " to reset your password", - "Please go to " + link + " to reset your password", + "Please go to "+link+" to reset your password", + "Please go to "+link+" to reset your password", ) }() @@ -150,7 +150,7 @@ func (s *Extension) getUserByPasswordResetTokenAndValidate(token string) (*user. userValue, err := s.UserExtension.GetUserByPasswordResetToken(token) - if err != nil || userValue == nil{ + if err != nil || userValue == nil { return nil, nil } @@ -163,4 +163,4 @@ func (s *Extension) getUserByPasswordResetTokenAndValidate(token string) (*user. } return userValue, nil -} \ No newline at end of file +} diff --git a/user/auth/local/registration.go b/user/auth/local/registration.go index 1ded775..d764055 100644 --- a/user/auth/local/registration.go +++ b/user/auth/local/registration.go @@ -24,7 +24,7 @@ func (s *Extension) RegisterFormHandler(w http.ResponseWriter, r *http.Request) // TODO: password meets requirements // look up the user with that email - u, err := s.UserExtension.GetUserByEmail(email); + u, err := s.UserExtension.GetUserByEmail(email) if err != nil { nibbler.Write500Json(w, err.Error()) return @@ -40,7 +40,7 @@ func (s *Extension) RegisterFormHandler(w http.ResponseWriter, r *http.Request) // begin putting together a new user emailValidated := !s.EmailVerificationEnabled userValue := user.User{ - Email: &email, + Email: &email, IsEmailValidated: &emailValidated, } @@ -87,7 +87,7 @@ func (s *Extension) RegisterFormHandler(w http.ResponseWriter, r *http.Request) go func() { // generate the link for the email - var link= s.EmailVerificationRedirect + var link = s.EmailVerificationRedirect useAmpersand := strings.Contains(s.EmailVerificationRedirect, "?") if useAmpersand { link += "&token=" + *userValue.EmailValidationToken @@ -108,13 +108,13 @@ func (s *Extension) RegisterFormHandler(w http.ResponseWriter, r *http.Request) // send the email _, err = s.Sender.SendMail( &outbound.Email{ - Name: s.EmailVerificationFromName, + Name: s.EmailVerificationFromName, Address: s.EmailVerificationFromEmail, }, "Email Verification", // TODO: make configurable toList, - "Please go to " + link + " to verify your email", // TODO: configurable, with template param for link - "Please go to " + link + " to verify your email", // TODO: configurable, with template param for link + "Please go to "+link+" to verify your email", // TODO: configurable, with template param for link + "Please go to "+link+" to verify your email", // TODO: configurable, with template param for link ) if err != nil { @@ -127,7 +127,7 @@ func (s *Extension) RegisterFormHandler(w http.ResponseWriter, r *http.Request) (*s.OnRegistrationSuccessful)(safeUser) } - nibbler.Write200Json(w, `{"user": ` + jsonString + `}`) + nibbler.Write200Json(w, `{"user": `+jsonString+`}`) } func (s *Extension) EmailTokenVerifyHandler(w http.ResponseWriter, r *http.Request) { @@ -206,7 +206,7 @@ func (s *Extension) getUserByEmailValidationToken(token string) (*user.User, err userValue, err := s.UserExtension.GetUserByEmailVerificationToken(token) - if err != nil || userValue == nil{ + if err != nil || userValue == nil { return nil, nil } @@ -219,4 +219,4 @@ func (s *Extension) getUserByEmailValidationToken(token string) (*user.User, err } return userValue, nil -} \ No newline at end of file +} diff --git a/user/auth/local/sample/sample.application.go b/user/auth/local/sample/sample.application.go index 6089c3c..a1c6866 100644 --- a/user/auth/local/sample/sample.application.go +++ b/user/auth/local/sample/sample.application.go @@ -39,13 +39,11 @@ func main() { log.Fatal(err) } - // prepare models for initialization - var models []interface{} - models = append(models, NibUser.User{}) - // allocate the sql extension, with all models sqlExtension := sql.Extension{ - Models: models, + Models: []interface{}{ + NibUser.User{}, + }, } // allocate user extension, providing sql extension to it @@ -58,11 +56,11 @@ func main() { // allocate session extension connector := connectors.SqlStoreConnector{ SqlExtension: &sqlExtension, - Secret: "dumbsecret", + Secret: "dumbsecret", } sessionExtension := session.Extension{ StoreConnector: &connector, - SessionName: "dumbcookie", + SessionName: "dumbcookie", } // allocate the sendgrid extension @@ -72,17 +70,17 @@ func main() { userLocalAuthExtension := NibUserLocalAuth.Extension{ SessionExtension: &sessionExtension, UserExtension: &userExtension, - Sender: &sendgridExtension, + Sender: &sendgridExtension, PasswordResetEnabled: true, PasswordResetFromName: "Test", PasswordResetFromEmail: "test@example.com", PasswordResetRedirect: "http://localhost:3000/test-ui", - RegistrationEnabled: true, - EmailVerificationEnabled:true, - EmailVerificationFromName:"Test", - EmailVerificationFromEmail:"test@example.com", - EmailVerificationRedirect:"http://localhost:3000/verify", + RegistrationEnabled: true, + EmailVerificationEnabled: true, + EmailVerificationFromName: "Test", + EmailVerificationFromEmail: "test@example.com", + EmailVerificationRedirect: "http://localhost:3000/verify", } // prepare extensions for initialization @@ -99,9 +97,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()) } @@ -118,7 +114,7 @@ func main() { // create a test user, if it does not exist password := NibUserLocalAuth.GeneratePasswordHash("tester123") _, errCreate := userExtension.Create(&NibUser.User{ - Email: &emailVal, + Email: &emailVal, Password: &password, }) @@ -129,9 +125,7 @@ func main() { } // start the app - err = app.Run() - - if err != nil { + if err = app.Run(); err != nil { log.Fatal(err.Error()) } } diff --git a/user/auth/local/security.go b/user/auth/local/security.go index ac61c84..0c22fa6 100644 --- a/user/auth/local/security.go +++ b/user/auth/local/security.go @@ -36,4 +36,4 @@ func ValidatePassword(password string, hashedPassword string) (bool, error) { return true, nil } return false, errors.New("password did not match") -} \ No newline at end of file +} diff --git a/user/database/sql/extension.go b/user/database/sql/extension.go index 0791cfd..96e4f38 100644 --- a/user/database/sql/extension.go +++ b/user/database/sql/extension.go @@ -88,17 +88,17 @@ func (s *Extension) Update(userValue *user.User) error { // Update will not save nil values, but Save will, presumably return s.SqlExtension.Db.Model(userValue).Updates(user.User{ - ID: userValue.ID, - FirstName: userValue.FirstName, - LastName: userValue.LastName, - PasswordResetToken: userValue.PasswordResetToken, + ID: userValue.ID, + FirstName: userValue.FirstName, + LastName: userValue.LastName, + PasswordResetToken: userValue.PasswordResetToken, PasswordResetExpiration: userValue.PasswordResetExpiration, }).Error } func (s *Extension) UpdatePassword(userValue *user.User) (error) { if err := s.SqlExtension.Db.Model(userValue).Updates(user.User{ - ID: userValue.ID, + ID: userValue.ID, Password: userValue.Password, }).Error; err != nil { return err @@ -113,4 +113,4 @@ func (s *Extension) UpdatePassword(userValue *user.User) (error) { } return nil -} \ No newline at end of file +} diff --git a/user/extension.go b/user/extension.go index a24a60d..493c54d 100644 --- a/user/extension.go +++ b/user/extension.go @@ -14,8 +14,8 @@ type PersistenceExtension interface { GetUserByEmail(email string) (*User, error) GetUserByUsername(username string) (*User, error) Create(user *User) (*User, error) - Update(user *User) (error) - UpdatePassword(user *User) (error) + Update(user *User) error + UpdatePassword(user *User) error GetUserByPasswordResetToken(token string) (*User, error) GetUserByEmailValidationToken(token string) (*User, error) } @@ -23,14 +23,14 @@ type PersistenceExtension interface { type Extension struct { nibbler.Extension - PersistenceExtension PersistenceExtension + PersistenceExtension PersistenceExtension - OnBeforeUserCreate *func(user *User) - OnAfterUserCreate *func(user *User) - OnBeforeUserUpdate *func(user *User) - OnAfterUserUpdate *func(user *User) - OnBeforePasswordUpdate *func(user *User) - OnAfterPasswordUpdate *func(user *User) + OnBeforeUserCreate *func(user *User) + OnAfterUserCreate *func(user *User) + OnBeforeUserUpdate *func(user *User) + OnAfterUserUpdate *func(user *User) + OnBeforePasswordUpdate *func(user *User) + OnAfterPasswordUpdate *func(user *User) } func (s *Extension) Init(app *nibbler.Application) error { @@ -70,7 +70,6 @@ func (s *Extension) GetUserByPasswordResetToken(token string) (*User, error) { return nil, errors.New(noExtensionErrorMessage) } - func (s *Extension) GetUserByEmailVerificationToken(token string) (*User, error) { if s.PersistenceExtension != nil { return s.PersistenceExtension.GetUserByEmailValidationToken(token) @@ -157,4 +156,3 @@ func (s *Extension) UpdatePassword(user *User) error { } return errors.New(noExtensionErrorMessage) } - diff --git a/user/sample/sample.application.go b/user/sample/sample.application.go index 76bef6a..5c8aee1 100644 --- a/user/sample/sample.application.go +++ b/user/sample/sample.application.go @@ -1,19 +1,19 @@ package main import ( - "log" - "github.com/markdicksonjr/nibbler/user" "github.com/markdicksonjr/nibbler" - "github.com/markdicksonjr/nibbler/database/sql" "github.com/markdicksonjr/nibbler/database/elasticsearch" + "github.com/markdicksonjr/nibbler/database/sql" + "github.com/markdicksonjr/nibbler/user" NibUserElastic "github.com/markdicksonjr/nibbler/user/database/elastic" NibUserSql "github.com/markdicksonjr/nibbler/user/database/sql" + "log" ) type UserAndDbExtensions struct { - UserExtension *user.Extension - UserPersistenceExtension user.PersistenceExtension - DbExtension nibbler.Extension + UserExtension *user.Extension + UserPersistenceExtension user.PersistenceExtension + DbExtension nibbler.Extension } func allocateSqlExtensions() UserAndDbExtensions { @@ -23,7 +23,7 @@ func allocateSqlExtensions() UserAndDbExtensions { models = append(models, user.User{}) // allocate an SQL extension, providing the models for auto-migration - sqlExtension := sql.Extension{ Models: models } + sqlExtension := sql.Extension{Models: models} sqlUserExtension := NibUserSql.Extension{ SqlExtension: &sqlExtension, @@ -46,7 +46,7 @@ func allocateEsExtensions() UserAndDbExtensions { } return UserAndDbExtensions{ - DbExtension: &elasticExtension, + DbExtension: &elasticExtension, UserPersistenceExtension: &elasticUserExtension, UserExtension: &user.Extension{ PersistenceExtension: &elasticUserExtension,