hnakra/cmd/router/main.go
2023-05-25 18:08:56 -04:00

80 lines
2.1 KiB
Go

package main
import "io"
import "os"
import "log"
import "hnakra/rcon"
import "hnakra/rotate"
import "hnakra/config"
import "hnakra/router"
import "hnakra/srvhttps"
import "hnakra/srvhnakra"
const banner = "\n" +
" -=\\\n" +
"__. __|.\\___\n" +
" - ``.__.-` ```'-. | | |\\ | |\\ | / |''| |\\\n" +
" `-, u `. |--| | \\| |-\\ |< |-< |-\\\n" +
" ,-' ,```` VVv | | | | | \\ | \\ | \\ | \\\n" +
"/___-' ````--//----'''``\n" +
" |/ DISTRIBUTED HOLANET SERVER ROUTER\n" +
" '/"
func main () {
// setup logging
logDir := os.Getenv("HNAKRA_LOG_DIR")
if logDir != "" {
logger, err := rotate.New(logDir)
if err != nil { log.Fatal("cannot access log dir:", err) }
log.SetOutput(logger)
}
rcon := rcon.New("/debug/rcon")
originalWriter := log.Writer()
log.SetOutput(io.MultiWriter(originalWriter, rcon))
log.Println(banner)
// load config
log.Println("... loading config")
conf, err := config.File("/etc/hnakra/hnakra.conf")
if err != nil { log.Fatal(err) }
log.Println("(i) users:")
conf.OverUsers(func (name string, user config.User) bool {
log.Println(" -", name)
return true
})
log.Println("(i) aliases:")
if conf.AliasFallback() != "" {
log.Println(" - (fallback) ->", conf.AliasFallback())
}
conf.OverAliases(func (alias, target string) bool{
log.Println(" -", alias, "->", target)
return true
})
rcon.SetConfig(conf)
// start servers
log.Println("... starting up")
rout := router.New(conf)
srvhnakra := &srvhnakra.Server { Config: conf, Router: rout }
rcon.SetRouter(rout)
if conf.RconEnable() {
err = rout.HTTPMux().Handle("@/debug/rcon/", rcon)
if err != nil { log.Fatalln("XXX", err) }
} else {
log.SetOutput(originalWriter)
}
if conf.HTTPSEnable() {
srvhttps := &srvhttps.Server { Config: conf, Handler: rout }
go httpsRoutine(srvhttps)
}
err = srvhnakra.ListenAndServe()
if err != nil { log.Println("XXX", err) }
}
func httpsRoutine (server *srvhttps.Server) {
err := server.ListenAndServe()
if err != nil { log.Println("XXX", err) }
}