-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
116 lines (95 loc) · 3.49 KB
/
main.go
File metadata and controls
116 lines (95 loc) · 3.49 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
package main
import (
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/hashicorp/nomad/api"
v1 "github.com/DistroByte/molecule/internal/api/v1"
"github.com/DistroByte/molecule/internal/config"
generated "github.com/DistroByte/molecule/internal/generated/go"
"github.com/DistroByte/molecule/internal/handlers"
"github.com/DistroByte/molecule/internal/server"
"github.com/DistroByte/molecule/logger"
)
//go:generate docker run --rm -v $PWD:/spec redocly/cli lint apispec/spec/index.yaml
//go:generate docker run -u 1000:1000 --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:v7.14.0 generate -i /local/apispec/spec/index.yaml -g go-server -o /local/internal/generated -c /local/apispec/server-config.yaml
func main() {
logger.InitLogger()
logger.Log.Info().Msg("logger initialized")
// Load configuration
var cfg *config.Config
var nomadService v1.NomadServiceInterface
if os.Getenv("PROD") == "true" {
var err error
cfg, err = config.LoadFromEnvironment()
if err != nil {
logger.Log.Fatal().Err(err).Msg("failed to load configuration")
}
if len(cfg.StandardURLs) == 0 {
logger.Log.Warn().Msg("no standard URLs found in configuration")
}
// Create Nomad client
nomadClient, err := api.NewClient(&api.Config{Address: cfg.Nomad.Address})
if err != nil {
logger.Log.Error().Err(err).Msg("failed to create nomad client")
return
}
// Convert config URLs to generated format
var standardURLsSlice []generated.ServiceUrl
for _, entry := range cfg.StandardURLs {
standardURLsSlice = append(standardURLsSlice, generated.ServiceUrl{
Service: entry.Service,
Url: entry.URL,
Icon: entry.Icon,
Fetched: false,
})
}
nomadService = v1.NewNomadService(nomadClient, standardURLsSlice)
} else {
nomadService = v1.NewMockNomadService()
cfg = &config.Config{} // Default config for dev mode
cfg.ServerConfig.Port = 8080
}
// Validate API key
apiKey := os.Getenv("API_KEY")
logger.Log.Trace().Msgf("API_KEY: %s", apiKey)
if apiKey == "" {
logger.Log.Fatal().Msg("API_KEY environment variable is required")
}
// Create services
moleculeAPIService := v1.NewMoleculeAPIService(nomadService)
moleculeAPIController := generated.NewDefaultAPIController(moleculeAPIService)
// Create and configure server
srv := server.New(cfg.ServerConfig.Host, cfg.ServerConfig.Port)
r := srv.Router()
// Setup routes
setupRoutes(r, moleculeAPIController, apiKey)
// Start server
logger.Log.Fatal().Err(srv.Start()).Msg("server failed to start")
}
// setupRoutes configures all application routes
func setupRoutes(r chi.Router, moleculeAPIController *generated.DefaultAPIController, apiKey string) {
// Create handlers
staticHandler := handlers.NewStaticHandler()
specHandler := handlers.NewAPISpecHandler()
// Static content routes
r.Get("/", staticHandler.ServeHome)
r.Route("/static", func(staticRouter chi.Router) {
staticRouter.Use(middleware.NoCache)
staticRouter.Handle("/*", http.StripPrefix("/static", http.FileServer(http.Dir("./web"))))
})
// API specification route
r.Get("/api/spec.json", specHandler.ServeSpec)
// Setup API routes with authentication
apiRouter := chi.NewRouter()
apiRouter.Use(server.APIKeyAuthMiddleware(apiKey))
for _, route := range moleculeAPIController.Routes() {
if server.RequiresAuth(route.Pattern) {
apiRouter.Method(route.Method, route.Pattern, route.HandlerFunc)
} else {
r.Method(route.Method, route.Pattern, route.HandlerFunc)
}
}
r.Mount("/", apiRouter)
}