Updated code structure using new Cobra CLI conventions
This commit is contained in:
+8
-65
@@ -1,87 +1,30 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"scratchbox/internal/cleanup"
|
|
||||||
"scratchbox/internal/config"
|
|
||||||
httpapi "scratchbox/internal/http"
|
|
||||||
"scratchbox/internal/storage"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd := newRootCmd()
|
cmd := newRootCmd()
|
||||||
if err := cmd.Execute(); err != nil {
|
if err := cmd.Execute(); err != nil {
|
||||||
log.Fatalf("command failed: %v", err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRootCmd() *cobra.Command {
|
func newRootCmd() *cobra.Command {
|
||||||
var configPath string
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "pastebin",
|
Use: "scratchbox",
|
||||||
Short: "Run the scratchbox service",
|
Short: "Scratchbox CLI",
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
SilenceErrors: true,
|
||||||
return run(configPath)
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
|
return cmd.Help()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().StringVarP(&configPath, "config", "c", "", "path to config file")
|
cmd.AddCommand(newServerCmd())
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(configPath string) error {
|
|
||||||
cfg, err := config.Load(configPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("config load failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger := log.New(os.Stdout, "[pastebin] ", log.LstdFlags|log.LUTC)
|
|
||||||
|
|
||||||
store, err := storage.NewFilesystemStore(cfg.Storage.DataDir)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("storage init failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
server, err := httpapi.NewServer(cfg, store, logger)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("http init failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupWorker := cleanup.NewWorker(store, cfg.Storage.CleanupIntervalParsed, logger)
|
|
||||||
|
|
||||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
go cleanupWorker.Start(ctx)
|
|
||||||
|
|
||||||
httpServer := &http.Server{
|
|
||||||
Addr: cfg.Server.ListenAddr,
|
|
||||||
Handler: server.Routes(),
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
<-ctx.Done()
|
|
||||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer shutdownCancel()
|
|
||||||
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
|
||||||
logger.Printf("http shutdown failed: %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
logger.Printf("listening on %s", cfg.Server.ListenAddr)
|
|
||||||
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
||||||
return fmt.Errorf("http server exited with error: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -180,16 +180,16 @@ func TestMainHelperProcess(t *testing.T) {
|
|||||||
_ = proc.Signal(os.Interrupt)
|
_ = proc.Signal(os.Interrupt)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
os.Args = []string{"pastebin", "--config", cfgPath}
|
os.Args = []string{"scratchbox", "server", "--config", cfgPath}
|
||||||
main()
|
main()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
case "bad-config":
|
case "bad-config":
|
||||||
os.Args = []string{"pastebin", "--config", "/no/such/config.yaml"}
|
os.Args = []string{"scratchbox", "server", "--config", "/no/such/config.yaml"}
|
||||||
main()
|
main()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
case "storage-fail":
|
case "storage-fail":
|
||||||
cfgPath := os.Getenv("MAIN_HELPER_CONFIG")
|
cfgPath := os.Getenv("MAIN_HELPER_CONFIG")
|
||||||
os.Args = []string{"pastebin", "--config", cfgPath}
|
os.Args = []string{"scratchbox", "server", "--config", cfgPath}
|
||||||
main()
|
main()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
case "http-fail":
|
case "http-fail":
|
||||||
@@ -197,7 +197,7 @@ func TestMainHelperProcess(t *testing.T) {
|
|||||||
if err := os.Chdir(t.TempDir()); err != nil {
|
if err := os.Chdir(t.TempDir()); err != nil {
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
os.Args = []string{"pastebin", "--config", cfgPath}
|
os.Args = []string{"scratchbox", "server", "--config", cfgPath}
|
||||||
main()
|
main()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"scratchbox/internal/cleanup"
|
||||||
|
"scratchbox/internal/config"
|
||||||
|
httpapi "scratchbox/internal/http"
|
||||||
|
"scratchbox/internal/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newServerCmd() *cobra.Command {
|
||||||
|
var configPath string
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "server",
|
||||||
|
Short: "Run the scratchbox service",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
|
return run(configPath)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.Flags().StringVarP(&configPath, "config", "c", "", "path to config file")
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(configPath string) error {
|
||||||
|
cfg, err := config.Load(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("config load failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger := log.New(os.Stdout, "[pastebin] ", log.LstdFlags|log.LUTC)
|
||||||
|
|
||||||
|
store, err := storage.NewFilesystemStore(cfg.Storage.DataDir)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("storage init failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
server, err := httpapi.NewServer(cfg, store, logger)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("http init failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanupWorker := cleanup.NewWorker(store, cfg.Storage.CleanupIntervalParsed, logger)
|
||||||
|
|
||||||
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
go cleanupWorker.Start(ctx)
|
||||||
|
|
||||||
|
httpServer := &http.Server{
|
||||||
|
Addr: cfg.Server.ListenAddr,
|
||||||
|
Handler: server.Routes(),
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer shutdownCancel()
|
||||||
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
||||||
|
logger.Printf("http shutdown failed: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
logger.Printf("listening on %s", cfg.Server.ListenAddr)
|
||||||
|
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
|
return fmt.Errorf("http server exited with error: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user