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