• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

foomo / contentserver / 19672837588

25 Nov 2025 02:24PM UTC coverage: 41.746% (+0.08%) from 41.662%
19672837588

Pull #67

github

web-flow
Merge a11a6abee into e7e5d09f5
Pull Request #67: Add Multi-Cloud Blob Storage Support (AWS S3, Azure, GCS)

166 of 366 new or added lines in 11 files covered. (45.36%)

3 existing lines in 2 files now uncovered.

875 of 2096 relevant lines covered (41.75%)

23682.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pkg/handler/http.go
1
package handler
2

3
import (
4
        "context"
5
        "io"
6
        "net/http"
7
        "strings"
8
        "time"
9

10
        "github.com/foomo/contentserver/pkg/metrics"
11
        "github.com/foomo/contentserver/pkg/repo"
12
        "github.com/foomo/contentserver/requests"
13
        "github.com/foomo/contentserver/responses"
14
        httputils "github.com/foomo/keel/utils/net/http"
15
        "github.com/pkg/errors"
16
        "go.uber.org/zap"
17
)
18

19
type (
20
        HTTP struct {
21
                l        *zap.Logger
22
                repo     *repo.Repo
23
                basePath string
24
        }
25
        HTTPOption func(*HTTP)
26
)
27

28
// ------------------------------------------------------------------------------------------------
29
// ~ Constructor
30
// ------------------------------------------------------------------------------------------------
31

32
// NewHTTP returns a shiny new web server
33
func NewHTTP(l *zap.Logger, repo *repo.Repo, opts ...HTTPOption) http.Handler {
×
34
        inst := &HTTP{
×
35
                l:        l.Named("http"),
×
36
                basePath: "/contentserver",
×
37
                repo:     repo,
×
38
        }
×
39

×
40
        for _, opt := range opts {
×
41
                opt(inst)
×
42
        }
×
43

44
        return inst
×
45
}
46

47
// ------------------------------------------------------------------------------------------------
48
// ~ Options
49
// ------------------------------------------------------------------------------------------------
50

51
func WithBasePath(v string) HTTPOption {
×
52
        return func(o *HTTP) {
×
53
                o.basePath = v
×
54
        }
×
55
}
56

57
// ------------------------------------------------------------------------------------------------
58
// ~ Public methods
59
// ------------------------------------------------------------------------------------------------
60

61
func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
×
62
        if r.Method != http.MethodPost {
×
63
                httputils.ServerError(h.l, w, r, http.StatusMethodNotAllowed, errors.New("method not allowed"))
×
64
                return
×
65
        }
×
66
        if r.Body == nil {
×
67
                httputils.BadRequestServerError(h.l, w, r, errors.New("empty request body"))
×
68
                return
×
69
        }
×
70

71
        bytes, err := io.ReadAll(r.Body)
×
72
        if err != nil {
×
73
                httputils.BadRequestServerError(h.l, w, r, errors.Wrap(err, "failed to read incoming request"))
×
74
                return
×
75
        }
×
76

77
        route := Route(strings.TrimPrefix(r.URL.Path, h.basePath+"/"))
×
78
        if route == RouteGetRepo {
×
79
                w.Header().Set("Content-Type", "application/json")
×
NEW
80
                if err := h.repo.WriteRepoBytes(r.Context(), w); err != nil {
×
NEW
81
                        h.l.Error("failed to write repo bytes", zap.Error(err))
×
NEW
82
                        http.Error(w, "failed to get repo", http.StatusInternalServerError)
×
NEW
83
                }
×
84
                return
×
85
        }
86

NEW
87
        reply, errReply := h.handleRequest(r.Context(), h.repo, route, bytes, "webserver")
×
88
        if errReply != nil {
×
89
                http.Error(w, errReply.Error(), http.StatusInternalServerError)
×
90
                return
×
91
        }
×
92
        _, _ = w.Write(reply)
×
93
}
94

