docs: for multiple golang files
continuous-integration/drone the build failed Details

This commit is contained in:
Geno 2021-06-01 17:41:05 +02:00
parent d007cc0ce2
commit d89da311b1
9 changed files with 35 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
"gorm.io/gorm/logger" "gorm.io/gorm/logger"
) )
// Database struct to read from config
type Database struct { type Database struct {
DB *gorm.DB DB *gorm.DB
Connection string `toml:"connection"` Connection string `toml:"connection"`
@ -17,6 +18,7 @@ type Database struct {
migrationTestdata map[string]*gormigrate.Migration migrationTestdata map[string]*gormigrate.Migration
} }
// Run database config - connect and migrate
func (config *Database) Run() error { func (config *Database) Run() error {
db, err := gorm.Open(postgres.Open(config.Connection), &gorm.Config{ db, err := gorm.Open(postgres.Open(config.Connection), &gorm.Config{
Logger: logger.Default.LogMode(config.LogLevel), Logger: logger.Default.LogMode(config.LogLevel),
@ -36,6 +38,7 @@ func (config *Database) Run() error {
return nil return nil
} }
// Status get status - is database pingable
func (config *Database) Status() error { func (config *Database) Status() error {
sqlDB, err := config.DB.DB() sqlDB, err := config.DB.DB()
if err != nil { if err != nil {

View File

@ -9,6 +9,7 @@ import (
) )
var ( var (
// ErrNothingToMigrate if nothing has to be migrated
ErrNothingToMigrate = errors.New("there is nothing to migrate") ErrNothingToMigrate = errors.New("there is nothing to migrate")
) )
@ -28,9 +29,12 @@ func (config *Database) sortedMigration(testdata bool) []*gormigrate.Migration {
return migrations return migrations
} }
// Migrate run migration
func (config *Database) Migrate() error { func (config *Database) Migrate() error {
return config.migrate(false) return config.migrate(false)
} }
// MigrateTestdata run migration and testdata migration
func (config *Database) MigrateTestdata() error { func (config *Database) MigrateTestdata() error {
return config.migrate(true) return config.migrate(true)
} }
@ -45,9 +49,12 @@ func (config *Database) migrate(testdata bool) error {
return m.Migrate() return m.Migrate()
} }
// AddMigration add to database config migration step
func (config *Database) AddMigration(m ...*gormigrate.Migration) { func (config *Database) AddMigration(m ...*gormigrate.Migration) {
config.addMigrate(false, m...) config.addMigrate(false, m...)
} }
// AddMigrationTestdata add to database config migration step of testdata
func (config *Database) AddMigrationTestdata(m ...*gormigrate.Migration) { func (config *Database) AddMigrationTestdata(m ...*gormigrate.Migration) {
config.addMigrate(true, m...) config.addMigrate(true, m...)
} }
@ -68,6 +75,7 @@ func (config *Database) addMigrate(testdata bool, m ...*gormigrate.Migration) {
} }
} }
// ReRun Rollback und run every migration step again till id
func (config *Database) ReRun(id string) { func (config *Database) ReRun(id string) {
migrations := config.sortedMigration(true) migrations := config.sortedMigration(true)
x := 0 x := 0

View File

@ -9,13 +9,17 @@ import (
) )
var ( var (
VERSION string = "" // VERSION string on status API
UP func() bool = func() bool { VERSION string = ""
// UP function to detect, if API is healthy
UP func() bool = func() bool {
return true return true
} }
// EXTRAS show more informations in status
EXTRAS interface{} = nil EXTRAS interface{} = nil
) )
// Status API response
type Status struct { type Status struct {
Version string `json:"version"` Version string `json:"version"`
Up bool `json:"up"` Up bool `json:"up"`

View File

@ -1,5 +1,6 @@
package web package web
// HTTPError as a response, with data
type HTTPError struct { type HTTPError struct {
Message string `json:"message" example:"invalid format"` Message string `json:"message" example:"invalid format"`
Error string `json:"error,omitempty" example:"<internal error message>"` Error string `json:"error,omitempty" example:"<internal error message>"`
@ -7,7 +8,10 @@ type HTTPError struct {
} }
const ( const (
// APIErrorInvalidRequestFormat const for api error with invalid request format
APIErrorInvalidRequestFormat = "Invalid Request Format" APIErrorInvalidRequestFormat = "Invalid Request Format"
APIErrorInternalDatabase = "Internal Database Error" // APIErrorInternalDatabase const for api error with problem with database
APIErrorNotFound = "Not found" APIErrorInternalDatabase = "Internal Database Error"
// APIErrorNotFound const for api error with not found object
APIErrorNotFound = "Not found"
) )

View File

@ -55,7 +55,6 @@ func (config *Service) Run() error {
Cache: autocert.DirCache(config.ACME.Cache), Cache: autocert.DirCache(config.ACME.Cache),
} }
return autotls.RunWithManager(r, &m) return autotls.RunWithManager(r, &m)
} else {
return r.Run(config.Listen)
} }
return r.Run(config.Listen)
} }

View File

@ -17,9 +17,12 @@ import (
) )
var ( var (
NAMESPACE string = "service" // NAMESPACE of prometheus metrics
VERSION string = "" NAMESPACE string = "service"
UP func() bool = func() bool { // VERSION in prometheus metrics
VERSION string = ""
// UP function for healthy check in prometheus metrics
UP func() bool = func() bool {
return true return true
} }
) )

View File

@ -10,12 +10,15 @@ var (
modules []ModuleRegisterFunc modules []ModuleRegisterFunc
) )
// ModuleRegisterFunc format of module which registerd to WebService
type ModuleRegisterFunc func(*gin.Engine, *Service) type ModuleRegisterFunc func(*gin.Engine, *Service)
// ModuleRegister used on start of WebService
func ModuleRegister(f ModuleRegisterFunc) { func ModuleRegister(f ModuleRegisterFunc) {
modules = append(modules, f) modules = append(modules, f)
} }
// Bind WebService to gin.Engine
func (ws *Service) Bind(r *gin.Engine) { func (ws *Service) Bind(r *gin.Engine) {
for _, f := range modules { for _, f := range modules {
f(r, ws) f(r, ws)

View File

@ -6,6 +6,7 @@ import (
"time" "time"
) )
// JSONRequest easy get request for json
func JSONRequest(url string, value interface{}) error { func JSONRequest(url string, value interface{}) error {
var netClient = &http.Client{ var netClient = &http.Client{
Timeout: time.Second * 20, Timeout: time.Second * 20,

View File

@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// LoadSession module to start Session Handling in WebService
func (config *Service) LoadSession(r *gin.Engine) { func (config *Service) LoadSession(r *gin.Engine) {
store := cookie.NewStore([]byte(config.Session.Secret)) store := cookie.NewStore([]byte(config.Session.Secret))
r.Use(sessions.Sessions(config.Session.Name, store)) r.Use(sessions.Sessions(config.Session.Name, store))