Compare commits

..

No commits in common. "3c7b964cfe1cd78022badcb3dc22834d0023fded" and "b3b810a20f7c59c452d0c983a6b7cd8c4d7714ae" have entirely different histories.

4 changed files with 22 additions and 31 deletions

View File

@ -3,14 +3,12 @@ package main
import "io"
import "os"
import "log"
import "time"
import "hnakra/rcon"
import "hnakra/rotate"
import "hnakra/config"
import "hnakra/router"
import "hnakra/routines"
import "hnakra/router/rcon"
import "hnakra/router/config"
import "hnakra/cmd/router/srvhttps"
import "hnakra/cmd/router/srvhnakra"
import "hnakra/srvhttps"
import "hnakra/srvhnakra"
const banner = "\n" +
" -=\\\n" +
@ -23,7 +21,7 @@ const banner = "\n" +
" '/"
func main () {
// set up logging
// setup logging
logDir := os.Getenv("HNAKRA_LOG_DIR")
if logDir != "" {
logger, err := rotate.New(logDir)
@ -33,6 +31,7 @@ func main () {
rcon := rcon.New("/debug/rcon")
originalWriter := log.Writer()
log.SetOutput(io.MultiWriter(originalWriter, rcon))
log.Println(banner)
// load config
@ -54,18 +53,10 @@ func main () {
})
rcon.SetConfig(conf)
// set up servers
// start servers
log.Println("... starting up")
manager := routines.Manager { RestartDeadline: time.Second * 8 }
rout := router.New(conf)
srvhnakra := &srvhnakra.Server { Config: conf, Router: rout }
manager.Append(srvhnakra.ListenAndServe)
if conf.HTTPSEnable() {
srvhttps := &srvhttps.Server { Config: conf, Handler: rout }
manager.Append(srvhttps.ListenAndServe)
}
// set up rcon
rcon.SetRouter(rout)
if conf.RconEnable() {
err = rout.HTTPMux().Handle("@/debug/rcon/", rcon)
@ -73,9 +64,12 @@ func main () {
} else {
log.SetOutput(originalWriter)
}
if conf.HTTPSEnable() {
srvhttps := &srvhttps.Server { Config: conf, Handler: rout }
go httpsRoutine(srvhttps)
}
// run servers
err = manager.Run()
err = srvhnakra.ListenAndServe()
if err != nil { log.Println("XXX", err) }
}

View File

@ -5,7 +5,7 @@ import "fmt"
import "net"
import "crypto/tls"
import "hnakra/router"
import "hnakra/router/config"
import "hnakra/config"
type Server struct {
underlying net.Listener

View File

@ -3,7 +3,7 @@ package srvhttps
import "fmt"
import "log"
import "net/http"
import "hnakra/router/config"
import "hnakra/config"
type Server struct {
underlying *http.Server

View File

@ -6,9 +6,12 @@ import "log"
import "time"
import "sync"
// Routine is a long-running function that does not return until it is finished.
// An error is returned if the routine exited due to an error.
type Routine func () error
// Routine represents an object that can be run.
type Routine interface {
// Run starts the routine and does not return until it is finished. An
// error is returned if the routine exited due to an error.
Run () error
}
// Manager is a system capable of managing multiple routines, and restarting
// them if they fail.
@ -46,12 +49,6 @@ func (manager *Manager) Run () error {
return errExit
}
// Append adds one or more routines to the Routines slice. This has no effect if
// the manager is already running.
func (manager *Manager) Append (routines ...Routine) {
manager.Routines = append(manager.Routines, routines...)
}
func (manager *Manager) log (message ...any) {
if manager.Logger == nil {
log.Println(message...)
@ -67,7 +64,7 @@ func (manager *Manager) runRoutine (routine Routine, group *sync.WaitGroup, errE
for {
// TODO: recover from panics
lastStart := time.Now()
err = routine()
err = routine.Run()
if err == nil {
manager.log("(i) routine exited")