2022-08-16 15:30:23 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-04-25 06:34:16 +00:00
|
|
|
"crypto/tls"
|
2022-08-16 15:30:23 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
2023-04-25 06:34:16 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-07-27 07:52:25 +00:00
|
|
|
"path"
|
2023-01-09 14:55:10 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-21 03:27:56 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/app"
|
2022-11-23 09:44:24 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/business"
|
2022-08-23 07:21:08 +00:00
|
|
|
|
2022-10-17 08:32:31 +00: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 15:30:23 +00:00
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/db"
|
2023-02-02 07:01:37 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/init/hook"
|
2022-10-17 08:32:31 +00: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 15:30:23 +00:00
|
|
|
|
|
|
|
"github.com/fvbock/endless"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Start() {
|
|
|
|
viper.Init()
|
|
|
|
log.Init()
|
2023-03-07 06:16:26 +00:00
|
|
|
app.Init()
|
2022-08-16 15:30:23 +00:00
|
|
|
db.Init()
|
|
|
|
migration.Init()
|
|
|
|
validator.Init()
|
|
|
|
gob.Register(psession.SessionUser{})
|
|
|
|
cache.Init()
|
|
|
|
session.Init()
|
2023-01-09 14:55:10 +00:00
|
|
|
gin.SetMode("debug")
|
2022-09-08 03:39:14 +00:00
|
|
|
cron.Run()
|
2022-11-23 09:44:24 +00:00
|
|
|
business.Init()
|
2023-07-05 08:10:19 +00:00
|
|
|
hook.Init()
|
2022-08-17 07:01:56 +00:00
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
rootRouter := router.Routers()
|
2023-02-02 07:01:37 +00:00
|
|
|
address := fmt.Sprintf(":%s", global.CONF.System.Port)
|
2023-04-25 06:34:16 +00:00
|
|
|
s := endless.NewServer(address, rootRouter)
|
2022-08-16 15:30:23 +00:00
|
|
|
s.ReadHeaderTimeout = 20 * time.Second
|
2022-11-17 09:39:33 +00:00
|
|
|
s.WriteTimeout = 60 * time.Second
|
2022-08-16 15:30:23 +00:00
|
|
|
s.MaxHeaderBytes = 1 << 20
|
2023-04-25 06:34:16 +00:00
|
|
|
|
|
|
|
if global.CONF.System.SSL == "disable" {
|
|
|
|
global.LOG.Infof("server run success on %s with http", global.CONF.System.Port)
|
|
|
|
if err := s.ListenAndServe(); err != nil {
|
|
|
|
global.LOG.Error(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-27 07:52:25 +00:00
|
|
|
certificate, err := os.ReadFile(path.Join(global.CONF.System.BaseDir, "1panel/secret/server.crt"))
|
2023-04-25 06:34:16 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-07-27 07:52:25 +00:00
|
|
|
key, err := os.ReadFile(path.Join(global.CONF.System.BaseDir, "1panel/secret/server.key"))
|
2023-04-25 06:34:16 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cert, err := tls.X509KeyPair(certificate, key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: address,
|
|
|
|
Handler: rootRouter,
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
global.LOG.Infof("server run success on %s with https", global.CONF.System.Port)
|
|
|
|
if err := s.ListenAndServeTLS("", ""); err != nil {
|
|
|
|
global.LOG.Error(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 15:30:23 +00:00
|
|
|
}
|