2022-08-16 23:30:23 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
2023-01-09 22:55:10 +08:00
|
|
|
"time"
|
|
|
|
|
2022-11-21 11:27:56 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/app"
|
2022-11-23 17:44:24 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/business"
|
2022-08-23 15:21:08 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/cron"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/cache"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/session"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/session/psession"
|
2022-08-16 23:30:23 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/db"
|
2023-02-02 15:01:37 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/hook"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/log"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/migration"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/router"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/validator"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/viper"
|
2022-08-16 23:30:23 +08:00
|
|
|
|
|
|
|
"github.com/fvbock/endless"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Start() {
|
|
|
|
viper.Init()
|
2023-01-30 21:05:20 +08:00
|
|
|
app.Init()
|
2022-08-16 23:30:23 +08:00
|
|
|
log.Init()
|
|
|
|
db.Init()
|
|
|
|
migration.Init()
|
|
|
|
validator.Init()
|
|
|
|
gob.Register(psession.SessionUser{})
|
|
|
|
cache.Init()
|
|
|
|
session.Init()
|
2023-01-09 22:55:10 +08:00
|
|
|
gin.SetMode("debug")
|
2022-09-08 11:39:14 +08:00
|
|
|
cron.Run()
|
2022-11-23 17:44:24 +08:00
|
|
|
business.Init()
|
2023-02-02 15:01:37 +08:00
|
|
|
hook.Init()
|
2022-08-17 15:01:56 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
rootRouter := router.Routers()
|
2023-02-02 15:01:37 +08:00
|
|
|
address := fmt.Sprintf(":%s", global.CONF.System.Port)
|
2022-10-17 16:32:31 +08:00
|
|
|
s := initServer(address, rootRouter)
|
2023-02-02 15:01:37 +08:00
|
|
|
global.LOG.Infof("server run success on %s", global.CONF.System.Port)
|
2022-08-16 23:30:23 +08:00
|
|
|
if err := s.ListenAndServe(); err != nil {
|
|
|
|
global.LOG.Error(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type server interface {
|
|
|
|
ListenAndServe() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func initServer(address string, router *gin.Engine) server {
|
|
|
|
s := endless.NewServer(address, router)
|
|
|
|
s.ReadHeaderTimeout = 20 * time.Second
|
2022-11-17 17:39:33 +08:00
|
|
|
s.WriteTimeout = 60 * time.Second
|
2022-08-16 23:30:23 +08:00
|
|
|
s.MaxHeaderBytes = 1 << 20
|
|
|
|
return s
|
|
|
|
}
|