2023-12-08 22:02:36 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.pyer.club/kingecg/gohttpd/admin"
|
|
|
|
"git.pyer.club/kingecg/gohttpd/model"
|
|
|
|
"git.pyer.club/kingecg/gohttpd/server"
|
2023-12-13 17:59:14 +08:00
|
|
|
"git.pyer.club/kingecg/gohttpd/utils"
|
2023-12-08 22:02:36 +08:00
|
|
|
"git.pyer.club/kingecg/gologger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GoHttp struct {
|
|
|
|
logger gologger.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GoHttp) Start() {
|
2023-12-11 18:15:29 +08:00
|
|
|
conf := model.GetConfig()
|
2023-12-10 19:26:09 +08:00
|
|
|
g.logger = gologger.GetLogger("Server")
|
2023-12-08 22:02:36 +08:00
|
|
|
g.logger.Info("start gohttpd")
|
|
|
|
// if g.conf != nil {
|
2023-12-14 23:47:04 +08:00
|
|
|
adminHandler := server.NewServeMux(conf.Admin)
|
|
|
|
adminHandler.Handle("/api/", http.StripPrefix("/api", admin.AdminServerMux))
|
2023-12-13 10:59:47 +08:00
|
|
|
g.makeServer(conf.Admin, adminHandler)
|
2023-12-08 22:02:36 +08:00
|
|
|
|
2023-12-14 23:47:04 +08:00
|
|
|
for _, s := range conf.Servers {
|
|
|
|
sHandler := server.NewServeMux(s)
|
|
|
|
g.makeServer(s, sHandler)
|
2023-12-09 17:08:31 +08:00
|
|
|
}
|
2023-12-08 22:02:36 +08:00
|
|
|
|
|
|
|
for _, listener := range server.ServerManager {
|
|
|
|
listener.Start()
|
|
|
|
listener.Serve()
|
|
|
|
|
|
|
|
}
|
|
|
|
g.logger.Info("gohttpd start success")
|
|
|
|
}
|
|
|
|
|
2023-12-13 09:22:05 +08:00
|
|
|
func (g *GoHttp) makeServer(conf *model.HttpServerConfig, mux http.Handler) {
|
2023-12-08 22:02:36 +08:00
|
|
|
s := &http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
2023-12-13 09:22:05 +08:00
|
|
|
name := conf.Name
|
|
|
|
port := conf.Port
|
|
|
|
ss := &server.Server{
|
|
|
|
Server: s,
|
|
|
|
Conf: conf,
|
|
|
|
}
|
|
|
|
server.AddServer(name, ss, port)
|
2023-12-08 22:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GoHttp) Stop() {}
|
|
|
|
|
2023-12-13 17:59:14 +08:00
|
|
|
func LoadConfig() {
|
|
|
|
cpath := utils.NormalizePath("./config.json")
|
2023-12-08 22:02:36 +08:00
|
|
|
|
|
|
|
// read content from cpath
|
|
|
|
content, _ := os.ReadFile(cpath)
|
2023-12-11 18:15:29 +08:00
|
|
|
json.Unmarshal(content, &model.Config)
|
2023-12-13 09:22:05 +08:00
|
|
|
//normalize path in config
|
|
|
|
for _, appender := range model.Config.Logging.Appenders {
|
|
|
|
if appender.Type == "file" {
|
2023-12-13 17:59:14 +08:00
|
|
|
appender.Options["file"] = utils.NormalizePath(appender.Options["file"].(string))
|
2023-12-13 09:22:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
normalizeServer(model.Config.Admin)
|
|
|
|
for _, server := range model.Config.Servers {
|
|
|
|
normalizeServer(server)
|
|
|
|
}
|
2023-12-11 18:15:29 +08:00
|
|
|
gologger.Configure(model.Config.Logging)
|
2023-12-10 19:26:09 +08:00
|
|
|
logger := gologger.GetLogger("Server")
|
|
|
|
logger.Info("Load config success")
|
2023-12-08 22:02:36 +08:00
|
|
|
}
|
2023-12-13 09:22:05 +08:00
|
|
|
|
|
|
|
func normalizeServer(server *model.HttpServerConfig) {
|
2023-12-13 10:59:47 +08:00
|
|
|
for index, path := range server.Paths {
|
2023-12-13 09:22:05 +08:00
|
|
|
if path.Root != "" {
|
2023-12-13 21:09:01 +08:00
|
|
|
server.Paths[index].Root = utils.NormalizePath(path.Root)
|
2023-12-13 09:22:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if server.CertFile != "" {
|
2023-12-13 21:09:01 +08:00
|
|
|
server.CertFile = utils.NormalizePath(server.CertFile)
|
2023-12-13 09:22:05 +08:00
|
|
|
}
|
|
|
|
if server.KeyFile != "" {
|
2023-12-13 21:09:01 +08:00
|
|
|
server.KeyFile = utils.NormalizePath(server.KeyFile)
|
2023-12-13 09:22:05 +08:00
|
|
|
}
|
|
|
|
}
|