@@ -38,11 +38,11 @@ import (
38
38
"github.com/dadav/gorge/internal/utils"
39
39
v3 "github.com/dadav/gorge/internal/v3/api"
40
40
backend "github.com/dadav/gorge/internal/v3/backend"
41
+ "github.com/dadav/gorge/internal/v3/ui"
41
42
openapi "github.com/dadav/gorge/pkg/gen/v3/openapi"
42
43
"github.com/go-chi/chi/v5"
43
44
"github.com/go-chi/chi/v5/middleware"
44
45
"github.com/go-chi/cors"
45
- "github.com/go-chi/jwtauth/v5"
46
46
"github.com/go-chi/stampede"
47
47
"github.com/spf13/cobra"
48
48
"golang.org/x/sync/errgroup"
@@ -127,34 +127,20 @@ You can also enable the caching functionality to speed things up.`,
127
127
r := chi .NewRouter ()
128
128
129
129
// Logger should come before any middleware that modifies the response
130
- // r.Use(middleware.Logger)
130
+ r .Use (middleware .Logger )
131
131
// Recoverer should also be pretty high in the middleware stack
132
132
r .Use (middleware .Recoverer )
133
133
r .Use (middleware .RealIP )
134
134
r .Use (customMiddleware .RequireUserAgent )
135
+ x := customMiddleware .NewStatistics ()
136
+ r .Use (customMiddleware .StatisticsMiddleware (x ))
135
137
r .Use (cors .Handler (cors.Options {
136
138
AllowedOrigins : strings .Split (config .CORSOrigins , "," ),
137
139
AllowedMethods : []string {"GET" , "POST" , "DELETE" , "PATCH" },
138
140
AllowedHeaders : []string {"Accept" , "Content-Type" },
139
141
AllowCredentials : false ,
140
142
MaxAge : 300 ,
141
143
}))
142
-
143
- if ! config .Dev {
144
- tokenAuth := jwtauth .New ("HS256" , []byte (config .JwtSecret ), nil )
145
- r .Use (customMiddleware .AuthMiddleware (tokenAuth , func (r * http.Request ) bool {
146
- // Everything but GET is protected and requires a jwt token
147
- return r .Method != "GET"
148
- }))
149
-
150
- _ , tokenString , _ := tokenAuth .Encode (map [string ]interface {}{"user" : "admin" })
151
- err = os .WriteFile (config .JwtTokenPath , []byte (tokenString ), 0400 )
152
- if err != nil {
153
- log .Log .Fatal (err )
154
- }
155
- log .Log .Infof ("JWT token was written to %s" , config .JwtTokenPath )
156
- }
157
-
158
144
if ! config .NoCache {
159
145
customKeyFunc := func (r * http.Request ) uint64 {
160
146
token := r .Header .Get ("Authorization" )
@@ -164,45 +150,58 @@ You can also enable the caching functionality to speed things up.`,
164
150
r .Use (cachedMiddleware )
165
151
}
166
152
167
- if config .FallbackProxyUrl != "" {
168
- proxies := strings .Split (config .FallbackProxyUrl , "," )
169
- slices .Reverse (proxies )
170
-
171
- for _ , proxy := range proxies {
172
- r .Use (customMiddleware .ProxyFallback (proxy , func (status int ) bool {
173
- return status == http .StatusNotFound
174
- },
175
- func (r * http.Response ) {
176
- if config .ImportProxiedReleases && strings .HasPrefix (r .Request .URL .Path , "/v3/files/" ) && r .StatusCode == http .StatusOK {
177
- body , err := io .ReadAll (r .Body )
178
- if err != nil {
179
- log .Log .Error (err )
180
- return
181
- }
153
+ if config .UI {
154
+ r .Group (func (r chi.Router ) {
155
+ r .HandleFunc ("/" , ui .IndexHandler )
156
+ r .HandleFunc ("/search" , ui .SearchHandler )
157
+ r .HandleFunc ("/modules/{module}" , ui .ModuleHandler )
158
+ r .HandleFunc ("/modules/{module}/{version}" , ui .ReleaseHandler )
159
+ r .HandleFunc ("/authors/{author}" , ui .AuthorHandler )
160
+ r .HandleFunc ("/statistics" , ui .StatisticsHandler (x ))
161
+ r .Handle ("/assets/*" , ui .HandleAssets ())
162
+ })
163
+ }
182
164
183
- // restore the body
184
- r .Body = io .NopCloser (bytes .NewBuffer (body ))
165
+ r .Group (func (r chi.Router ) {
166
+ if config .FallbackProxyUrl != "" {
167
+ proxies := strings .Split (config .FallbackProxyUrl , "," )
168
+ slices .Reverse (proxies )
185
169
186
- release , err := backend .ConfiguredBackend .AddRelease (body )
187
- if err != nil {
188
- log .Log .Error (err )
189
- return
190
- }
191
- log .Log .Infof ("Imported release %s\n " , release .Slug )
192
- }
170
+ for _ , proxy := range proxies {
171
+ r .Use (customMiddleware .ProxyFallback (proxy , func (status int ) bool {
172
+ return status == http .StatusNotFound
193
173
},
194
- ))
174
+ func (r * http.Response ) {
175
+ if config .ImportProxiedReleases && strings .HasPrefix (r .Request .URL .Path , "/v3/files/" ) && r .StatusCode == http .StatusOK {
176
+ body , err := io .ReadAll (r .Body )
177
+ if err != nil {
178
+ log .Log .Error (err )
179
+ return
180
+ }
181
+
182
+ // restore the body
183
+ r .Body = io .NopCloser (bytes .NewBuffer (body ))
184
+
185
+ release , err := backend .ConfiguredBackend .AddRelease (body )
186
+ if err != nil {
187
+ log .Log .Error (err )
188
+ return
189
+ }
190
+ log .Log .Infof ("Imported release %s\n " , release .Slug )
191
+ }
192
+ },
193
+ ))
194
+ }
195
195
}
196
- }
197
-
198
- apiRouter := openapi .NewRouter (
199
- openapi .NewModuleOperationsAPIController (moduleService ),
200
- openapi .NewReleaseOperationsAPIController (releaseService ),
201
- openapi .NewSearchFilterOperationsAPIController (searchFilterService ),
202
- openapi .NewUserOperationsAPIController (userService ),
203
- )
204
-
205
- r .Mount ("/" , apiRouter )
196
+ apiRouter := openapi .NewRouter (
197
+ openapi .NewModuleOperationsAPIController (moduleService ),
198
+ openapi .NewReleaseOperationsAPIController (releaseService ),
199
+ openapi .NewSearchFilterOperationsAPIController (searchFilterService ),
200
+ openapi .NewUserOperationsAPIController (userService ),
201
+ )
202
+
203
+ r .Mount ("/v3" , apiRouter )
204
+ })
206
205
207
206
r .Get ("/readyz" , func (w http.ResponseWriter , r * http.Request ) {
208
207
w .Header ().Set ("Content-Type" , "application/json" )
@@ -325,6 +324,7 @@ func init() {
325
324
serveCmd .Flags ().StringVar (& config .FallbackProxyUrl , "fallback-proxy" , "" , "optional comma separated list of fallback upstream proxy urls" )
326
325
serveCmd .Flags ().BoolVar (& config .Dev , "dev" , false , "enables dev mode" )
327
326
serveCmd .Flags ().BoolVar (& config .DropPrivileges , "drop-privileges" , false , "drops privileges to the given user/group" )
327
+ serveCmd .Flags ().BoolVar (& config .UI , "ui" , false , "enables the web ui" )
328
328
serveCmd .Flags ().StringVar (& config .CachePrefixes , "cache-prefixes" , "/v3/files" , "url prefixes to cache" )
329
329
serveCmd .Flags ().StringVar (& config .JwtSecret , "jwt-secret" , "changeme" , "jwt secret" )
330
330
serveCmd .Flags ().StringVar (& config .JwtTokenPath , "jwt-token-path" , "~/.gorge/token" , "jwt token path" )
0 commit comments