-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
2,051 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package apps | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/apex/log" | ||
"github.com/crawlab-team/crawlab-core/controllers" | ||
"github.com/crawlab-team/crawlab-core/interfaces" | ||
"github.com/crawlab-team/crawlab-core/middlewares" | ||
"github.com/gin-gonic/gin" | ||
"github.com/spf13/viper" | ||
"net" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func init() { | ||
// set gin mode | ||
if viper.GetString("gin.mode") == "" { | ||
gin.SetMode(gin.ReleaseMode) | ||
} else { | ||
gin.SetMode(viper.GetString("gin.mode")) | ||
} | ||
} | ||
|
||
type ApiV2 struct { | ||
// dependencies | ||
interfaces.WithConfigPath | ||
|
||
// internals | ||
app *gin.Engine | ||
ln net.Listener | ||
srv *http.Server | ||
ready bool | ||
} | ||
|
||
func (app *ApiV2) Init() { | ||
// initialize middlewares | ||
_ = app.initModuleWithApp("middlewares", middlewares.InitMiddlewares) | ||
|
||
// initialize routes | ||
_ = app.initModuleWithApp("routes", controllers.InitRoutes) | ||
} | ||
|
||
func (app *ApiV2) Start() { | ||
// address | ||
host := viper.GetString("server.host") | ||
port := viper.GetString("server.port") | ||
address := net.JoinHostPort(host, port) | ||
|
||
// http server | ||
app.srv = &http.Server{ | ||
Handler: app.app, | ||
Addr: address, | ||
} | ||
|
||
// listen | ||
var err error | ||
app.ln, err = net.Listen("tcp", address) | ||
if err != nil { | ||
panic(err) | ||
} | ||
app.ready = true | ||
|
||
// serve | ||
if err := http.Serve(app.ln, app.app); err != nil { | ||
if !errors.Is(err, http.ErrServerClosed) { | ||
log.Error("run server error:" + err.Error()) | ||
} else { | ||
log.Info("server graceful down") | ||
} | ||
} | ||
} | ||
|
||
func (app *ApiV2) Wait() { | ||
DefaultWait() | ||
} | ||
|
||
func (app *ApiV2) Stop() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
|
||
if err := app.srv.Shutdown(ctx); err != nil { | ||
log.Error("run server error:" + err.Error()) | ||
} | ||
} | ||
|
||
func (app *ApiV2) GetGinEngine() *gin.Engine { | ||
return app.app | ||
} | ||
|
||
func (app *ApiV2) GetHttpServer() *http.Server { | ||
return app.srv | ||
} | ||
|
||
func (app *ApiV2) Ready() (ok bool) { | ||
return app.ready | ||
} | ||
|
||
func (app *ApiV2) initModuleWithApp(name string, fn func(app *gin.Engine) error) (err error) { | ||
return initModule(name, func() error { | ||
return fn(app.app) | ||
}) | ||
} | ||
|
||
func NewApiV2() *ApiV2 { | ||
api := &ApiV2{ | ||
app: gin.New(), | ||
} | ||
api.Init() | ||
return api | ||
} | ||
|
||
var apiV2 *ApiV2 | ||
|
||
func GetApiV2() *ApiV2 { | ||
if apiV2 != nil { | ||
return apiV2 | ||
} | ||
apiV2 = NewApiV2() | ||
return apiV2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package apps | ||
|
||
import ( | ||
"fmt" | ||
"github.com/apex/log" | ||
"github.com/crawlab-team/crawlab-core/config" | ||
"github.com/crawlab-team/crawlab-core/interfaces" | ||
"github.com/crawlab-team/crawlab-core/node/service" | ||
"github.com/crawlab-team/crawlab-core/utils" | ||
"github.com/spf13/viper" | ||
"net/http" | ||
_ "net/http/pprof" | ||
) | ||
|
||
func init() { | ||
injectModules() | ||
} | ||
|
||
type ServerV2 struct { | ||
// settings | ||
grpcAddress interfaces.Address | ||
|
||
// dependencies | ||
interfaces.WithConfigPath | ||
|
||
// modules | ||
nodeSvc interfaces.NodeService | ||
api *ApiV2 | ||
dck *Docker | ||
|
||
// internals | ||
quit chan int | ||
} | ||
|
||
func (app *ServerV2) Init() { | ||
// log node info | ||
app.logNodeInfo() | ||
|
||
// pprof | ||
app.initPprof() | ||
} | ||
|
||
func (app *ServerV2) Start() { | ||
if utils.IsMaster() { | ||
// start docker app | ||
if utils.IsDocker() { | ||
go app.dck.Start() | ||
} | ||
|
||
// start api | ||
go app.api.Start() | ||
} | ||
|
||
// start node service | ||
go app.nodeSvc.Start() | ||
} | ||
|
||
func (app *ServerV2) Wait() { | ||
<-app.quit | ||
} | ||
|
||
func (app *ServerV2) Stop() { | ||
app.api.Stop() | ||
app.quit <- 1 | ||
} | ||
|
||
func (app *ServerV2) logNodeInfo() { | ||
log.Infof("current node type: %s", utils.GetNodeType()) | ||
if utils.IsDocker() { | ||
log.Infof("running in docker container") | ||
} | ||
} | ||
|
||
func (app *ServerV2) initPprof() { | ||
if viper.GetBool("pprof") { | ||
go func() { | ||
fmt.Println(http.ListenAndServe("0.0.0.0:6060", nil)) | ||
}() | ||
} | ||
} | ||
|
||
func NewServerV2() (app NodeApp) { | ||
// server | ||
svr := &ServerV2{ | ||
WithConfigPath: config.NewConfigPathService(), | ||
quit: make(chan int, 1), | ||
} | ||
|
||
// master modules | ||
if utils.IsMaster() { | ||
// api | ||
svr.api = GetApiV2() | ||
|
||
// docker | ||
if utils.IsDocker() { | ||
svr.dck = GetDocker(WithDockerParent(svr)) | ||
} | ||
} | ||
|
||
// node service | ||
var err error | ||
if utils.IsMaster() { | ||
svr.nodeSvc, err = service.NewMasterServiceV2() | ||
} else { | ||
svr.nodeSvc, err = service.NewWorkerServiceV2() | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return svr | ||
} | ||
|
||
var serverV2 NodeApp | ||
|
||
func GetServerV2() NodeApp { | ||
if serverV2 != nil { | ||
return serverV2 | ||
} | ||
serverV2 = NewServerV2() | ||
return serverV2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.