goshort/cmd/serve/serve.go

127 lines
4.4 KiB
Go
Raw Permalink Normal View History

2023-08-17 14:14:59 -03:00
package servecmd
import (
"context"
"flag"
"fmt"
2023-08-24 22:03:58 -03:00
"log/slog"
2023-08-17 14:14:59 -03:00
"git.maronato.dev/maronato/goshort/cmd/shared"
2023-08-30 21:03:44 -03:00
"git.maronato.dev/maronato/goshort/docs"
"git.maronato.dev/maronato/goshort/frontend"
2023-08-17 14:14:59 -03:00
"git.maronato.dev/maronato/goshort/internal/config"
"git.maronato.dev/maronato/goshort/internal/server"
apiserver "git.maronato.dev/maronato/goshort/internal/server/api"
healthcheckserver "git.maronato.dev/maronato/goshort/internal/server/healthcheck"
2023-08-30 21:03:44 -03:00
authmiddleware "git.maronato.dev/maronato/goshort/internal/server/middleware/auth"
2024-03-09 06:44:22 -03:00
oidcserver "git.maronato.dev/maronato/goshort/internal/server/oidc"
2023-08-17 14:14:59 -03:00
shortserver "git.maronato.dev/maronato/goshort/internal/server/short"
staticssterver "git.maronato.dev/maronato/goshort/internal/server/static"
2024-03-09 07:42:36 -03:00
configservice "git.maronato.dev/maronato/goshort/internal/service/config"
2024-03-09 06:44:22 -03:00
oidcservice "git.maronato.dev/maronato/goshort/internal/service/oidc"
2023-08-17 14:14:59 -03:00
shortservice "git.maronato.dev/maronato/goshort/internal/service/short"
2023-08-24 22:03:58 -03:00
shortlogservice "git.maronato.dev/maronato/goshort/internal/service/shortlog"
2023-08-21 01:19:10 -03:00
tokenservice "git.maronato.dev/maronato/goshort/internal/service/token"
2023-08-17 17:55:28 -03:00
userservice "git.maronato.dev/maronato/goshort/internal/service/user"
2023-08-17 14:14:59 -03:00
handlerutils "git.maronato.dev/maronato/goshort/internal/util/handler"
2023-08-24 22:03:58 -03:00
"git.maronato.dev/maronato/goshort/internal/util/logging"
2023-08-17 17:55:28 -03:00
"github.com/go-chi/chi/v5"
2023-08-17 14:14:59 -03:00
"github.com/peterbourgon/ff/v3/ffcli"
)
func New(cfg *config.Config) *ffcli.Command {
// Create the flagset and register the flags.
fs := flag.NewFlagSet("goshort serve", flag.ContinueOnError)
shared.RegisterBaseFlags(fs, cfg)
shared.RegisterServerFlags(fs, cfg)
2023-08-24 22:03:58 -03:00
fs.BoolVar(&cfg.Prod, "prod", config.DefaultProd, "run in production mode")
2023-08-17 14:14:59 -03:00
// Create the command and options
cmd := shared.NewCommand(cfg, exec)
opts := shared.NewSharedCmdOptions()
// Return the ffcli command.
return &ffcli.Command{
Name: "serve",
ShortUsage: "goshort serve [flags]",
2024-03-28 18:29:30 -03:00
ShortHelp: "Start the API server with embedded frontend",
2023-08-17 14:14:59 -03:00
FlagSet: fs,
Exec: cmd.Exec,
Options: opts,
}
}
func exec(ctx context.Context, cfg *config.Config) error {
2023-08-24 22:03:58 -03:00
l := logging.FromCtx(ctx)
l.Debug("Executing serve command", slog.Any("config", cfg))
2023-08-17 14:14:59 -03:00
// Create the new server
2023-08-26 11:47:46 -03:00
srv := server.NewServer(cfg)
2023-08-17 14:14:59 -03:00
// Create services
2023-08-26 11:47:46 -03:00
storage := shared.InitStorage(ctx, cfg)
2023-08-22 00:48:12 -03:00
err := storage.Start(ctx)
if err != nil {
return fmt.Errorf("failed to start storage, %w", err)
}
2023-08-26 11:47:46 -03:00
defer func() {
err := storage.Stop(ctx)
if err != nil {
l.Error("failed to stop storage", "error", err)
}
}()
2023-08-22 00:48:12 -03:00
2023-08-17 19:55:08 -03:00
shortService := shortservice.NewShortService(storage)
userService := userservice.NewUserService(cfg, storage)
2023-08-21 01:19:10 -03:00
tokenService := tokenservice.NewTokenService(storage)
2023-08-24 22:03:58 -03:00
shortLogService := shortlogservice.NewShortLogService(storage)
2024-03-09 06:44:22 -03:00
oidcService := oidcservice.NewOIDCService(ctx, cfg)
2024-03-09 07:42:36 -03:00
configService := configservice.NewConfigService(cfg)
2023-08-24 22:03:58 -03:00
// Start short log worker
2023-09-06 12:38:27 -03:00
stopWorker, _ := shortLogService.StartWorker(ctx)
2023-08-24 22:03:58 -03:00
defer stopWorker()
2023-08-17 14:14:59 -03:00
// Create handlers
2024-03-09 07:42:36 -03:00
apiHandler := apiserver.NewAPIHandler(shortService, userService, tokenService, shortLogService, configService)
2023-08-24 22:03:58 -03:00
shortHandler := shortserver.NewShortHandler(shortService, shortLogService)
2023-08-30 21:03:44 -03:00
staticHandler := staticssterver.NewStaticHandler(cfg, "/", frontend.Assets())
2023-08-24 22:03:58 -03:00
healthcheckHandler := healthcheckserver.NewHealthcheckHandler(storage)
2023-08-30 21:03:44 -03:00
docsHandler := staticssterver.NewStaticHandler(cfg, "/api/docs", docs.Assets())
2024-03-09 07:42:36 -03:00
oidcHandler := oidcserver.NewOIDCHandler(cfg, oidcService, userService)
2023-08-17 14:14:59 -03:00
// Create routers
apiRouter := apiserver.NewAPIRouter(apiHandler)
shortRouter := shortserver.NewShortRouter(shortHandler)
staticRouter := staticssterver.NewStaticRouter(staticHandler)
healthcheckRouter := healthcheckserver.NewHealthcheckRouter(healthcheckHandler)
2023-08-30 21:03:44 -03:00
docsRouter := staticssterver.NewStaticRouter(docsHandler)
2024-03-09 06:44:22 -03:00
oidcRouter := oidcserver.NewOIDCRouter(oidcHandler)
2023-08-17 14:14:59 -03:00
// Create the root URL handler by chaining short and static routers
chainedRouter := handlerutils.NewChainedHandler(shortRouter, staticRouter)
// Configure app routes
2023-08-26 11:47:46 -03:00
srv.Mux.Route("/api", func(r chi.Router) {
2023-08-30 21:03:44 -03:00
r.Use(authmiddleware.Auth(userService, tokenService))
2023-08-17 17:55:28 -03:00
r.Mount("/", apiRouter)
2023-08-30 21:03:44 -03:00
r.Mount("/docs", docsRouter)
2023-08-17 17:55:28 -03:00
})
2024-03-09 06:44:22 -03:00
// Register OIDC routes if the service is enabled
if oidcService != nil {
2024-03-23 15:56:02 -03:00
srv.Mux.Mount("/odc", oidcRouter)
2024-03-09 06:44:22 -03:00
}
2023-08-26 11:47:46 -03:00
srv.Mux.Mount("/healthz", healthcheckRouter)
srv.Mux.Mount("/", chainedRouter)
2023-08-17 14:14:59 -03:00
2023-08-26 11:47:46 -03:00
if err := srv.ListenAndServe(ctx); err != nil {
2023-08-17 14:14:59 -03:00
return fmt.Errorf("rrror during server execution, %w", err)
}
return nil
}