Compare commits

...

3 Commits

Author SHA1 Message Date
Sasha Koshka 3c7b964cfe Use routine.Manager in router 2023-05-26 13:07:05 -04:00
Sasha Koshka 23f7497622 Routine manager now manages functions 2023-05-26 13:06:21 -04:00
Sasha Koshka b17bad32fe Changed some bad import paths 2023-05-26 13:05:22 -04:00
4 changed files with 31 additions and 22 deletions

View File

@ -3,12 +3,14 @@ package main
import "io"
import "os"
import "log"
import "hnakra/rcon"
import "time"
import "hnakra/rotate"
import "hnakra/config"
import "hnakra/router"
import "hnakra/srvhttps"
import "hnakra/srvhnakra"
import "hnakra/routines"
import "hnakra/router/rcon"
import "hnakra/router/config"
import "hnakra/cmd/router/srvhttps"
import "hnakra/cmd/router/srvhnakra"
const banner = "\n" +
" -=\\\n" +
@ -21,7 +23,7 @@ const banner = "\n" +
" '/"
func main () {
// setup logging
// set up logging
logDir := os.Getenv("HNAKRA_LOG_DIR")
if logDir != "" {
logger, err := rotate.New(logDir)
@ -31,7 +33,6 @@ func main () {
rcon := rcon.New("/debug/rcon")
originalWriter := log.Writer()
log.SetOutput(io.MultiWriter(originalWriter, rcon))
log.Println(banner)
// load config
@ -53,10 +54,18 @@ func main () {
})
rcon.SetConfig(conf)
// start servers
// set up 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)
@ -64,12 +73,9 @@ func main () {
} else {
log.SetOutput(originalWriter)
}
if conf.HTTPSEnable() {
srvhttps := &srvhttps.Server { Config: conf, Handler: rout }
go httpsRoutine(srvhttps)
}
err = srvhnakra.ListenAndServe()
// run servers
err = manager.Run()
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/config"
import "hnakra/router/config"
type Server struct {
underlying net.Listener

View File

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

View File

@ -6,12 +6,9 @@ import "log"
import "time"
import "sync"
// 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
}
// 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
// Manager is a system capable of managing multiple routines, and restarting
// them if they fail.
@ -49,6 +46,12 @@ 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...)
@ -64,7 +67,7 @@ func (manager *Manager) runRoutine (routine Routine, group *sync.WaitGroup, errE
for {
// TODO: recover from panics
lastStart := time.Now()
err = routine.Run()
err = routine()
if err == nil {
manager.log("(i) routine exited")