gohttp/gohttp.go

62 lines
1.2 KiB
Go

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"
"git.pyer.club/kingecg/gologger"
)
type GoHttp struct {
conf model.GoHttpdConfig
logger gologger.Logger
}
func (g *GoHttp) Start() {
g.logger.Info("start gohttpd")
// if g.conf != nil {
port := model.DefaultAdminConfig.Port
if g.conf.Admin != nil {
port = g.conf.Admin.Port
}
g.makeServer("admin", port, admin.AdminServerMux)
//TODO: create other server
// }
for _, listener := range server.ServerManager {
listener.Start()
listener.Serve()
}
g.logger.Info("gohttpd start success")
}
func (g *GoHttp) makeServer(name string, port int, mux *server.RestMux) {
s := &http.Server{
Handler: mux,
}
server.AddServer(name, s, port)
}
func (g *GoHttp) Stop() {}
func (g *GoHttp) LoadConfig(configPath string) {
cpath := configPath
if cpath == "" {
cpath = GetExecDir() + "/config.json"
}
// read content from cpath
content, _ := os.ReadFile(cpath)
json.Unmarshal(content, &g.conf)
gologger.Configure(g.conf.Logging)
g.logger = gologger.GetLogger("Server")
g.logger.Info("Load config success")
}