web: move global module register to service
continuous-integration/drone the build is pending Details

This commit is contained in:
Geno 2021-07-19 17:59:08 +02:00
parent ffd77c3aab
commit 6af94245b6
18 changed files with 214 additions and 205 deletions

View File

@ -34,8 +34,7 @@ type Status struct {
// @Failure 400 {object} web.HTTPError
// @Failure 404 {object} web.HTTPError
// @Router /api/status [get]
func init() {
web.ModuleRegister(func(r *gin.Engine, ws *web.Service) {
func Register(r *gin.Engine, ws *web.Service) {
r.GET("/api/status", func(c *gin.Context) {
status := &Status{
Version: VERSION,
@ -48,5 +47,4 @@ func init() {
}
c.JSON(http.StatusOK, status)
})
})
}

View File

@ -11,7 +11,7 @@ import (
func TestAPIStatus(t *testing.T) {
assert := assert.New(t)
s, err := webtest.New()
s, err := webtest.New(Register)
assert.NoError(err)
defer s.Close()
assert.NotNil(s)

View File

@ -27,8 +27,7 @@ type login struct {
// @Failure 500 {object} web.HTTPError
// @Router /api/v1/auth/login [post]
// @Param body body login false "login"
func init() {
web.ModuleRegister(func(r *gin.Engine, ws *web.Service) {
func apiLogin(r *gin.Engine, ws *web.Service) {
r.POST("/api/v1/auth/login", func(c *gin.Context) {
var data login
if err := c.BindJSON(&data); err != nil {
@ -73,5 +72,4 @@ func init() {
c.JSON(http.StatusOK, d)
})
})
}

View File

@ -12,7 +12,7 @@ import (
func TestAPILogin(t *testing.T) {
assert := assert.New(t)
s, err := webtest.NewWithDBSetup(SetupMigration)
s, err := webtest.NewWithDBSetup(apiLogin, SetupMigration)
assert.NoError(err)
defer s.Close()
assert.NotNil(s)

View File

@ -17,8 +17,7 @@ import (
// @Failure 500 {object} web.HTTPError
// @Router /api/v1/my/profil [delete]
// @Security ApiKeyAuth
func init() {
web.ModuleRegister(func(r *gin.Engine, ws *web.Service) {
func apiMyDelete(r *gin.Engine, ws *web.Service) {
r.DELETE("/api/v1/my/profil", func(c *gin.Context) {
id, ok := GetCurrentUserID(c)
if !ok {
@ -33,5 +32,4 @@ func init() {
}
c.JSON(http.StatusOK, true)
})
})
}

View File

@ -12,7 +12,7 @@ import (
func TestAPIDeleteMyProfil(t *testing.T) {
assert := assert.New(t)
s, err := webtest.NewWithDBSetup(SetupMigration)
s, err := webtest.NewWithDBSetup(Register, SetupMigration)
assert.NoError(err)
defer s.Close()
assert.NotNil(s)

View File

@ -20,8 +20,7 @@ import (
// @Router /api/v1/my/auth/password [post]
// @Security ApiKeyAuth
// @Param body body string false "new password"
func init() {
web.ModuleRegister(func(r *gin.Engine, ws *web.Service) {
func apiMyPassword(r *gin.Engine, ws *web.Service) {
r.POST("/api/v1/my/auth/password", MiddlewareLogin(ws), func(c *gin.Context) {
d, ok := GetCurrentUser(c, ws)
if !ok {
@ -53,5 +52,4 @@ func init() {
c.JSON(http.StatusOK, true)
})
})
}

View File

@ -12,7 +12,7 @@ import (
func TestAPIPassword(t *testing.T) {
assert := assert.New(t)
s, err := webtest.NewWithDBSetup(SetupMigration)
s, err := webtest.NewWithDBSetup(Register, SetupMigration)
assert.NoError(err)
defer s.Close()
assert.NotNil(s)

View File

@ -18,13 +18,11 @@ import (
// @Failure 500 {object} web.HTTPError
// @Router /api/v1/my/auth/status [get]
// @Security ApiKeyAuth
func init() {
web.ModuleRegister(func(r *gin.Engine, ws *web.Service) {
func apiMyStatus(r *gin.Engine, ws *web.Service) {
r.GET("/api/v1/my/auth/status", MiddlewareLogin(ws), func(c *gin.Context) {
d, ok := GetCurrentUser(c, ws)
if ok {
c.JSON(http.StatusOK, d)
}
})
})
}

View File

@ -10,9 +10,9 @@ import (
"dev.sum7.eu/genofire/golang-lib/web/webtest"
)
func TestAPIStatus(t *testing.T) {
func TestAPIMyStatus(t *testing.T) {
assert := assert.New(t)
s, err := webtest.NewWithDBSetup(SetupMigration)
s, err := webtest.NewWithDBSetup(Register, SetupMigration)
assert.NoError(err)
defer s.Close()
assert.NotNil(s)

View File

@ -28,8 +28,7 @@ type PasswordWithForgetCode struct {
// @Failure 500 {object} web.HTTPError
// @Router /api/v1/auth/password/code [post]
// @Param body body PasswordWithForgetCode false "new password and forget code"
func init() {
web.ModuleRegister(func(r *gin.Engine, ws *web.Service) {
func apiPasswordCode(r *gin.Engine, ws *web.Service) {
r.POST("/api/v1/auth/password/code", func(c *gin.Context) {
var req PasswordWithForgetCode
if err := c.BindJSON(&req); err != nil {
@ -72,5 +71,4 @@ func init() {
}
c.JSON(http.StatusOK, d.Username)
})
})
}

View File

@ -13,7 +13,7 @@ import (
func TestAPIPasswordCode(t *testing.T) {
assert := assert.New(t)
s, err := webtest.NewWithDBSetup(SetupMigration)
s, err := webtest.NewWithDBSetup(apiPasswordCode, SetupMigration)
assert.NoError(err)
defer s.Close()
assert.NotNil(s)

15
web/auth/main.go Normal file
View File

@ -0,0 +1,15 @@
package auth
import (
"dev.sum7.eu/genofire/golang-lib/web"
"github.com/gin-gonic/gin"
)
// Register to WebService
func Register(r *gin.Engine, ws *web.Service) {
apiLogin(r, ws)
apiMyDelete(r, ws)
apiMyPassword(r, ws)
apiMyStatus(r, ws)
apiPasswordCode(r, ws)
}

View File

@ -3,9 +3,11 @@ package web
import (
"github.com/bdlm/log"
"github.com/gin-gonic/gin"
// acme
"github.com/gin-gonic/autotls"
"golang.org/x/crypto/acme/autocert"
// internal
"dev.sum7.eu/genofire/golang-lib/mailer"
"gorm.io/gorm"
@ -30,6 +32,8 @@ type Service struct {
// internal
DB *gorm.DB `toml:"-"`
Mailer *mailer.Service `toml:"-"`
modules []ModuleRegisterFunc
}
// Run to startup all related web parts

View File

@ -10,6 +10,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
// db metrics
gormPrometheus "gorm.io/plugin/prometheus"
@ -27,8 +28,8 @@ var (
}
)
func init() {
web.ModuleRegister(func(r *gin.Engine, ws *web.Service) {
// Register to WebService
func Register(r *gin.Engine, ws *web.Service) {
r.Use(ginprom.PromMiddleware(&ginprom.PromOpts{
EndpointLabelMappingFn: func(c *gin.Context) string {
url := c.Request.URL.Path
@ -63,5 +64,4 @@ func init() {
}
r.GET("/metrics", ginprom.PromHandler(promhttp.Handler()))
})
}

View File

@ -11,7 +11,7 @@ import (
func TestMetricsLoaded(t *testing.T) {
assert := assert.New(t)
s, err := webtest.New()
s, err := webtest.New(Register)
assert.NoError(err)
defer s.Close()
assert.NotNil(s)

View File

@ -6,24 +6,20 @@ import (
"github.com/gin-gonic/gin"
)
var (
modules []ModuleRegisterFunc
)
// ModuleRegisterFunc format of module which registered to WebService
type ModuleRegisterFunc func(*gin.Engine, *Service)
// ModuleRegister used on start of WebService
func ModuleRegister(f ModuleRegisterFunc) {
modules = append(modules, f)
func (ws *Service) ModuleRegister(f ModuleRegisterFunc) {
ws.modules = append(ws.modules, f)
}
// Bind WebService to gin.Engine
func (ws *Service) Bind(r *gin.Engine) {
for _, f := range modules {
for _, f := range ws.modules {
f(r, ws)
}
log.Infof("loaded %d modules", len(modules))
log.Infof("loaded %d modules", len(ws.modules))
r.Use(static.Serve("/", static.LocalFile(ws.Webroot, false)))
}

View File

@ -25,6 +25,7 @@ var (
type Option struct {
ReRun bool
DBSetup func(db *database.Database)
ModuleLoader web.ModuleRegisterFunc
}
type testServer struct {
@ -32,7 +33,7 @@ type testServer struct {
Mails chan *mailer.TestingMail
Close func()
gin *gin.Engine
ws *web.Service
WS *web.Service
lastCookies []*http.Cookie
}
@ -43,13 +44,17 @@ type Login struct {
}
// New starts WebService for testing
func New() (*testServer, error) {
return NewWithOption(Option{})
func New(modules web.ModuleRegisterFunc) (*testServer, error) {
return NewWithOption(Option{ModuleLoader: modules})
}
// NewWithDBSetup allows to reconfigure before ReRun the database - e.g. for adding Migration-Steps
func NewWithDBSetup(dbCall func(db *database.Database)) (*testServer, error) {
return NewWithOption(Option{ReRun: true, DBSetup: dbCall})
func NewWithDBSetup(modules web.ModuleRegisterFunc, dbCall func(db *database.Database)) (*testServer, error) {
return NewWithOption(Option{
ReRun: true,
DBSetup: dbCall,
ModuleLoader: modules,
})
}
// NewWithOption allows to configure WebService for testing
@ -92,6 +97,7 @@ func NewWithOption(option Option) (*testServer, error) {
DB: dbConfig.DB,
Mailer: mail,
}
ws.ModuleRegister(option.ModuleLoader)
ws.Session.Name = "mysession"
ws.Session.Secret = "hidden"
@ -103,13 +109,13 @@ func NewWithOption(option Option) (*testServer, error) {
Mails: mock.Mails,
Close: mock.Close,
gin: r,
ws: ws,
WS: ws,
}, nil
}
// DatabaseForget, to run a test without a database
func (s *testServer) DatabaseForget() {
s.ws.DB = nil
s.WS.DB = nil
s.DB = nil
}