goshort/cmd/dev/dev.go
2023-08-17 14:14:59 -03:00

116 lines
3.4 KiB
Go

package devcmd
import (
"context"
"flag"
"fmt"
"net/http"
"git.maronato.dev/maronato/goshort/cmd/shared"
"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"
shortserver "git.maronato.dev/maronato/goshort/internal/server/short"
shortservice "git.maronato.dev/maronato/goshort/internal/service/short"
memorystorage "git.maronato.dev/maronato/goshort/internal/storage/memory"
handlerutils "git.maronato.dev/maronato/goshort/internal/util/handler"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"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 {
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 {
// Create the new server
server := server.NewServer(cfg)
// Create services
shortStorage := memorystorage.NewMemoryStorage()
shortService := shortservice.NewShortService(shortStorage)
// Create handlers
apiHandler := apiserver.NewAPIHandler(shortService)
shortHandler := shortserver.NewShortHandler(shortService)
healthcheckHandler := healthcheckserver.NewHealthcheckHandler()
// Create routers
apiRouter := apiserver.NewAPIRouter(apiHandler)
shortRouter := shortserver.NewShortRouter(shortHandler)
healthcheckRouter := healthcheckserver.NewHealthcheckRouter(healthcheckHandler)
// Create the root URL handler by chaining short and NotFound handlers
chainedRouter := handlerutils.NewChainedHandler(shortRouter, http.NotFoundHandler())
// Configure app routes
server.Mux.Group(func(r chi.Router) {
// Set CORS headers for API routes in development mode
r.Use(middleware.SetHeader("Access-Control-Allow-Origin", "*"))
r.Mount("/api", apiRouter)
})
server.Mux.Mount("/healthz", healthcheckRouter)
server.Mux.Mount("/", chainedRouter)
if err := server.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
server := devuiserver.NewServer(cfg)
if err := server.ListenAndServe(ctx); err != nil {
return fmt.Errorf("error during UI server execution, %w", err)
}
return nil
}