generated from templates/go-generic
Dan Anglin
4fdad2d805
- 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.
55 lines
1.2 KiB
Go
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
|
|
}
|