indieauth-server/internal/executors/serve.go
Dan Anglin 4fdad2d805
All checks were successful
CI / Tests (pull_request) Successful in 9s
CI / Style (pull_request) Successful in 9s
feat: project setup
- Set the project's placeholder name to indieauth-server.
- Update the magefiles and CI workflow.
- Add the version command to print the app's build information.
- Add the serve command to run the HTTP server.
2024-10-13 15:36:54 +01:00

55 lines
1.2 KiB
Go

package executors
import (
"flag"
"fmt"
"log/slog"
"net/http"
"time"
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/info"
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/router"
)
type serveExecutor struct {
*flag.FlagSet
address string
}
func executeServeCommand(args []string) error {
executorName := "serve"
executor := serveExecutor{
FlagSet: flag.NewFlagSet(executorName, flag.ExitOnError),
}
executor.StringVar(&executor.address, "address", "0.0.0.0:8080", "The address that the server will listen on")
if err := executor.Parse(args); err != nil {
return fmt.Errorf("(%s) flag parsing error: %w", executorName, err)
}
if err := executor.execute(); err != nil {
return fmt.Errorf("(%s) execution error: %w", executorName, err)
}
return nil
}
func (e *serveExecutor) execute() error {
server := http.Server{
Addr: e.address,
Handler: router.NewServeMux(),
ReadHeaderTimeout: 1 * time.Second,
}
slog.Info(info.ApplicationName+" is listening for web requests", "address", e.address)
err := server.ListenAndServe()
if err != nil {
return fmt.Errorf("error running the server: %w", err)
}
return nil
}