Skip to content

Commit

Permalink
Merge pull request #517 from factly/fix/api/middleware
Browse files Browse the repository at this point in the history
add logs
  • Loading branch information
shreeharsha-factly authored May 9, 2022
2 parents c212838 + e1f1272 commit 793de79
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions api/graph/loaders/dataloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"

Expand Down Expand Up @@ -39,6 +40,7 @@ func (v values) Get(key string) interface{} {
// DataloaderMiddleware to add middleware in main
func DataloaderMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(" DataloaderMiddleware entry")
ratingloader := RatingLoader{
maxBatch: 100,
wait: 1 * time.Second,
Expand Down Expand Up @@ -363,6 +365,8 @@ func DataloaderMiddleware(next http.Handler) http.Handler {
}}

ctx := context.WithValue(r.Context(), loadersKey, v)

log.Println(" DataloaderMiddleware exit")
next.ServeHTTP(w, r.WithContext(ctx))
})
}
Expand Down
3 changes: 3 additions & 0 deletions api/graph/resolvers/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"

"gorm.io/gorm"

Expand Down Expand Up @@ -142,6 +143,8 @@ func (r *queryResolver) FeaturedCategories(ctx context.Context, featuredCount in
}

func (r *queryResolver) Categories(ctx context.Context, ids []int, spaces []int, page *int, limit *int, sortBy *string, sortOrder *string) (*models.CategoriesPaging, error) {

log.Println(" categories resolver entry")
sID, err := validator.GetSpace(ctx)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions api/graph/resolvers/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resolvers
import (
"context"
"fmt"
"log"

"github.com/factly/dega-api/config"
"github.com/factly/dega-api/graph/generated"
Expand All @@ -28,6 +29,8 @@ func (r *menuResolver) MetaFields(ctx context.Context, obj *models.Menu) (interf

func (r *queryResolver) Menu(ctx context.Context) (*models.MenusPaging, error) {

log.Println(" Menu resolver entry")

sID, err := validator.GetSpace(ctx)
if err != nil {
return nil, err
Expand Down
26 changes: 24 additions & 2 deletions api/graph/resolvers/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (r *queryResolver) Posts(ctx context.Context, spaces []int, formats *models
result := &models.PostsPaging{}
result.Nodes = make([]*models.Post, 0)

offset, pageLimit := util.Parse(page, limit)
offset, pageLimit := parse(page, limit)

tx := config.DB.Model(&models.Post{}).Where("is_page = ?", false)

Expand Down Expand Up @@ -342,7 +342,9 @@ func (r *queryResolver) Posts(ctx context.Context, spaces []int, formats *models
var total int64
tx.Where(&models.Post{
SpaceID: uint(sID),
}).Where(filterStr).Count(&total).Order(order).Offset(offset).Limit(pageLimit).Select("posts.*").Find(&result.Nodes)
}).Where(filterStr).Count(&total).Offset(offset).Limit(pageLimit).Order(order).Select("posts.*").Find(&result.Nodes)

tx.Commit()

result.Total = int(total)

Expand All @@ -354,3 +356,23 @@ func createFilters(arr []string) string {
filter = "'" + filter + "'"
return filter
}

// Parse pagination
func parse(page *int, perPage *int) (int, int) {
offset := 0 // no. of records to skip
limit := 100 // limit

if page == nil || perPage == nil {
return offset, limit
}

if *perPage > 0 && *perPage <= 100 {
limit = *perPage
}

if *page > 1 {
offset = (*page - 1) * limit
}

return offset, limit
}
3 changes: 3 additions & 0 deletions api/graph/resolvers/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"

"github.com/factly/dega-api/config"
"github.com/factly/dega-api/graph/generated"
Expand Down Expand Up @@ -74,6 +75,8 @@ func (r *spaceResolver) MetaFields(ctx context.Context, obj *models.Space) (inte

func (r *queryResolver) Space(ctx context.Context) (*models.Space, error) {

log.Println(" Space resolver entry")

sID, err := validator.GetSpace(ctx)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions api/util/cache/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"

Expand All @@ -23,6 +24,7 @@ type requestBody struct {
func CachingMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(" CachingMiddleware entry")
body := requestBody{}
bodyBytes, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(bodyBytes, &body)
Expand Down Expand Up @@ -61,6 +63,8 @@ func CachingMiddleware() func(http.Handler) http.Handler {

r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))

log.Println(" CachingMiddleware exit")
next.ServeHTTP(w, r)
})
}
Expand Down
3 changes: 3 additions & 0 deletions api/util/cache/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (myrw *CacheResponseWriter) WriteHeader(header int) {

func RespMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(" RespMiddleware entry")
// Create a response writer:
crw := &CacheResponseWriter{
ResponseWriter: w,
Expand Down Expand Up @@ -65,5 +66,7 @@ func RespMiddleware(next http.Handler) http.Handler {
if _, err = io.Copy(w, crw.buf); err != nil {
log.Printf("Failed to send out response: %v", err)
}

log.Println(" RespMiddleware exit")
})
}

0 comments on commit 793de79

Please sign in to comment.