forked from erikvanbrakel/anthology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (59 loc) · 1.73 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"net/http"
v1 "github.com/erikvanbrakel/anthology/api/v1"
"github.com/erikvanbrakel/anthology/app"
"github.com/erikvanbrakel/anthology/registry"
"github.com/erikvanbrakel/anthology/services"
routing "github.com/go-ozzo/ozzo-routing"
"github.com/go-ozzo/ozzo-routing/content"
"github.com/sirupsen/logrus"
)
func main() {
fmt.Println("Starting server ... loading config ... new ")
if err := app.LoadConfig(); err != nil {
panic(fmt.Errorf("invalid configuration: %s", err))
}
logger := logrus.New()
var r registry.Registry
switch app.Config.Backend {
case "s3":
r = registry.NewS3Registry(app.Config.S3)
break
case "filesystem":
r = registry.NewFilesystemRegistry(app.Config.FileSystem)
break
case "oras":
r = registry.NewOrasRegistry(app.Config.Oras)
break
}
http.Handle("/", buildRouter(logger, r))
address := fmt.Sprintf(":%v", app.Config.Port)
logger.Infof("server %v is started at %v", app.Version, address)
if app.Config.SSLConfig.IsValid() {
panic(http.ListenAndServeTLS(address, app.Config.SSLConfig.Certificate, app.Config.SSLConfig.Key, nil))
} else {
panic(http.ListenAndServe(address, nil))
}
}
func buildRouter(logger *logrus.Logger, reg registry.Registry) *routing.Router {
router := routing.New()
router.To("GET,HEAD", "/ping", func(c *routing.Context) error {
c.Abort()
return c.Write("OK" + app.Version)
})
router.Use(
app.Init(logger),
content.TypeNegotiator(content.JSON),
)
router.To("GET", "/.well-known/terraform.json", func(c *routing.Context) error {
c.Abort()
return c.Write(map[string]string{
"modules.v1": "/v1/modules/",
})
})
rg := router.Group("/v1/modules")
v1.ServeModuleResource(rg, services.NewModuleService(reg))
return router
}