-
Notifications
You must be signed in to change notification settings - Fork 386
/
main.go
159 lines (132 loc) · 4.29 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
// Package main defines the satellite administration API through the API generator and generates
// source code of the API server handlers and clients and the documentation markdown document.
package main
//go:generate go run $GOFILE
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"storj.io/common/uuid"
"storj.io/storj/private/apigen"
backoffice "storj.io/storj/satellite/admin/back-office"
)
func main() {
api := &apigen.API{
PackageName: "admin",
PackagePath: "storj.io/storj/satellite/admin/back-office",
Version: "v1",
BasePath: path.Join(backoffice.PathPrefix, "/api"),
}
group := api.Group("Settings", "settings")
group.Get("/", &apigen.Endpoint{
Name: "Get settings",
Description: "Gets the settings of the service and relevant Storj services settings",
GoName: "GetSettings",
TypeScriptName: "get",
Response: backoffice.Settings{},
})
group = api.Group("PlacementManagement", "placements")
group.Get("/", &apigen.Endpoint{
Name: "Get placements",
Description: "Gets placement rule IDs and their locations",
GoName: "GetPlacements",
TypeScriptName: "getPlacements",
Response: []backoffice.PlacementInfo{},
})
group = api.Group("UserManagement", "users")
group.Middleware = append(group.Middleware, authMiddleware{})
group.Get("/{email}", &apigen.Endpoint{
Name: "Get user",
Description: "Gets user by email address",
GoName: "GetUserByEmail",
TypeScriptName: "getUserByEmail",
PathParams: []apigen.Param{
apigen.NewParam("email", ""),
},
Response: backoffice.UserAccount{},
Settings: map[any]any{
authPermsKey: []backoffice.Permission{backoffice.PermAccountView},
},
})
group = api.Group("ProjectManagement", "projects")
group.Middleware = append(group.Middleware, authMiddleware{})
group.Get("/{publicID}", &apigen.Endpoint{
Name: "Get project",
Description: "Gets project by ID",
GoName: "GetProject",
TypeScriptName: "getProject",
PathParams: []apigen.Param{
apigen.NewParam("publicID", uuid.UUID{}),
},
Response: backoffice.Project{},
Settings: map[any]any{
authPermsKey: []backoffice.Permission{backoffice.PermProjectView},
},
})
group.Put("/limits/{publicID}", &apigen.Endpoint{
Name: "Update project limits",
Description: "Updates project limits by ID",
GoName: "UpdateProjectLimits",
TypeScriptName: "updateProjectLimits",
PathParams: []apigen.Param{
apigen.NewParam("publicID", uuid.UUID{}),
},
Request: backoffice.ProjectLimitsUpdate{},
Settings: map[any]any{
authPermsKey: []backoffice.Permission{backoffice.PermProjectSetLimits},
},
})
modroot := findModuleRootDir()
api.MustWriteGo(filepath.Join(modroot, "satellite", "admin", "back-office", "handlers.gen.go"))
api.MustWriteTS(filepath.Join(modroot, "satellite", "admin", "back-office", "ui", "src", "api", "client.gen.ts"))
api.MustWriteDocs(filepath.Join(modroot, "satellite", "admin", "back-office", "api-docs.gen.md"))
}
type authMiddleware struct {
//lint:ignore U1000 this field is used by the API generator to expose in the handler.
auth *backoffice.Authorizer
}
func (a authMiddleware) Generate(api *apigen.API, group *apigen.EndpointGroup, ep *apigen.FullEndpoint) string {
perms := apigen.LoadSetting(authPermsKey, ep, []backoffice.Permission{})
if len(perms) == 0 {
return ""
}
verbs := make([]string, 0, len(perms))
values := make([]any, 0, len(perms))
for _, p := range perms {
verbs = append(verbs, "%d")
values = append(values, p)
}
format := fmt.Sprintf(`if h.auth.IsRejected(w, r, %s) {
return
}`, strings.Join(verbs, ", "))
return fmt.Sprintf(format, values...)
}
var _ apigen.Middleware = authMiddleware{}
type tagAuthPerms struct{}
var authPermsKey = tagAuthPerms{}
func findModuleRootDir() string {
dir, err := os.Getwd()
if err != nil {
panic("unable to find current working directory")
}
start := dir
for i := 0; i < 100; i++ {
if fileExists(filepath.Join(dir, "go.mod")) {
return dir
}
next := filepath.Dir(dir)
if next == dir {
break
}
dir = next
}
panic("unable to find go.mod starting from " + start)
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}