Seed from go-template-repo, renamed to simplexcalc
The template's files at a77fd30, without its history or LICENSE, after script/rename simplexcalc. Model: opus-5-5
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// Command simplexcalc is the service entrypoint.
|
||||
//
|
||||
// Seeding a new project: script/rename renames this directory, the
|
||||
// module path and the binary. Nothing here needs editing by hand.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"sneak.berlin/go/simplexcalc/internal/globals"
|
||||
)
|
||||
|
||||
// appname is the name the service reports in logs, metrics and the
|
||||
// healthcheck.
|
||||
const appname = "simplexcalc"
|
||||
|
||||
// version is injected at build time with -ldflags "-X main.version=...".
|
||||
// The Dockerfile passes VERSION; a `go build` without it says "dev",
|
||||
// which is exactly what a locally built binary is. It is a package
|
||||
// variable because the linker has no other way to reach it.
|
||||
var version = "dev"
|
||||
|
||||
func main() {
|
||||
globals.Appname = appname
|
||||
globals.Version = version
|
||||
globals.Buildarch = runtime.GOARCH
|
||||
|
||||
err := rootCmd().Execute()
|
||||
if err != nil {
|
||||
// cobra has already printed the error; this only sets the exit
|
||||
// status, which is what a supervisor and a shell script read.
|
||||
fmt.Fprintln(os.Stderr, "exiting: "+err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// rootCmd builds the command tree. Configuration comes from the
|
||||
// environment (see internal/config), not from flags: the service runs
|
||||
// in containers, where the environment is the interface, and one source
|
||||
// of truth means there is no precedence rule to get wrong.
|
||||
func rootCmd() *cobra.Command {
|
||||
root := &cobra.Command{
|
||||
Use: appname,
|
||||
Short: appname + " — an HTTP service",
|
||||
Long: appname + " is an HTTP service.\n\n" +
|
||||
"Configuration is read from the environment and from an\n" +
|
||||
"optional .env file in the working directory. See README.md\n" +
|
||||
"for the full list of variables.",
|
||||
SilenceUsage: true,
|
||||
// Without this, cobra prints the error itself and main prints
|
||||
// it again.
|
||||
SilenceErrors: true,
|
||||
}
|
||||
|
||||
root.AddCommand(serveCmd(), versionCmd())
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
func versionCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "print the version and exit",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
_, err := fmt.Fprintf(cmd.OutOrStdout(), "%s %s %s/%s\n",
|
||||
appname, version, runtime.GOOS, runtime.GOARCH)
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing version: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/fx"
|
||||
"sneak.berlin/go/simplexcalc/internal/app"
|
||||
)
|
||||
|
||||
// startTimeout bounds startup: opening the database, applying
|
||||
// migrations and binding the port. A start that hangs past this is a
|
||||
// start that failed, and saying so beats waiting forever.
|
||||
const startTimeout = 30 * time.Second
|
||||
|
||||
// stopTimeout bounds the whole stop sequence. The server's own drain is
|
||||
// bounded by SHUTDOWN_GRACE within this; the margin covers closing the
|
||||
// database and flushing Sentry after the drain finishes.
|
||||
const stopTimeout = 45 * time.Second
|
||||
|
||||
func serveCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "serve",
|
||||
Short: "run the HTTP server",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
return serve(cmd.Context())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// serve builds the graph and runs until a signal arrives.
|
||||
//
|
||||
// fx.App.Run handles SIGINT and SIGTERM and then runs the stop
|
||||
// sequence, which is why nothing here installs a signal handler: two
|
||||
// handlers for the same signal is how a shutdown ends up half done.
|
||||
func serve(ctx context.Context) error {
|
||||
fxApp := app.New(
|
||||
fx.StartTimeout(startTimeout),
|
||||
fx.StopTimeout(stopTimeout),
|
||||
)
|
||||
|
||||
// A construction error is reported here rather than by Run, which
|
||||
// would exit the process itself and give the caller nothing to
|
||||
// report. Configuration failures arrive through this path.
|
||||
err := fxApp.Err()
|
||||
if err != nil {
|
||||
return fmt.Errorf("building application: %w", err)
|
||||
}
|
||||
|
||||
startCtx, cancel := context.WithTimeout(ctx, startTimeout)
|
||||
defer cancel()
|
||||
|
||||
err = fxApp.Start(startCtx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("starting application: %w", err)
|
||||
}
|
||||
|
||||
// Block until a signal. fx closes this channel after it receives
|
||||
// one; the stop sequence is ours to run.
|
||||
<-fxApp.Wait()
|
||||
|
||||
// Deliberately NOT derived from ctx: by the time this runs, the
|
||||
// signal that ends the process has already cancelled it, and a
|
||||
// stop context that is cancelled from the outset gives the drain
|
||||
// no time at all — which is the abrupt termination the graceful
|
||||
// shutdown exists to avoid.
|
||||
stopCtx, stopCancel := context.WithTimeout(context.Background(), stopTimeout)
|
||||
defer stopCancel()
|
||||
|
||||
//nolint:contextcheck // see above: the fresh context is the point.
|
||||
err = fxApp.Stop(stopCtx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("stopping application: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user