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) }