-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (115 loc) · 3.1 KB
/
Copy pathmain.go
File metadata and controls
132 lines (115 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"
"github.com/UnitVectorY-Labs/remventory/internal/config"
"github.com/UnitVectorY-Labs/remventory/internal/httpapi"
"github.com/UnitVectorY-Labs/remventory/internal/itemimages"
"github.com/UnitVectorY-Labs/remventory/internal/mcpserver"
"github.com/UnitVectorY-Labs/remventory/internal/remy"
"github.com/UnitVectorY-Labs/remventory/internal/store"
mcptransport "github.com/mark3labs/mcp-go/server"
)
// Version is the application version, injected at build time via ldflags.
var Version = "dev"
func main() {
if err := run(); err != nil {
slog.Error("remventory stopped", "error", err)
os.Exit(1)
}
}
func run() error {
resolveVersion()
cfg := config.Load()
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: cfg.LogLevel,
}))
slog.SetDefault(logger)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
var repo *store.Store
if cfg.DatabaseURL != "" {
db, err := store.Open(ctx, cfg.DatabaseURL)
if err != nil {
return fmt.Errorf("open database: %w", err)
}
defer db.Close()
if cfg.AutoMigrate {
if err := db.Migrate(ctx); err != nil {
return fmt.Errorf("run migrations: %w", err)
}
}
if err := db.EnsureDefaultUser(ctx, cfg.DefaultUserName); err != nil {
return fmt.Errorf("ensure default user: %w", err)
}
repo = db
} else {
logger.Warn("DATABASE_URL is not set; database-backed routes will report unavailable")
}
remyService := remy.New(cfg, repo)
var imageObjects itemimages.ObjectStore
if cfg.StorageConfigured() {
storage, err := itemimages.NewS3Store(ctx, cfg)
if err != nil {
return fmt.Errorf("configure image storage: %w", err)
}
imageObjects = storage
} else {
logger.Warn("S3 image storage is not configured; image uploads will report unavailable")
}
var mcpHandler http.Handler
if repo != nil {
mcpHandler = mcptransport.NewStreamableHTTPServer(mcpserver.New(Version, repo, remyService))
}
api := httpapi.New(httpapi.Options{
Config: cfg,
Store: repo,
Remy: remyService,
MCPHandler: mcpHandler,
Version: Version,
Logger: logger,
Images: imageObjects,
})
server := &http.Server{
Addr: cfg.HTTPAddr,
Handler: api,
ReadHeaderTimeout: 5 * time.Second,
}
errc := make(chan error, 1)
go func() {
logger.Info("starting remventory", "version", Version, "addr", cfg.HTTPAddr)
errc <- server.ListenAndServe()
}()
select {
case <-ctx.Done():
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(shutdownCtx); err != nil {
return fmt.Errorf("shutdown server: %w", err)
}
return nil
case err := <-errc:
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
}
}
func resolveVersion() {
if Version != "dev" && Version != "" {
return
}
if bi, ok := debug.ReadBuildInfo(); ok {
if bi.Main.Version != "" && bi.Main.Version != "(devel)" {
Version = bi.Main.Version
}
}
}