-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (51 loc) · 1.64 KB
/
main.go
File metadata and controls
64 lines (51 loc) · 1.64 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
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
/*
* MassBank3 API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 3.0
*/
package main
import (
"embed"
"io/fs"
"log"
"net/http"
"strconv"
"github.com/MassBank/MassBank3/pkg/config"
"github.com/MassBank/MassBank3/pkg/mb3server"
"github.com/go-chi/chi/v5"
)
// Embed the swagger-ui directory
//go:embed swagger-ui
var swaggerContent embed.FS
func main() {
log.Printf("Server started")
mb3server.ServerConfig = config.GetServerConfig()
if mb3server.ServerConfig == nil {
log.Fatal("Could not read config variables")
}
DefaultApiService := mb3server.NewDefaultApiService()
DefaultApiController := mb3server.NewDefaultAPIController(DefaultApiService)
r := mb3server.NewRouter(DefaultApiController)
addSwaggerEndpoint(r)
router := chi.NewRouter()
router.Mount(mb3server.ServerConfig.BaseUrl, r)
log.Fatal(http.ListenAndServe(":"+strconv.FormatUint(uint64(mb3server.ServerConfig.ServerPort), 10), router))
}
func addSwaggerEndpoint(router chi.Router) {
// Create a subdirectory filesystem for swagger-ui
fsys, err := fs.Sub(swaggerContent, "swagger-ui")
if err != nil {
panic("failed to create sub filesystem: " + err.Error())
}
baseUrl := mb3server.ServerConfig.BaseUrl
if baseUrl == "/" {
baseUrl = ""
}
// Serve the swagger-ui files
router.Handle("/ui/*", http.StripPrefix(baseUrl+"/ui/", http.FileServer(http.FS(fsys))))
// Redirect root to the swagger-ui
router.Handle("/", http.RedirectHandler(mb3server.ServerConfig.ApiUrl+baseUrl+"/ui/", http.StatusFound))
}