2022-08-16 23:30:23 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-04-25 14:34:16 +08:00
|
|
|
"crypto/tls"
|
2022-08-16 23:30:23 +08:00
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
2024-02-19 13:55:27 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
2023-11-01 10:48:38 +08:00
|
|
|
"net"
|
2023-04-25 14:34:16 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-07-27 15:52:25 +08:00
|
|
|
"path"
|
2023-01-09 22:55:10 +08:00
|
|
|
|
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/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Start() {
|
|
|
|
viper.Init()
|
2024-02-19 13:55:27 +08:00
|
|
|
i18n.Init()
|
2022-08-16 23:30:23 +08:00
|
|
|
log.Init()
|
|
|
|
db.Init()
|
|
|
|
migration.Init()
|
2023-11-28 15:00:10 +08:00
|
|
|
app.Init()
|
2022-08-16 23:30:23 +08:00
|
|
|
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-07-05 16:10:19 +08:00
|
|
|
hook.Init()
|
2023-12-25 17:38:09 +08:00
|
|
|
InitOthers()
|
2022-08-17 15:01:56 +08:00
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
rootRouter := router.Routers()
|
2023-04-25 14:34:16 +08:00
|
|
|
|
2023-10-25 10:41:26 +08:00
|
|
|
tcpItem := "tcp4"
|
|
|
|
if global.CONF.System.Ipv6 == "enable" {
|
|
|
|
tcpItem = "tcp"
|
|
|
|
global.CONF.System.BindAddress = fmt.Sprintf("[%s]", global.CONF.System.BindAddress)
|
|
|
|
}
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: global.CONF.System.BindAddress + ":" + global.CONF.System.Port,
|
|
|
|
Handler: rootRouter,
|
|
|
|
}
|
2023-11-01 10:48:38 +08:00
|
|
|
ln, err := net.Listen(tcpItem, server.Addr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
type tcpKeepAliveListener struct {
|
|
|
|
*net.TCPListener
|
|
|
|
}
|
2023-10-25 10:41:26 +08:00
|
|
|
if global.CONF.System.SSL == "enable" {
|
2023-11-01 22:25:08 +08:00
|
|
|
certPath := path.Join(global.CONF.System.BaseDir, "1panel/secret/server.crt")
|
|
|
|
keyPath := path.Join(global.CONF.System.BaseDir, "1panel/secret/server.key")
|
|
|
|
certificate, err := os.ReadFile(certPath)
|
2023-04-25 14:34:16 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-11-01 22:25:08 +08:00
|
|
|
key, err := os.ReadFile(keyPath)
|
2023-04-25 14:34:16 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cert, err := tls.X509KeyPair(certificate, key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-10-25 10:41:26 +08:00
|
|
|
server.TLSConfig = &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
2023-04-25 14:34:16 +08:00
|
|
|
}
|
2023-10-31 21:59:39 +08:00
|
|
|
global.LOG.Infof("listen at https://%s:%s [%s]", global.CONF.System.BindAddress, global.CONF.System.Port, tcpItem)
|
2023-11-01 10:48:38 +08:00
|
|
|
|
2023-11-01 22:25:08 +08:00
|
|
|
if err := server.ServeTLS(tcpKeepAliveListener{ln.(*net.TCPListener)}, certPath, keyPath); err != nil {
|
2023-10-31 21:59:39 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-07 10:18:07 +08:00
|
|
|
global.LOG.Infof("listen at http://%s:%s [%s]", global.CONF.System.BindAddress, global.CONF.System.Port, tcpItem)
|
2023-11-01 10:48:38 +08:00
|
|
|
if err := server.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}); err != nil {
|
2023-10-31 21:59:39 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2023-10-25 10:41:26 +08:00
|
|
|
}
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|