1Panel/backend/server/server.go

87 lines
2.2 KiB
Go
Raw Normal View History

2022-08-16 15:30:23 +00:00
package server
import (
"crypto/tls"
2022-08-16 15:30:23 +00:00
"encoding/gob"
"fmt"
"net"
"net/http"
"os"
"path"
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"
"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
"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"
"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/gin-gonic/gin"
)
func Start() {
viper.Init()
log.Init()
app.Init()
2022-08-16 15:30:23 +00:00
db.Init()
migration.Init()
validator.Init()
gob.Register(psession.SessionUser{})
cache.Init()
session.Init()
gin.SetMode("debug")
2022-09-08 03:39:14 +00:00
cron.Run()
2022-11-23 09:44:24 +00:00
business.Init()
hook.Init()
2022-08-17 07:01:56 +00:00
rootRouter := router.Routers()
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,
}
if global.CONF.System.SSL == "enable" {
certificate, err := os.ReadFile(path.Join(global.CONF.System.BaseDir, "1panel/secret/server.crt"))
if err != nil {
panic(err)
}
key, err := os.ReadFile(path.Join(global.CONF.System.BaseDir, "1panel/secret/server.key"))
if err != nil {
panic(err)
}
cert, err := tls.X509KeyPair(certificate, key)
if err != nil {
panic(err)
}
server.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
}
global.LOG.Infof("listen at %s:%s [%s]", global.CONF.System.BindAddress, global.CONF.System.Port, tcpItem)
ln, err := net.Listen(tcpItem, global.CONF.System.BindAddress+":"+global.CONF.System.Port)
if err != nil {
panic(err)
}
type tcpKeepAliveListener struct {
*net.TCPListener
}
if err := server.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}); err != nil {
panic(err)
}
2022-08-16 15:30:23 +00:00
}