-
-
Notifications
You must be signed in to change notification settings - Fork 0
chore: Refactor main startup for idiomatic Go #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gocanto
merged 20 commits into
main
from
gus/refactor-code-to-be-more-idiomatic-go-2025-10-17
Oct 17, 2025
+319
−193
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cb7112c
Refine main entrypoint for idiomatic Go
gocanto 8c8276f
Improve server lifecycle handling
gocanto 5c2d172
Refine CLI and seeder lifecycle
gocanto 8b0dd29
Fail fast when database ping fails
gocanto 5e71dce
Ensure sentry flush happens after captures
gocanto 5cba7a2
Inline executable sentry flush
gocanto 7aeb027
Ensure Sentry flush defer runs early
gocanto dd3a8aa
Flush sentry errors before exiting server
gocanto a13ac59
Handle Ignite load failures
gocanto 93a3728
Keep CLI menu loop interactive
gocanto 60f52ee
Lower Go directive to match available toolchain
gocanto 15692eb
Restore Go 1.25.3 directive
gocanto d27c61c
chore: bump golang alpine digest
gocanto 3ddf814
Adjust compose service definitions
gocanto 3a167dd
Remove custom container names from compose
gocanto 856f0b4
Add runner container name for api-runner service
gocanto a806968
Extract server lifecycle to endpoint package
gocanto cc5d045
Move server handler helper into endpoint package
gocanto 0867eeb
Rename server handler helper
gocanto 5fd8066
Ensure dev CORS origin includes scheme
gocanto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,95 +1,85 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "net/http" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/getsentry/sentry-go" | ||
| _ "github.com/lib/pq" | ||
| "github.com/oullin/metal/kernel" | ||
| "github.com/oullin/pkg/endpoint" | ||
| "github.com/oullin/pkg/portal" | ||
| "github.com/rs/cors" | ||
| ) | ||
|
|
||
| var app *kernel.App | ||
| func main() { | ||
| defer sentry.Flush(2 * time.Second) | ||
|
|
||
| func init() { | ||
| validate := portal.GetDefaultValidator() | ||
| secrets := kernel.Ignite("./.env", validate) | ||
| application, err := kernel.NewApp(secrets, validate) | ||
| if err := run(); err != nil { | ||
| sentry.CurrentHub().CaptureException(err) | ||
| if !sentry.Flush(2 * time.Second) { | ||
| slog.Warn("sentry flush timed out after capture") | ||
| } | ||
| slog.Error("server exited with error", "error", err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func run() error { | ||
| validate := portal.GetDefaultValidator() | ||
| secrets, err := kernel.Ignite("./.env", validate) | ||
| if err != nil { | ||
| panic(fmt.Sprintf("init: Error creating application: %s", err)) | ||
| return fmt.Errorf("ignite environment: %w", err) | ||
| } | ||
|
|
||
| app = application | ||
| } | ||
| app, err := kernel.NewApp(secrets, validate) | ||
| if err != nil { | ||
| return fmt.Errorf("create application: %w", err) | ||
| } | ||
|
|
||
| func main() { | ||
| defer sentry.Flush(2 * time.Second) | ||
| defer app.CloseDB() | ||
| defer app.CloseLogs() | ||
| defer app.Recover() | ||
|
|
||
| app.Boot() | ||
|
|
||
| // --- Testing | ||
| if err := app.GetDB().Ping(); err != nil { | ||
| slog.Error("database ping failed", "error", err) | ||
| return fmt.Errorf("database ping failed: %w", err) | ||
| } | ||
| slog.Info("Starting new server on :" + app.GetEnv().Network.HttpPort) | ||
| // --- | ||
|
|
||
| if err := http.ListenAndServe(app.GetEnv().Network.GetHostURL(), serverHandler()); err != nil { | ||
| sentry.CurrentHub().CaptureException(err) | ||
| slog.Error("Error starting server", "error", err) | ||
| panic("Error starting server." + err.Error()) | ||
| env := app.GetEnv() | ||
| if env == nil { | ||
| return errors.New("application environment is nil") | ||
| } | ||
| } | ||
| addr := env.Network.GetHostURL() | ||
|
|
||
gocanto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func serverHandler() http.Handler { | ||
| mux := app.GetMux() | ||
| if mux == nil { | ||
| return http.NotFoundHandler() | ||
| var wrap func(http.Handler) http.Handler | ||
| if sentry := app.GetSentry(); sentry != nil && sentry.Handler != nil { | ||
| wrap = sentry.Handler.Handle | ||
| } | ||
|
|
||
| var handler http.Handler = mux | ||
|
|
||
| if !app.IsProduction() { // Caddy handles CORS. | ||
| localhost := app.GetEnv().Network.GetHostURL() | ||
|
|
||
| headers := []string{ | ||
| "Accept", | ||
| "Authorization", | ||
| "Content-Type", | ||
| "X-CSRF-Token", | ||
| "User-Agent", | ||
| "X-API-Key", | ||
| "X-API-Username", | ||
| "X-API-Signature", | ||
| "X-API-Timestamp", | ||
| "X-API-Nonce", | ||
| "X-Request-ID", | ||
| "If-None-Match", | ||
| "X-API-Intended-Origin", //new | ||
| } | ||
|
|
||
| c := cors.New(cors.Options{ | ||
| AllowedOrigins: []string{localhost, "http://localhost:5173"}, | ||
| AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodOptions}, | ||
| AllowedHeaders: headers, | ||
| AllowCredentials: true, | ||
| Debug: true, | ||
| }) | ||
|
|
||
| handler = c.Handler(handler) | ||
| handler := endpoint.NewServerHandler(endpoint.ServerHandlerConfig{ | ||
| Mux: app.GetMux(), | ||
| IsProduction: app.IsProduction(), | ||
| DevHost: addr, | ||
| Wrap: wrap, | ||
| }) | ||
|
|
||
| server := &http.Server{ | ||
| Addr: addr, | ||
| Handler: handler, | ||
| ReadTimeout: 15 * time.Second, | ||
| ReadHeaderTimeout: 5 * time.Second, | ||
| WriteTimeout: 15 * time.Second, | ||
| IdleTimeout: 60 * time.Second, | ||
| } | ||
|
|
||
| if sentry := app.GetSentry(); sentry != nil && sentry.Handler != nil { | ||
| handler = sentry.Handler.Handle(handler) | ||
| if err := endpoint.RunServer(addr, server); err != nil { | ||
| return fmt.Errorf("serve http: %w", err) | ||
| } | ||
|
|
||
| return handler | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.