95
// ------------------------------------------------------------------------------------------------
96
// ~ Private methods
97
// ------------------------------------------------------------------------------------------------
98

NEW
99
func (h *HTTP) handleRequest(ctx context.Context, r *repo.Repo, route Route, jsonBytes []byte, source string) ([]byte, error) {
×
100
        start := time.Now()
×
101

×
NEW
102
        reply, err := h.executeRequest(ctx, r, route, jsonBytes, source)
×
103
        result := "success"
×
104
        if err != nil {
×
105
                result = "error"
×
106
        }
×
107

108
        metrics.ServiceRequestCounter.WithLabelValues(string(route), result, source).Inc()
×
109
        metrics.ServiceRequestDuration.WithLabelValues(string(route), result, source).Observe(time.Since(start).Seconds())
×
110

×
111
        return reply, err
×
112
}
113

NEW
114
func (h *HTTP) executeRequest(ctx context.Context, r *repo.Repo, route Route, jsonBytes []byte, source string) (replyBytes []byte, err error) {
×
115
        var (
×
116
                reply             interface{}
×
117
                apiErr            error
×
118
                jsonErr           error
×
119
                processIfJSONIsOk = func(err error, processingFunc func()) {
×
120
                        if err != nil {
×
121
                                jsonErr = err
×
122
                                return
×
123
                        }
×
124
                        processingFunc()
×
125
                }
126
        )
127
        metrics.ContentRequestCounter.WithLabelValues(source).Inc()
×
128

×
129
        // handle and process
×
130
        switch route {
×
131
        // case HandlerGetRepo: // This case is handled prior to handleRequest being called.
132
        // since the resulting bytes are written directly in to the http.ResponseWriter / net.Connection
133
        case RouteGetURIs:
×
134
                getURIRequest := &requests.URIs{}
×
135
                processIfJSONIsOk(json.Unmarshal(jsonBytes, &getURIRequest), func() {
×
136
                        reply = r.GetURIs(getURIRequest.Dimension, getURIRequest.IDs)
×
137
                })
×
138
        case RouteGetContent:
×
139
                contentRequest := &requests.Content{}
×
140
                processIfJSONIsOk(json.Unmarshal(jsonBytes, &contentRequest), func() {
×
141
                        reply, apiErr = r.GetContent(contentRequest)
×
142
                })
×
143
        case RouteGetNodes:
×
144
                nodesRequest := &requests.Nodes{}
×
145
                processIfJSONIsOk(json.Unmarshal(jsonBytes, &nodesRequest), func() {
×
146
                        reply = r.GetNodes(nodesRequest)
×
147
                })
×
148
        case RouteUpdate:
×
149
                updateRequest := &requests.Update{}
×
150
                processIfJSONIsOk(json.Unmarshal(jsonBytes, &updateRequest), func() {
×
NEW
151
                        reply = r.Update(ctx)
×
152
                })
×
153
        default:
×
154
                reply = responses.NewError(1, "unknown route: "+string(route))
×
155
        }
156

157
        // error handling
158
        if jsonErr != nil {
×
159
                h.l.Error("could not read incoming json", zap.Error(jsonErr))
×
160
                reply = responses.NewError(2, "could not read incoming json "+jsonErr.Error())
×
161
        } else if apiErr != nil {
×
162
                h.l.Error("an API error occurred", zap.Error(apiErr))
×
163
                reply = responses.NewError(3, "internal error "+apiErr.Error())
×
164
        }
×
165

166
        return h.encodeReply(reply)
×
167
}
168

169
// encodeReply takes an interface and encodes it as JSON
170
// it returns the resulting JSON and a marshalling error
171
func (h *HTTP) encodeReply(reply interface{}) (bytes []byte, err error) {
×
172
        bytes, err = json.Marshal(map[string]interface{}{
×
173
                "reply": reply,
×
174
        })
×
175
        if err != nil {
×
176
                h.l.Error("could not encode reply", zap.Error(err))
×
177
        }
×
178
        return
×
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc