mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-31 18:10:50 +01:00
updated code structure
This commit is contained in:
@@ -18,26 +18,25 @@ import (
|
||||
)
|
||||
|
||||
type Api struct {
|
||||
BaseApp
|
||||
app *gin.Engine
|
||||
srv *http.Server
|
||||
}
|
||||
|
||||
func (app *Api) init() {
|
||||
func (app *Api) Init() {
|
||||
// initialize config
|
||||
_ = app.initModule("config", config.InitConfig)
|
||||
_ = initModule("config", config.InitConfig)
|
||||
|
||||
// initialize mongo
|
||||
_ = app.initModule("mongo", mongo.InitMongo)
|
||||
_ = initModule("mongo", mongo.InitMongo)
|
||||
|
||||
// initialize redis
|
||||
_ = app.initModule("redis", redis.InitRedis)
|
||||
_ = initModule("redis", redis.InitRedis)
|
||||
|
||||
// initialize model services
|
||||
_ = app.initModule("mode-services", models.InitModelServices)
|
||||
_ = initModule("mode-services", models.InitModelServices)
|
||||
|
||||
// initialize controllers
|
||||
_ = app.initModule("controllers", controllers.InitControllers)
|
||||
_ = initModule("controllers", controllers.InitControllers)
|
||||
|
||||
// initialize middlewares
|
||||
_ = app.initModuleWithApp("middlewares", middlewares.InitMiddlewares)
|
||||
@@ -46,7 +45,7 @@ func (app *Api) init() {
|
||||
_ = app.initModuleWithApp("routes", routes.InitRoutes)
|
||||
}
|
||||
|
||||
func (app *Api) run() {
|
||||
func (app *Api) Run() {
|
||||
host := viper.GetString("server.host")
|
||||
port := viper.GetString("server.port")
|
||||
address := net.JoinHostPort(host, port)
|
||||
@@ -63,7 +62,7 @@ func (app *Api) run() {
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Api) stop() {
|
||||
func (app *Api) Stop() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
if err := app.srv.Shutdown(ctx); err != nil {
|
||||
@@ -72,7 +71,7 @@ func (app *Api) stop() {
|
||||
}
|
||||
|
||||
func (app *Api) initModuleWithApp(name string, fn func(app *gin.Engine) error) (err error) {
|
||||
return app.initModule(name, func() error {
|
||||
return initModule(name, func() error {
|
||||
return fn(app.app)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,58 +1,7 @@
|
||||
package apps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/apex/log"
|
||||
"github.com/crawlab-team/go-trace"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type App interface {
|
||||
init()
|
||||
run()
|
||||
stop()
|
||||
}
|
||||
|
||||
type BaseApp struct {
|
||||
}
|
||||
|
||||
func (app *BaseApp) init() {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (app *BaseApp) run() {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (app *BaseApp) stop() {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (app *BaseApp) start() {
|
||||
app.init()
|
||||
go app.run()
|
||||
app.waitForStop()
|
||||
app.stop()
|
||||
}
|
||||
|
||||
func (app *BaseApp) Start() {
|
||||
app.start()
|
||||
}
|
||||
|
||||
func (app *BaseApp) waitForStop() {
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
}
|
||||
|
||||
func (app *BaseApp) initModule(name string, fn func() error) (err error) {
|
||||
if err := fn(); err != nil {
|
||||
log.Error(fmt.Sprintf("init %s error: %s", name, err.Error()))
|
||||
_ = trace.TraceError(err)
|
||||
panic(err)
|
||||
}
|
||||
log.Info(fmt.Sprintf("initialized %s successfully", name))
|
||||
return nil
|
||||
Init()
|
||||
Run()
|
||||
Stop()
|
||||
}
|
||||
|
||||
@@ -2,21 +2,24 @@ package apps
|
||||
|
||||
import (
|
||||
"github.com/apex/log"
|
||||
"github.com/crawlab-team/crawlab-core/services"
|
||||
"github.com/crawlab-team/crawlab-core/grpc"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
BaseApp
|
||||
}
|
||||
|
||||
func (app *Handler) init() {
|
||||
_ = app.initModule("task-service", services.InitTaskService)
|
||||
func (app *Handler) Init() {
|
||||
_ = initModule("grpc", grpc.InitGrpcServices)
|
||||
}
|
||||
|
||||
func (app *Handler) run() {
|
||||
func (app *Handler) Run() {
|
||||
log.Info("handler has started")
|
||||
}
|
||||
|
||||
func (app *Handler) Stop() {
|
||||
log.Info("handler has stopped")
|
||||
}
|
||||
|
||||
func NewHandler() *Handler {
|
||||
return &Handler{}
|
||||
}
|
||||
|
||||
23
backend/apps/master.go
Normal file
23
backend/apps/master.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package apps
|
||||
|
||||
type Master struct {
|
||||
api *Api
|
||||
}
|
||||
|
||||
func (app *Master) Init() {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (app *Master) Run() {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (app *Master) Stop() {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func NewMaster() *Master {
|
||||
return &Master{
|
||||
api: NewApi(),
|
||||
}
|
||||
}
|
||||
25
backend/apps/scheduler.go
Normal file
25
backend/apps/scheduler.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package apps
|
||||
|
||||
import (
|
||||
"github.com/apex/log"
|
||||
"github.com/crawlab-team/crawlab-core/grpc"
|
||||
)
|
||||
|
||||
type Scheduler struct {
|
||||
}
|
||||
|
||||
func (app *Scheduler) Init() {
|
||||
_ = initModule("grpc", grpc.InitGrpcServices)
|
||||
}
|
||||
|
||||
func (app *Scheduler) Run() {
|
||||
log.Info("scheduler has started")
|
||||
}
|
||||
|
||||
func (app *Scheduler) Stop() {
|
||||
log.Info("scheduler has stopped")
|
||||
}
|
||||
|
||||
func NewScheduler() *Scheduler {
|
||||
return &Scheduler{}
|
||||
}
|
||||
37
backend/apps/utils.go
Normal file
37
backend/apps/utils.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package apps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/apex/log"
|
||||
"github.com/crawlab-team/go-trace"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func Start(app App) {
|
||||
start(app)
|
||||
}
|
||||
|
||||
func start(app App) {
|
||||
app.Init()
|
||||
go app.Run()
|
||||
waitForStop()
|
||||
app.Stop()
|
||||
}
|
||||
|
||||
func waitForStop() {
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
}
|
||||
|
||||
func initModule(name string, fn func() error) (err error) {
|
||||
if err := fn(); err != nil {
|
||||
log.Error(fmt.Sprintf("init %s error: %s", name, err.Error()))
|
||||
_ = trace.TraceError(err)
|
||||
panic(err)
|
||||
}
|
||||
log.Info(fmt.Sprintf("initialized %s successfully", name))
|
||||
return nil
|
||||
}
|
||||
28
backend/apps/worker.go
Normal file
28
backend/apps/worker.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package apps
|
||||
|
||||
import (
|
||||
"github.com/apex/log"
|
||||
"github.com/crawlab-team/crawlab-core/grpc"
|
||||
)
|
||||
|
||||
type Worker struct {
|
||||
handler *Handler
|
||||
}
|
||||
|
||||
func (app *Worker) Init() {
|
||||
_ = initModule("grpc", grpc.InitGrpcServices)
|
||||
}
|
||||
|
||||
func (app *Worker) Run() {
|
||||
log.Info("worker has started")
|
||||
}
|
||||
|
||||
func (app *Worker) Stop() {
|
||||
log.Info("worker has stopped")
|
||||
}
|
||||
|
||||
func NewWorker() *Worker {
|
||||
return &Worker{
|
||||
handler: NewHandler(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user