-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (45 loc) · 1.47 KB
/
main.go
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
package main
import (
"fmt"
"github.com/recrsn/coffee-beans/repositories"
"html/template"
"io/fs"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/recrsn/coffee-beans/config"
"github.com/recrsn/coffee-beans/handlers"
"github.com/recrsn/coffee-beans/utils"
)
var (
version = "dev"
commit = "HEAD"
)
func main() {
log.Printf("Coffee Beans %s (%s-%s)\n", version, commit, buildMode)
gin.SetMode(buildMode)
cfg, err := config.Load()
utils.Must(err)
router := gin.Default()
// version is a build-time variable, so it's always set
if buildMode == "release" {
router.SetHTMLTemplate(template.Must(template.New("").ParseFS(embedFS, "templates/*.html")))
static, err := fs.Sub(embedFS, "static")
utils.Must(err)
router.StaticFS("/static", http.FS(static))
} else {
log.Printf("Running in development mode, loading templates and assets from filesystem\n")
router.LoadHTMLGlob("templates/*.html")
router.Static("/static", "static")
}
router.GET("/health", handlers.HealthCheck())
manager := repositories.NewRepositoryManager(cfg.Server.BaseURL, cfg.Repositories)
router.GET("/", repositories.RepositoryList(manager))
router.GET("/repositories/:id", repositories.RepositoryDetails(manager))
for _, r := range manager.GetRepositories() {
router.PUT(r.Path(), handlers.Upload(r.Root))
router.StaticFS(r.StaticPath(), gin.Dir(r.Root, true))
}
addr := fmt.Sprintf("%s:%d", cfg.Server.ListenAddress, cfg.Server.ListenPort)
utils.Must(router.Run(addr))
}