commit b0fbce962a82fbe4023caa6eb07ae1be9eba9bff
parent 043839faeeb66403d747fdd405ad07a2a016ac56
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Sat, 31 Aug 2024 13:48:03 -0700
Add bind to (-b) option (and fix -s flag)
The bind option allows you to set the address the server binds to,
or if not given tells the server to use the FastCGI protocol on
stdin.
Diffstat:
2 files changed, 9 insertions(+), 11 deletions(-)
diff --git a/cmd/api/Makefile b/cmd/api/Makefile
@@ -35,8 +35,8 @@ run:
-p ${srvroot} ${srvdir}/${prog} < api.config
test-run: ${name}
- go run . localhost:8888
-
+ ./${prog} -s -b localhost:8080 < api.config
+
clean:
rm -f ${name}
diff --git a/cmd/api/main.go b/cmd/api/main.go
@@ -26,8 +26,11 @@ const configFile = "/etc/spaceplanner.app/api.config"
func main() {
var config Config
var useStdin bool
+ var bindTo string
flag.BoolVar(&useStdin, "s", false, "Read config from stdin")
+ flag.StringVar(&bindTo, "b", "", "Bind address and port")
+ flag.Parse()
var f io.Reader
if useStdin {
@@ -36,7 +39,7 @@ func main() {
var err error
f, err = os.Open(configFile)
if err != nil {
- log.Fatalf("%s: %s", configFile, err.Error())
+ log.Fatalf("%s: Could not load configuration: %s", configFile, err.Error())
}
}
if err := json.NewDecoder(f).Decode(&config); err != nil {
@@ -70,15 +73,10 @@ func main() {
engine.Use(AuthMiddleware(e.Auth))
setRoutes(e, engine.Group("/v0"))
- args := os.Args[1:]
- if len(args) > 1 {
- log.Fatal("Too many arguments")
- }
-
- if len(args) == 0 {
- fcgi.Serve(nil, engine)
+ if len(bindTo) > 0 {
+ engine.Run(bindTo)
} else {
- engine.Run(args[0])
+ fcgi.Serve(nil, engine)
}
}