Compare commits
3 Commits
b3b810a20f
...
3c7b964cfe
Author | SHA1 | Date |
---|---|---|
Sasha Koshka | 3c7b964cfe | |
Sasha Koshka | 23f7497622 | |
Sasha Koshka | b17bad32fe |
|
@ -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) }
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue