Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dadav committed Jul 12, 2024
1 parent 46d6e53 commit aa60220
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 70 deletions.
102 changes: 44 additions & 58 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/jwtauth/v5"
"github.com/go-chi/stampede"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -128,7 +127,7 @@ You can also enable the caching functionality to speed things up.`,
r := chi.NewRouter()

// Logger should come before any middleware that modifies the response
// r.Use(middleware.Logger)
r.Use(middleware.Logger)
// Recoverer should also be pretty high in the middleware stack
r.Use(middleware.Recoverer)
r.Use(middleware.RealIP)
Expand All @@ -140,22 +139,6 @@ You can also enable the caching functionality to speed things up.`,
AllowCredentials: false,
MaxAge: 300,
}))

if !config.Dev {
tokenAuth := jwtauth.New("HS256", []byte(config.JwtSecret), nil)
r.Use(customMiddleware.AuthMiddleware(tokenAuth, func(r *http.Request) bool {
// Everything but GET is protected and requires a jwt token
return r.Method != "GET" && strings.HasPrefix(r.URL.Path, "/v3")
}))

_, tokenString, _ := tokenAuth.Encode(map[string]interface{}{"user": "admin"})
err = os.WriteFile(config.JwtTokenPath, []byte(tokenString), 0600)
if err != nil {
log.Log.Fatal(err)
}
log.Log.Infof("JWT token was written to %s", config.JwtTokenPath)
}

if !config.NoCache {
customKeyFunc := func(r *http.Request) uint64 {
token := r.Header.Get("Authorization")
Expand All @@ -165,50 +148,53 @@ You can also enable the caching functionality to speed things up.`,
r.Use(cachedMiddleware)
}

if config.FallbackProxyUrl != "" {
proxies := strings.Split(config.FallbackProxyUrl, ",")
slices.Reverse(proxies)

for _, proxy := range proxies {
r.Use(customMiddleware.ProxyFallback(proxy, func(status int) bool {
return status == http.StatusNotFound
},
func(r *http.Response) {
if config.ImportProxiedReleases && strings.HasPrefix(r.Request.URL.Path, "/v3/files/") && r.StatusCode == http.StatusOK {
body, err := io.ReadAll(r.Body)
if err != nil {
log.Log.Error(err)
return
}
if config.UI {
r.Group(func(r chi.Router) {
g := handlers.NewGorgeService()
g.AddRoutesToRouter(r)
})
}

// restore the body
r.Body = io.NopCloser(bytes.NewBuffer(body))
r.Group(func(r chi.Router) {
if config.FallbackProxyUrl != "" {
proxies := strings.Split(config.FallbackProxyUrl, ",")
slices.Reverse(proxies)

release, err := backend.ConfiguredBackend.AddRelease(body)
if err != nil {
log.Log.Error(err)
return
}
log.Log.Infof("Imported release %s\n", release.Slug)
}
for _, proxy := range proxies {
r.Use(customMiddleware.ProxyFallback(proxy, func(status int) bool {
return status == http.StatusNotFound
},
))
func(r *http.Response) {
if config.ImportProxiedReleases && strings.HasPrefix(r.Request.URL.Path, "/v3/files/") && r.StatusCode == http.StatusOK {
body, err := io.ReadAll(r.Body)
if err != nil {
log.Log.Error(err)
return
}

// restore the body
r.Body = io.NopCloser(bytes.NewBuffer(body))

release, err := backend.ConfiguredBackend.AddRelease(body)
if err != nil {
log.Log.Error(err)
return
}
log.Log.Infof("Imported release %s\n", release.Slug)
}
},
))
}
}
}

apiRouter := openapi.NewRouter(
openapi.NewModuleOperationsAPIController(moduleService),
openapi.NewReleaseOperationsAPIController(releaseService),
openapi.NewSearchFilterOperationsAPIController(searchFilterService),
openapi.NewUserOperationsAPIController(userService),
)

r.Mount("/v3", apiRouter)

if config.UI {
g := handlers.NewGorgeService()
r.Mount("/", g.Router)
}
apiRouter := openapi.NewRouter(
openapi.NewModuleOperationsAPIController(moduleService),
openapi.NewReleaseOperationsAPIController(releaseService),
openapi.NewSearchFilterOperationsAPIController(searchFilterService),
openapi.NewUserOperationsAPIController(userService),
)

r.Mount("/v3", apiRouter)
})

r.Get("/readyz", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
Expand Down
20 changes: 8 additions & 12 deletions internal/v3/ui/handlers/default.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package handlers

import (
"context"
"net/http"
"strings"

"github.com/a-h/templ"
"github.com/dadav/gorge/internal/log"
"github.com/dadav/gorge/internal/v3/backend"
"github.com/dadav/gorge/internal/v3/ui"
Expand All @@ -16,21 +16,17 @@ type GorgeService struct {
}

func NewGorgeService() *GorgeService {
r := chi.NewRouter()

gs := &GorgeService{
Router: r,
}
return &GorgeService{}
}

r.HandleFunc("/", gs.IndexHandler)
r.HandleFunc("/search", gs.SearchHandler)
func (g *GorgeService) AddRoutesToRouter(r chi.Router) {
r.HandleFunc("/", g.IndexHandler)
r.HandleFunc("/search", g.SearchHandler)
r.Handle("/assets/*", ui.HandleAssets())

return gs
}

func (g *GorgeService) IndexHandler(w http.ResponseWriter, r *http.Request) {
ui.Index().Render(context.Background(), w)
templ.Handler(ui.Index()).ServeHTTP(w, r)
}

func (g *GorgeService) SearchHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -44,7 +40,7 @@ func (g *GorgeService) SearchHandler(w http.ResponseWriter, r *http.Request) {

for _, module := range modules {
if strings.Contains(module.Name, query) || strings.Contains(module.Owner.Username, query) || strings.Contains(module.CurrentRelease.Version, query) {
ui.ModuleToTableRow(module.Name, module.Owner.Username, module.CurrentRelease.Version).Render(context.Background(), w)
templ.Handler(ui.ModuleToTableRow(module.Name, module.Owner.Username, module.CurrentRelease.Version)).ServeHTTP(w, r)
}
}
}

0 comments on commit aa60220

Please sign in to comment.