yoake/server/server.go

55 lines
976 B
Go
Raw Normal View History

2022-11-07 04:45:02 -06:00
package server
import (
"strings"
2022-11-11 22:58:05 -06:00
"github.com/eternal-flame-AD/yoake/config"
"github.com/eternal-flame-AD/yoake/internal/apparmor"
2022-11-07 04:45:02 -06:00
"github.com/labstack/echo/v4"
)
type (
Host struct {
Echo *echo.Echo
}
)
var Server = echo.New()
var hosts = map[string]*Host{}
func init() {
2022-11-11 22:58:05 -06:00
hatChanged := false
2022-11-07 04:45:02 -06:00
Server.Any("/*", func(c echo.Context) (err error) {
2022-11-11 22:58:05 -06:00
if !hatChanged {
appArmor := config.Config().Listen.AppArmor
if appArmor.Serve != "" {
if key, err := apparmor.GetMagicToken(); err != nil {
return err
} else {
if err := apparmor.ChangeHat(appArmor.Serve, key); err != nil {
return err
}
}
}
hatChanged = true
}
2022-11-07 04:45:02 -06:00
req := c.Request()
res := c.Response()
host := hosts[strings.ToLower(req.Host)]
if host == nil {
host = hosts[""]
if host == nil {
return echo.ErrNotFound
}
}
host.Echo.ServeHTTP(res, req)
return
})
}
func RegisterHostname(hostname string, h *Host) {
hosts[hostname] = h
}