Skip to content

Commit

Permalink
Fixes and defaults from Config
Browse files Browse the repository at this point in the history
  • Loading branch information
henrod committed May 8, 2017
1 parent 64f2008 commit 6f880bf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
17 changes: 17 additions & 0 deletions api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ type App struct {
Server *http.Server
Cache *cache.Cache
OffersCacheMaxAge time.Duration
Pagination *Pagination
}

//Pagination holds the page size (limit) and the offset that is the page number
type Pagination struct {
Limit uint64
Offset uint64
}

//NewApp ctor
Expand Down Expand Up @@ -214,11 +221,21 @@ func (a *App) configureApp() error {
a.configureSentry()

a.MaxAge = a.Config.GetInt64("cache.maxAgeSeconds")
a.configurePagination()
a.configureServer()
a.configureCache()
return nil
}

func (a *App) configurePagination() {
limit := uint64(a.Config.GetInt64("pagination.limit"))
offset := uint64(a.Config.GetInt64("pagination.offset"))
a.Pagination = &Pagination{
Limit: limit,
Offset: offset,
}
}

func (a *App) configureCache() {
maxAge := time.Duration(a.Config.GetInt64("offersCache.maxAgeSeconds")) * time.Second
cleanupInterval := time.Duration(a.Config.GetInt64("offersCache.cleanupInterval")) * time.Second
Expand Down
34 changes: 18 additions & 16 deletions api/offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,27 @@ func (g *OfferHandler) list(w http.ResponseWriter, r *http.Request) {

var limit uint64
if limitStr == "" {
limit = 50
} else if limit, err = strconv.ParseUint(limitStr, 10, 64); err != nil {
logger.WithError(err).Error("List game offers failed.")
g.App.HandleError(w, http.StatusBadRequest, "The limit parameter must be an uint.", err)
return
limit = g.App.Pagination.Limit
} else {
var err error
limit, err = strconv.ParseUint(limitStr, 10, 64)
if err != nil {
logger.WithError(err).Error("List game offers failed.")
g.App.HandleError(w, http.StatusBadRequest, "The limit parameter must be an uint.", err)
return
}
}

var offset uint64
if offsetStr == "" {
offset = 0
} else if offset, err = strconv.ParseUint(offsetStr, 10, 64); err != nil {
logger.WithError(err).Error("List game offers failed.")
g.App.HandleError(w, http.StatusBadRequest, "The offset parameter must be an uint.", err)
return
offset = g.App.Pagination.Offset
} else {
offset, err = strconv.ParseUint(offsetStr, 10, 64)
if err != nil {
logger.WithError(err).Error("List game offers failed.")
g.App.HandleError(w, http.StatusBadRequest, "The offset parameter must be an uint.", err)
return
}
}

var offers []*models.Offer
Expand All @@ -221,10 +228,5 @@ func (g *OfferHandler) list(w http.ResponseWriter, r *http.Request) {
}

bts, _ := json.Marshal(responseObj)
status := http.StatusOK
if len(offers) == 0 {
status = http.StatusNoContent
}

WriteBytes(w, status, bts)
WriteBytes(w, http.StatusOK, bts)
}
4 changes: 2 additions & 2 deletions api/offer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ var _ = Describe("Offer Template Handler", func() {
app.Router.ServeHTTP(recorder, request)
Expect(recorder.Header().Get("Content-Type")).To(Equal("application/json"))

Expect(recorder.Code).To(Equal(http.StatusNoContent))
Expect(recorder.Code).To(Equal(http.StatusOK))
var obj map[string]interface{}
err := json.Unmarshal(recorder.Body.Bytes(), &obj)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -1224,7 +1224,7 @@ var _ = Describe("Offer Template Handler", func() {

app.Router.ServeHTTP(recorder, request)
Expect(recorder.Header().Get("Content-Type")).To(Equal("application/json"))
Expect(recorder.Code).To(Equal(http.StatusNoContent))
Expect(recorder.Code).To(Equal(http.StatusOK))

var obj map[string]interface{}
err := json.Unmarshal(recorder.Body.Bytes(), &obj)
Expand Down
3 changes: 3 additions & 0 deletions config/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ offersCache:
basicauth:
username: user
password: pass
pagination:
limit: 50
offset: 0
3 changes: 3 additions & 0 deletions config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ cache:
offersCache:
maxAgeSeconds: 300
cleanupInterval: 30
pagination:
limit: 50
offset: 0

0 comments on commit 6f880bf

Please sign in to comment.