2023-08-17 14:14:59 -03:00
|
|
|
package devcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2023-08-24 22:03:58 -03:00
|
|
|
"log/slog"
|
2023-08-18 19:51:10 -03:00
|
|
|
"net"
|
2023-08-17 14:14:59 -03:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.maronato.dev/maronato/goshort/cmd/shared"
|
2023-08-30 21:03:44 -03:00
|
|
|
"git.maronato.dev/maronato/goshort/docs"
|
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"
|
|
|
|
devuiserver "git.maronato.dev/maronato/goshort/internal/server/devui"
|
|
|
|
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"
|
2023-08-30 21:03:44 -03:00
|
|
|
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 14:14:59 -03:00
|
|
|
"github.com/go-chi/chi/v5"
|
2023-08-21 01:19:10 -03:00
|
|
|
"github.com/go-chi/cors"
|
2023-08-17 14:14:59 -03:00
|
|
|
"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 {
|
2023-08-24 22:03:58 -03:00
|
|
|
l := logging.FromCtx(ctx)
|
|
|
|
l.Debug("Executing dev command", slog.Any("config", cfg))
|
|
|
|
|
2023-08-17 14:14:59 -03:00
|
|
|
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 {
|
2023-08-26 11:47:46 -03:00
|
|
|
l := logging.FromCtx(ctx)
|
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
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 NotFound handlers
|
|
|
|
chainedRouter := handlerutils.NewChainedHandler(shortRouter, http.NotFoundHandler())
|
|
|
|
|
|
|
|
// Configure app routes
|
2023-08-26 11:47:46 -03:00
|
|
|
srv.Mux.Route("/api", func(r chi.Router) {
|
2023-08-17 14:14:59 -03:00
|
|
|
// Set CORS headers for API routes in development mode
|
2023-08-21 01:19:10 -03:00
|
|
|
r.Use(cors.Handler(cors.Options{
|
|
|
|
AllowedOrigins: []string{
|
|
|
|
"http://" + net.JoinHostPort(cfg.Host, cfg.UIPort),
|
|
|
|
},
|
2023-08-24 22:03:58 -03:00
|
|
|
AllowedMethods: []string{"GET", "POST", "PATCH", "DELETE", "OPTIONS"},
|
2023-08-21 01:19:10 -03:00
|
|
|
AllowCredentials: true,
|
|
|
|
}))
|
|
|
|
|
2023-08-30 21:03:44 -03:00
|
|
|
r.Use(authmiddleware.Auth(userService, tokenService))
|
|
|
|
r.Mount("/docs", docsRouter)
|
2023-08-17 17:55:28 -03:00
|
|
|
r.Mount("/", apiRouter)
|
2023-08-17 14:14:59 -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
|
|
|
}
|
2024-03-09 07:42:36 -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("error during API server execution, %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveUI(ctx context.Context, cfg *config.Config) error {
|
|
|
|
// Create the UI server
|
2023-08-26 11:47:46 -03:00
|
|
|
srv := devuiserver.NewServer(cfg)
|
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("error during UI server execution, %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|