ctrl/run.go
2024-07-21 12:40:28 +02:00

94 lines
1.8 KiB
Go

package main
import (
"flag"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
)
var (
port = 8081
host = "localhost"
)
const tmplDir = "templates"
func run() {
log.Println("starting ctrl")
flag.Parse()
c := readConfig(*configFlag)
if c.HTTP.Port > 0 {
port = c.HTTP.Port
}
log.Println("port:", port)
if c.HTTP.Host != "" {
host = c.HTTP.Host
}
log.Println("host:", host)
if len(c.Limits.RemoteCmds) != 0 {
log.Println("available cmmands:", c.Limits.RemoteCmds)
} else {
log.Println("no remote commands configured")
}
if len(c.Limits.WolHosts) != 0 {
log.Println("configured WoL hosts:")
for _, w := range c.Limits.WolHosts {
log.Println("\tname:", w.Name, "mac:", w.MAC)
}
} else {
log.Println("no WoL hosts configured")
}
log.Println("ssh key path:", c.SSH.SSHKeyPath)
log.Println("ssh key passphrase path:", c.SSH.SSHKeyPassphrasePath)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" && r.Method != "POST" {
http.Error(w, "method is not supported.", http.StatusNotFound)
return
}
http.ServeFile(w, r, r.URL.Path[1:])
})
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Println("starting server on host:", host, "port:", port)
go func() {
if err := http.ListenAndServe(strings.Join([]string{host, strconv.Itoa(port)}, ":"), nil); err != nil {
log.Println("could not start server on port", port, "error", err)
done <- nil
}
}()
log.Println("started server on port", port)
<-done
log.Println("stopping HTTP server")
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
// defer func() { cancel() }()
// if err := h.Shutdown(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
// log.Println("could not stop server", "error", err)
// }
}