graceful shutdown, CoreInterface changes, MongoDB official driver is the only DB backend

This commit is contained in:
Darko Luketic
2020-05-06 01:49:46 +02:00
parent b722cb8a62
commit 58609926b9
6 changed files with 277 additions and 175 deletions

View File

@ -1,153 +0,0 @@
package base
import (
"fmt"
"github.com/globalsign/mgo"
"github.com/jinzhu/gorm"
)
type Base struct{}
func (b *Base) Register() (string, error) {
if e := b.BeforeLoadPlugins(); e != nil {
return "", e
}
if e := b.LoadPlugins(); e != nil {
return "", e
}
if e := b.AfterLoadPlugins(); e != nil {
return "", e
}
if e := b.BeforeLoadConfiguration(); e != nil {
return "", e
}
if e := b.LoadConfiguration(); e != nil {
return "", e
}
if e := b.AfterLoadConfiguration(); e != nil {
return "", e
}
if e := b.BeforeConnectToDatabases(); e != nil {
return "", e
}
if e := b.ConnectToDatabases(); e != nil {
return "", e
}
if e := b.AfterConnectToDatabases(); e != nil {
return "", e
}
if e := b.BeforeSetupLogger(); e != nil {
return "", e
}
if e := b.SetupLogger(); e != nil {
return "", e
}
if e := b.AfterSetupLogger(); e != nil {
return "", e
}
if e := b.BeforeSetupRoutes(); e != nil {
return "", e
}
if e := b.SetupRoutes(); e != nil {
return "", e
}
if e := b.AfterSetupRoutes(); e != nil {
return "", e
}
if e := b.BeforeRun(); e != nil {
return "", e
}
if e := b.Run(); e != nil {
return "", e
}
if e := b.AfterRun(); e != nil {
return "", e
}
return "base", nil
}
func (b *Base) UnRegister() error {
return nil
}
func (b *Base) BeforeLoadPlugins() error {
fmt.Println("Hello World. BeforeLoadPlugins")
return nil
}
func (b *Base) LoadPlugins() error {
return nil
}
func (b *Base) AfterLoadPlugins() error {
return nil
}
func (b *Base) BeforeLoadConfiguration() error {
return nil
}
func (b *Base) LoadConfiguration() error {
return nil
}
func (b *Base) AfterLoadConfiguration() error {
return nil
}
func (b *Base) ConnectMongoDB(string) (*mgo.Session, error) {
return nil, nil
}
func (b *Base) ConnectGORM(string) (*gorm.DB, error) {
return nil, nil
}
func (b *Base) BeforeConnectToDatabases() error {
return nil
}
func (b *Base) ConnectToDatabases() error {
return nil
}
func (b *Base) AfterConnectToDatabases() error {
return nil
}
func (b *Base) BeforeSetupLogger() error {
return nil
}
func (b *Base) SetupLogger() error {
return nil
}
func (b *Base) AfterSetupLogger() error {
return nil
}
func (b *Base) BeforeSetupRoutes() error {
return nil
}
func (b *Base) SetupRoutes() error {
return nil
}
func (b *Base) AfterSetupRoutes() error {
return nil
}
func (b *Base) BeforeRun() error {
return nil
}
func (b *Base) Run() error {
return nil
}
func (b *Base) AfterRun() error {
return nil
}

198
core/core.go Normal file
View File

@ -0,0 +1,198 @@
package core
import (
"context"
"fmt"
"net/http"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Core struct {
client *mongo.Client
clientContext context.Context
clientTimeout time.Duration
clientURI string
}
func (c *Core) Register() (string, error) {
if e := c.BeforeLoadPlugins(); e != nil {
return "", e
}
if e := c.LoadPlugins(); e != nil {
return "", e
}
if e := c.AfterLoadPlugins(); e != nil {
return "", e
}
if e := c.BeforeLoadConfiguration(); e != nil {
return "", e
}
if e := c.LoadConfiguration(); e != nil {
return "", e
}
if e := c.AfterLoadConfiguration(); e != nil {
return "", e
}
if e := c.BeforeDatabaseInit(); e != nil {
return "", e
}
if e := c.DatabaseInit(); e != nil {
return "", e
}
if e := c.AfterDatabaseInit(); e != nil {
return "", e
}
if e := c.BeforeSetupLogger(); e != nil {
return "", e
}
if e := c.SetupLogger(); e != nil {
return "", e
}
if e := c.AfterSetupLogger(); e != nil {
return "", e
}
if e := c.BeforeSetupRoutes(); e != nil {
return "", e
}
if e := c.SetupRoutes(); e != nil {
return "", e
}
if e := c.AfterSetupRoutes(); e != nil {
return "", e
}
return "core", nil
}
func (c *Core) UnRegister() error {
fmt.Println("Core: UnRegister")
if e := c.BeforeShutdown(); e != nil {
return e
}
if e := c.Shutdown(); e != nil {
return e
}
return nil
}
func (c *Core) BeforeLoadPlugins() error {
fmt.Println("Core: BeforeLoadPlugins")
return nil
}
func (c *Core) LoadPlugins() error {
fmt.Println("Core: LoadPlugins")
return nil
}
func (c *Core) AfterLoadPlugins() error {
fmt.Println("Core: AfterLoadPlugins")
return nil
}
func (c *Core) BeforeLoadConfiguration() error {
fmt.Println("Core: BeforeLoadConfiguration")
return nil
}
func (c *Core) LoadConfiguration() error {
fmt.Println("Core: LoadConfiguration")
c.clientURI = "mongodb://localhost:27017/"
return nil
}
func (c *Core) AfterLoadConfiguration() error {
fmt.Println("Core: AfterLoadConfiguration")
return nil
}
func (c *Core) BeforeDatabaseInit() error {
fmt.Println("Core: BeforeDatabaseInit")
return nil
}
func (c *Core) DatabaseInit() error {
fmt.Println("Core: DatabaseInit")
c.clientTimeout = 10 * time.Second
c.clientContext = context.Background()
var e error
c.client, e = mongo.NewClient(options.Client().ApplyURI(c.clientURI))
if e != nil {
return e
}
ctx, cancel := context.WithTimeout(c.clientContext, c.clientTimeout)
defer cancel()
return c.client.Connect(ctx)
}
func (c *Core) AfterDatabaseInit() error {
fmt.Println("Core: AfterDatabaseInit")
return nil
}
func (c *Core) BeforeSetupLogger() error {
fmt.Println("Core: BeforeSetupLogger")
return nil
}
func (c *Core) SetupLogger() error {
fmt.Println("Core: SetupLogger")
return nil
}
func (c *Core) AfterSetupLogger() error {
fmt.Println("Core: AfterSetupLogger")
return nil
}
func (c *Core) BeforeSetupRoutes() error {
fmt.Println("Core: BeforeSetupRoutes")
return nil
}
func (c *Core) SetupRoutes() error {
fmt.Println("Core: SetupRoutes")
return nil
}
func (c *Core) AfterSetupRoutes() error {
fmt.Println("Core: AfterSetupRoutes")
return nil
}
func (c *Core) BeforeRun() error {
fmt.Println("Core: BeforeRun")
return nil
}
func (c *Core) Run() error {
fmt.Println("Core: Run")
return http.ListenAndServe(":8080", nil)
}
func (c *Core) AfterRun() error {
fmt.Println("Core: AfterRun")
return nil
}
func (c *Core) BeforeShutdown() error {
fmt.Println("Core: BeforeShutdown")
return nil
}
func (c *Core) Shutdown() error {
fmt.Println("Core: Shutdown")
return c.Close()
}
func (c *Core) Client() *mongo.Client {
fmt.Println("Core: Client")
return c.client
}
func (c *Core) Close() error {
fmt.Println("Core: Close")
return c.client.Disconnect(c.clientContext)
}