goshort/cmd/dev/dev.go
Gustavo Maronato 28b49fb78f
All checks were successful
Check / checks (push) Successful in 2m57s
use odc instead of oidc for url
2024-03-23 14:56:02 -04:00

172 lines
5.4 KiB
Go

package devcmd
import (
"context"
"flag"
"fmt"
"log/slog"
"net"
"net/http"
"git.maronato.dev/maronato/goshort/cmd/shared"
"git.maronato.dev/maronato/goshort/docs"
"git.maronato.dev/maronato/goshort/internal/config"
"git.maronato.dev/maronato/goshort/internal/server"
apiserver "git.maronato.dev/maronato/goshort/internal/server/api"
devuiserver "git.maronato.dev/maronato/goshort/internal/server/devui"
healthcheckserver "git.maronato.dev/maronato/goshort/internal/server/healthcheck"
authmiddleware "git.maronato.dev/maronato/goshort/internal/server/middleware/auth"
oidcserver "git.maronato.dev/maronato/goshort/internal/server/oidc"
shortserver "git.maronato.dev/maronato/goshort/internal/server/short"
staticssterver "git.maronato.dev/maronato/goshort/internal/server/static"
configservice "git.maronato.dev/maronato/goshort/internal/service/config"
oidcservice "git.maronato.dev/maronato/goshort/internal/service/oidc"
shortservice "git.maronato.dev/maronato/goshort/internal/service/short"
shortlogservice "git.maronato.dev/maronato/goshort/internal/service/shortlog"
tokenservice "git.maronato.dev/maronato/goshort/internal/service/token"
userservice "git.maronato.dev/maronato/goshort/internal/service/user"
handlerutils "git.maronato.dev/maronato/goshort/internal/util/handler"
"git.maronato.dev/maronato/goshort/internal/util/logging"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/peterbourgon/ff/v3/ffcli"
"golang.org/x/sync/errgroup"
)
func New(cfg *config.Config) *ffcli.Command {
// Create the flagset and register the flags.
fs := flag.NewFlagSet("goshort dev", flag.ContinueOnError)
shared.RegisterBaseFlags(fs, cfg)
shared.RegisterServerFlags(fs, cfg)
// Create the command and options
cmd := shared.NewCommand(cfg, exec)
opts := shared.NewSharedCmdOptions()
// Register the UI-port flag
fs.StringVar(&cfg.UIPort, "ui-port", config.DefaultUIPort, "port to listen on for the UI")
// Return the ffcli command.
return &ffcli.Command{
Name: "dev",
ShortUsage: "goshort dev [flags]",
ShortHelp: "Starts the API and frontend servers in development mode",
FlagSet: fs,
Exec: cmd.Exec,
Options: opts,
}
}
func exec(ctx context.Context, cfg *config.Config) error {
l := logging.FromCtx(ctx)
l.Debug("Executing dev command", slog.Any("config", cfg))
eg, egCtx := errgroup.WithContext(ctx)
// Start the API server
eg.Go(func() error {
return serveAPI(egCtx, cfg)
})
// Start the UI server
eg.Go(func() error {
return serveUI(egCtx, cfg)
})
// Wait for the context to be done
if err := eg.Wait(); err != nil {
return fmt.Errorf("error during dev servers execution, %w", err)
}
return nil
}
func serveAPI(ctx context.Context, cfg *config.Config) error {
l := logging.FromCtx(ctx)
// Create the new server
srv := server.NewServer(cfg)
// Create services
storage := shared.InitStorage(ctx, cfg)
err := storage.Start(ctx)
if err != nil {
return fmt.Errorf("failed to start storage, %w", err)
}
defer func() {
err := storage.Stop(ctx)
if err != nil {
l.Error("failed to stop storage", "error", err)
}
}()
shortService := shortservice.NewShortService(storage)
userService := userservice.NewUserService(cfg, storage)
tokenService := tokenservice.NewTokenService(storage)
shortLogService := shortlogservice.NewShortLogService(storage)
oidcService := oidcservice.NewOIDCService(ctx, cfg)
configService := configservice.NewConfigService(cfg)
// Start short log worker
stopWorker, _ := shortLogService.StartWorker(ctx)
defer stopWorker()
// Create handlers
apiHandler := apiserver.NewAPIHandler(shortService, userService, tokenService, shortLogService, configService)
shortHandler := shortserver.NewShortHandler(shortService, shortLogService)
healthcheckHandler := healthcheckserver.NewHealthcheckHandler(storage)
docsHandler := staticssterver.NewStaticHandler(cfg, "/api/docs", docs.Assets())
oidcHandler := oidcserver.NewOIDCHandler(cfg, oidcService, userService)
// Create routers
apiRouter := apiserver.NewAPIRouter(apiHandler)
shortRouter := shortserver.NewShortRouter(shortHandler)
healthcheckRouter := healthcheckserver.NewHealthcheckRouter(healthcheckHandler)
docsRouter := staticssterver.NewStaticRouter(docsHandler)
oidcRouter := oidcserver.NewOIDCRouter(oidcHandler)
// Create the root URL handler by chaining short and NotFound handlers
chainedRouter := handlerutils.NewChainedHandler(shortRouter, http.NotFoundHandler())
// Configure app routes
srv.Mux.Route("/api", func(r chi.Router) {
// Set CORS headers for API routes in development mode
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{
"http://" + net.JoinHostPort(cfg.Host, cfg.UIPort),
},
AllowedMethods: []string{"GET", "POST", "PATCH", "DELETE", "OPTIONS"},
AllowCredentials: true,
}))
r.Use(authmiddleware.Auth(userService, tokenService))
r.Mount("/docs", docsRouter)
r.Mount("/", apiRouter)
})
// Register OIDC routes if the service is enabled
if oidcService != nil {
srv.Mux.Mount("/odc", oidcRouter)
}
srv.Mux.Mount("/healthz", healthcheckRouter)
srv.Mux.Mount("/", chainedRouter)
if err := srv.ListenAndServe(ctx); err != nil {
return fmt.Errorf("error during API server execution, %w", err)
}
return nil
}
func serveUI(ctx context.Context, cfg *config.Config) error {
// Create the UI server
srv := devuiserver.NewServer(cfg)
if err := srv.ListenAndServe(ctx); err != nil {
return fmt.Errorf("error during UI server execution, %w", err)
}
return nil
}