Skip to content

Commit

Permalink
feat: Add more code
Browse files Browse the repository at this point in the history
  • Loading branch information
dadav committed Feb 26, 2024
1 parent 77d89d5 commit 7d3c1ec
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
9 changes: 8 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
backend "github.com/dadav/gorge/internal/backend"
config "github.com/dadav/gorge/internal/config"
log "github.com/dadav/gorge/internal/log"
middleware "github.com/dadav/gorge/internal/middleware"
openapi "github.com/dadav/gorge/pkg/gen/v3/openapi"
"github.com/go-chi/chi/v5"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -53,15 +55,20 @@ to quickly create a Cobra application.`,
releaseService := v3.NewReleaseOperationsApi()
searchFilterService := v3.NewSearchFilterOperationsApi()
userService := v3.NewUserOperationsApi()

r := chi.NewRouter()
r.Use(middleware.RequireUserAgent)
handler := openapi.NewRouter(
openapi.NewModuleOperationsAPIController(moduleService),
openapi.NewReleaseOperationsAPIController(releaseService),
openapi.NewSearchFilterOperationsAPIController(searchFilterService),
openapi.NewUserOperationsAPIController(userService),
)

r.Mount("/", handler)

log.Log.Infof("Listen on %s:%d", config.Bind, config.Port)
log.Log.Panic(http.ListenAndServe(fmt.Sprintf("%s:%d", config.Bind, config.Port), handler))
log.Log.Panic(http.ListenAndServe(fmt.Sprintf("%s:%d", config.Bind, config.Port), r))
} else {
log.Log.Panicf("%s version not supported", config.ApiVersion)
}
Expand Down
52 changes: 47 additions & 5 deletions internal/api/v3/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"strings"

"github.com/dadav/gorge/internal/backend"
Expand Down Expand Up @@ -94,12 +95,53 @@ func (s *ModuleOperationsApi) GetModules(ctx context.Context, limit int32, offse
allModules := backend.ConfiguredBackend.GetAllModules()

for _, m := range allModules {
if query != "" && (strings.Contains(m.Slug, query) || strings.Contains(m.Owner.Slug, query)) {
filtered = append(filtered, *m)
continue
var filterMatched, filterSet bool
if query != "" {
filterSet = true
filterMatched = strings.Contains(m.Slug, query) || strings.Contains(m.Owner.Slug, query)
}

if tag != "" {
filterSet = true
filterMatched = slices.Contains(m.CurrentRelease.Tags, tag)
}

if owner != "" {
filterSet = true
filterMatched = m.Owner.Username == owner
}

if withTasks {
filterSet = true
filterMatched = len(m.CurrentRelease.Tasks) > 0
}

if withPlans {
filterSet = true
filterMatched = len(m.CurrentRelease.Plans) > 0
}

if withPdk {
filterSet = true
filterMatched = m.CurrentRelease.Pdk
}

if premium {
filterSet = true
filterMatched = m.Premium
}

if excludePremium {
filterSet = true
filterMatched = !m.Premium
}

if len(endorsements) > 0 {
filterSet = true
filterMatched = m.Endorsement != nil && slices.Contains(endorsements, *m.Endorsement)
}

if query == "" && tag == "" && owner == "" {
if !filterSet || filterMatched {
filtered = append(filtered, *m)
}
}
Expand All @@ -126,7 +168,7 @@ func (s *ModuleOperationsApi) GetModules(ctx context.Context, limit int32, offse

return gen.Response(http.StatusOK, gen.GetModules200Response{
Pagination: gen.GetModules200ResponsePagination{
Limit: 20,
Limit: limit,
Offset: offset,
First: &firstInf,
Current: &currentInf,
Expand Down
33 changes: 33 additions & 0 deletions internal/middleware/useragent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package middleware

import (
"encoding/json"
"net/http"
)

type UserAgentNotSetResponse struct {
Message string `json:"message,omitempty"`
Errors []string `json:"errors,omitempty"`
}

func RequireUserAgent(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userAgent := r.Header.Get("User-Agent")
if userAgent == "" {
errorResponse := UserAgentNotSetResponse{
Message: "User-Agent header is missing",
Errors: []string{"User-Agent must have some value"},
}
jsonError, err := json.Marshal(errorResponse)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write(jsonError)
return
}
next.ServeHTTP(w, r)
})
}

0 comments on commit 7d3c1ec

Please sign in to comment.