-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
121 lines (102 loc) · 2.92 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
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
package main
/*
* License: GPL-3.0-or-later
* Authors:
* Mateus Melchiades <matbme@duck.com>
* Copyright: 2023
*/
import (
"errors"
"fmt"
"os"
"os/exec"
"github.com/gin-gonic/gin"
"github.com/vanilla-os/differ/core"
"github.com/vanilla-os/differ/core/handlers"
)
func setupRouter(dbPath string) (*gin.Engine, error) {
// Create database if it doesn't exist
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
dbUser, ok := os.LookupEnv("admin_user")
if !ok {
return nil, errors.New("admin_user environment variable not found")
}
dbPass, ok := os.LookupEnv("admin_password")
if !ok {
return nil, errors.New("admin_password environment variable not found")
}
err = exec.Command(
"sh",
"-c",
fmt.Sprintf("sqlite3 %s \"create table auth(ID INTEGER, name, pass TEXT, PRIMARY KEY(ID)); insert into auth values(1, '%s', '%s');\"", dbPath, dbUser, dbPass),
).Run()
if err != nil {
return nil, err
}
}
// Initialize storage database
err := core.InitStorage(dbPath)
if err != nil {
return nil, errors.New("Failed to init storage: " + err.Error())
}
// Initialize cache
err = core.InitCache()
if err != nil {
return nil, errors.New("Failed to init cache: " + err.Error())
}
// Fetches authentications from storage
auths, err := core.FetchAuthorizations()
if err != nil {
return nil, errors.New("Failed to fetch authorizations from storage: " + err.Error())
}
// If auths is empty, we run the API in "read-only" mode.
// In other words, we won't be able to add any images or releases via the API.
readOnly := len(auths) == 0
var authRequired gin.HandlerFunc
if !readOnly {
authRequired = gin.BasicAuth(auths)
}
r := gin.Default()
r.SetTrustedProxies(nil)
// Endpoint to check if API is running
r.GET("/status", handlers.HandleStatus)
// Manipulate images
images := r.Group("/images")
{
// List all images
images.GET("/", handlers.HandleGetImages)
// List specific image
images.GET("/:name", handlers.HandleFindImage)
// Creates new image (Auth required)
if !readOnly {
images.POST("/new", authRequired, handlers.HandleAddImage)
}
// Release-related endpoints
// Diffs two releases
images.GET("/:name/diff", handlers.HandleGetReleaseDiff)
// Gets latest release
images.GET("/:name/latest", handlers.HandleGetLatestRelease)
// Gets specific release with digest
images.GET("/:name/:digest", handlers.HandleFindRelease)
// Creates new release (Auth required)
if !readOnly {
images.POST("/:name/new", authRequired, handlers.HandleAddRelease)
}
}
return r, nil
}
func main() {
var dbPath string
if len(os.Args) > 1 {
dbPath = os.Args[1]
} else if dbPathArg, ok := os.LookupEnv("db_path"); ok {
dbPath = dbPathArg
} else {
panic("No path to DB was provided. You must either pass it as a positional argument or by setting the 'db_path' environment variable")
}
router, err := setupRouter(dbPath)
if err != nil {
panic(err)
}
router.Run()
}