cmd/stepd: Configuration file is read and used for the HTTP server

Closes #7
This commit is contained in:
Sasha Koshka 2024-12-10 00:28:53 -05:00
parent d4d882c40e
commit 11e8e7460a

View File

@ -4,6 +4,7 @@ package main
import "os"
import "log"
import "time"
import "slices"
import "errors"
import "context"
import "net/http"
@ -16,30 +17,30 @@ import "git.tebibyte.media/sashakoshka/go-service/daemon"
import stephttp"git.tebibyte.media/sashakoshka/step/http"
import "git.tebibyte.media/sashakoshka/go-service/routines"
const configFileName = "step.meta"
func main () {
// parse command line arguments
flagPidFile := cli.NewInputFlag (
'p', "pid-file",
"Write the PID to the specified file.",
"", cli.ValString)
flagAddress := cli.NewInputFlag (
'a', "address",
"The address to host the server on.",
flagHTTPAddress := cli.NewInputFlag (
'h', "http-address",
"The address to host the HTTP server on.",
":8080", cli.ValString)
// TODO have in conf, override here
flagErrorDocument := cli.NewInputFlag (
0, "error-document",
"The document to use for displaying errors.",
flagHTTPErrorDocument := cli.NewInputFlag (
0, "http-error-document",
"The document to use for displaying http errors.",
"", cli.ValString)
// TODO have in conf, override here
flagDirectories := cli.NewFlag (
'd', "directories",
"Serve the contents of directories.")
cmd := cli.New (
"Run an HTTP server that automaticaly executes STEP files.",
flagPidFile,
flagAddress,
flagErrorDocument,
flagHTTPAddress,
flagHTTPErrorDocument,
flagDirectories,
cli.NewHelp())
cmd.Syntax = "[OPTIONS]... [DIRECTORY]"
@ -51,6 +52,7 @@ func main () {
// manage start and end of program
ctx, done := context.WithCancel(context.Background())
defer done()
daemon.OnSigint(done)
pidFileAbs, err := filepath.Abs(flagPidFile.Value)
if err != nil { log.Fatalln("XXX", err) }
@ -73,9 +75,36 @@ func main () {
cmd.Usage()
os.Exit(1)
}
// read the config file
var config step.Meta
if configFile, err := os.Open(configFileName); err == nil {
defer configFile.Close()
config, err = step.DecodeMeta(configFile)
configFile.Close()
if err != nil { log.Fatalln("XXX", err) }
} else {
config = make(step.Meta)
log.Printf (
"(i) could not open %s, using default configuration",
configFileName)
}
// override the config file with explicitly specified options
if flagHTTPAddress.Value != "" {
config.Set("http.address", flagHTTPAddress.Value)
}
if flagHTTPErrorDocument.Value != "" {
config.Set("http.error-document", flagHTTPErrorDocument.Value)
}
if flagDirectories.Value != "" {
config.Set("http.serve-directories", flagDirectories.Value)
}
// set up the environment
environment := step.Environment { }
environment := step.Environment {
Config: config,
}
environment.Providers = providers.All()
err = environment.Init(context.Background())
if err != nil { log.Fatal(err) }
@ -83,20 +112,26 @@ func main () {
// set up the HTTP handler
handler := stephttp.Handler {
Environment: &environment,
Directories: flagDirectories.Value == "true",
StepExt: ucontainer.NewSet(".step"),
Index: []string { "index.step", "index.html", "index" },
ErrorDocument: flagErrorDocument.Value,
Directories: config.Get("http.serve-directories") == "true",
StepExt: ucontainer.NewSet(slices.Clone(config["http.step-extension"])...),
Index: slices.Clone(config["http.index-file"]),
ErrorDocument: config.Get("http.error-document"),
}
if len(handler.StepExt) == 0 {
handler.StepExt.Add(".step")
}
if len(handler.Index) == 0 {
handler.Index = []string { "index.step", "index.html", "index" }
}
// set up the HTTP server
httpServer := http.Server {
Addr: flagAddress.Value,
Addr: config.Get("http.address"),
Handler: &handler,
}
httpServerRoutine := httpServerRoutine(httpServer)
// set up the routine manager
httpServerRoutine := httpServerRoutine(httpServer)
manager := routines.Manager {
Routines: []routines.Routine {
&httpServerRoutine,