diff --git a/README.md b/README.md index ce60ff3..ab19e83 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ +# Go Restful API Boilerplate -## Go Restful API Boilerplate +[![GoDoc Badge]][GoDoc] [![GoReportCard Badge]][GoReportCard] Easily extendible RESTful API boilerplate aiming to follow idiomatic go and best practice. @@ -74,4 +75,9 @@ EMAIL_FROM_NAME | string || from name used in sending emails ### Contributing -Any feedback and pull requests are welcome and highly appreciated. Please open an issue first if you intend to send in a larger pull request or want to add additional features. \ No newline at end of file +Any feedback and pull requests are welcome and highly appreciated. Please open an issue first if you intend to send in a larger pull request or want to add additional features. + +[GoDoc]: https://godoc.org/github.com/dhax/go-base +[GoDoc Badge]: https://godoc.org/github.com/dhax/go-base?status.svg +[GoReportCard]: https://goreportcard.com/report/github.com/dhax/go-base +[GoReportCard Badge]: https://goreportcard.com/badge/github.com/dhax/go-base \ No newline at end of file diff --git a/api/admin/accounts.go b/api/admin/accounts.go index 2daa87f..cdee723 100644 --- a/api/admin/accounts.go +++ b/api/admin/accounts.go @@ -27,7 +27,7 @@ type AccountStore interface { Delete(*auth.Account) error } -// AccountResource implements account managment handler. +// AccountResource implements account management handler. type AccountResource struct { Store AccountStore } diff --git a/api/admin/api.go b/api/admin/api.go index 3babffa..a18bab7 100644 --- a/api/admin/api.go +++ b/api/admin/api.go @@ -1,3 +1,4 @@ +// Package admin ties together administration resources and handlers. package admin import ( diff --git a/api/api.go b/api/api.go index 4166d27..abfde1b 100644 --- a/api/api.go +++ b/api/api.go @@ -1,3 +1,4 @@ +// Package api configures an http server for administration and application resources. package api import ( @@ -19,8 +20,8 @@ import ( "github.com/go-chi/render" ) -// NewAPI configures application resources and routes. -func NewAPI() (*chi.Mux, error) { +// New configures application resources and routes. +func New() (*chi.Mux, error) { logger := logging.NewLogger() db, err := database.DBConn() diff --git a/api/app/account.go b/api/app/account.go index 217d661..2b3adfd 100644 --- a/api/app/account.go +++ b/api/app/account.go @@ -22,7 +22,7 @@ type AccountStore interface { DeleteToken(*auth.Token) error } -// AccountResource implements account managment handler. +// AccountResource implements account management handler. type AccountResource struct { Store AccountStore } @@ -64,7 +64,7 @@ func (rs *AccountResource) accountCtx(next http.Handler) http.Handler { type accountRequest struct { *auth.Account - // override protected data here, although not really neccessary here + // override protected data here, although not really necessary here // as we limit updated database columns in store as well ProtectedID int `json:"id"` ProtectedActive bool `json:"active"` diff --git a/api/app/api.go b/api/app/api.go index 9a6ae7a..3331609 100644 --- a/api/app/api.go +++ b/api/app/api.go @@ -1,3 +1,4 @@ +// Package app ties together application resources and handlers. package app import ( diff --git a/api/server.go b/api/server.go index 9759005..93348bf 100644 --- a/api/server.go +++ b/api/server.go @@ -19,7 +19,7 @@ type Server struct { // NewServer creates and configures an APIServer serving all application routes. func NewServer() (*Server, error) { log.Println("configuring server...") - api, err := NewAPI() + api, err := New() if err != nil { return nil, err } diff --git a/auth/api.go b/auth/api.go index 3117210..0c063b3 100644 --- a/auth/api.go +++ b/auth/api.go @@ -1,3 +1,5 @@ +// Package auth provides JSON Web Token (JWT) authentication and authorization middleware. +// It implements a passwordless authentication flow by sending login tokens vie email which are then exchanged for JWT access and refresh tokens. package auth import ( @@ -59,7 +61,7 @@ func NewResource(store Storer, mailer Mailer) (*Resource, error) { return resource, nil } -// Router provides neccessary routes for passwordless authentication flow. +// Router provides necessary routes for passwordless authentication flow. func (rs *Resource) Router() *chi.Mux { r := chi.NewRouter() r.Use(render.SetContentType(render.ContentTypeJSON)) diff --git a/auth/handler_test.go b/auth/handler_test.go index 0a1bfb7..e1101d7 100644 --- a/auth/handler_test.go +++ b/auth/handler_test.go @@ -241,14 +241,16 @@ func TestAuthResource_refresh(t *testing.T) { if tc.status == http.StatusUnauthorized && authstore.SaveRefreshTokenInvoked { t.Errorf("SaveRefreshToken invoked for status %d", tc.status) } - if tc.status == http.StatusOK && !authstore.GetByRefreshTokenInvoked { - t.Errorf("GetRefreshToken not invoked") - } - if tc.status == http.StatusOK && !authstore.SaveRefreshTokenInvoked { - t.Errorf("SaveRefreshToken not invoked") - } - if tc.status == http.StatusOK && authstore.DeleteRefreshTokenInvoked { - t.Errorf("DeleteRefreshToken should not be invoked") + if tc.status == http.StatusOK { + if !authstore.GetByRefreshTokenInvoked { + t.Errorf("GetRefreshToken not invoked") + } + if !authstore.SaveRefreshTokenInvoked { + t.Errorf("SaveRefreshToken not invoked") + } + if authstore.DeleteRefreshTokenInvoked { + t.Errorf("DeleteRefreshToken should not be invoked") + } } authstore.GetByRefreshTokenInvoked = false authstore.SaveRefreshTokenInvoked = false diff --git a/cmd/gendoc.go b/cmd/gendoc.go index 012d7c7..d5cd88d 100644 --- a/cmd/gendoc.go +++ b/cmd/gendoc.go @@ -61,7 +61,7 @@ func init() { } func genRoutesDoc() { - api, _ := api.NewAPI() + api, _ := api.New() fmt.Print("generating routes markdown file: ") md := docgen.MarkdownRoutesDoc(api, docgen.MarkdownOpts{ ProjectPath: "github.com/dhax/go-base", diff --git a/database/migrate/main.go b/database/migrate/main.go index e90f93d..ae1930e 100644 --- a/database/migrate/main.go +++ b/database/migrate/main.go @@ -1,3 +1,4 @@ +// Package migrate implements postgres migrations. package migrate import ( diff --git a/database/postgres.go b/database/postgres.go index 9185041..3e4817c 100644 --- a/database/postgres.go +++ b/database/postgres.go @@ -1,3 +1,4 @@ +// Package database implements postgres connection and queries. package database import ( diff --git a/email/email.go b/email/email.go index d8a8bbb..6b8fabf 100644 --- a/email/email.go +++ b/email/email.go @@ -1,3 +1,4 @@ +// Package email provides email sending functionality. package email import ( diff --git a/logging/logger.go b/logging/logger.go index 6f84d8c..f45602c 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -1,3 +1,4 @@ +// Package logging provides structured logging with logrus. package logging import ( @@ -93,7 +94,7 @@ func (l *StructuredLoggerEntry) Write(status, bytes int, elapsed time.Duration) l.Logger = l.Logger.WithFields(logrus.Fields{ "resp_status": status, "resp_bytes_length": bytes, - "resp_elasped_ms": float64(elapsed.Nanoseconds()) / 1000000.0, + "resp_elapsed_ms": float64(elapsed.Nanoseconds()) / 1000000.0, }) l.Logger.Infoln("request complete") diff --git a/models/profile.go b/models/profile.go index 115b6ef..9f46232 100644 --- a/models/profile.go +++ b/models/profile.go @@ -1,3 +1,4 @@ +// Package models contains application specific entities. package models import (