Skip to content

Commit

Permalink
refactor server and studio
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeharsha-factly committed Jul 7, 2024
1 parent 5a6a176 commit c8115fb
Show file tree
Hide file tree
Showing 131 changed files with 1,427 additions and 1,224 deletions.
11 changes: 11 additions & 0 deletions server/config.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ NATS_USER_PASSWORD=natspassword
HUKZ_URL=http://hukz:8000

ORGANISATION_PERMISSION_ENABLED=false

#zitadel
ZITADEL_DOMAIN=develop.zitadel.cloud
ZITADEL_PROTOCOL=https
ZITADEL_PROJECT_ID=project_id

#redis
ENABLE_CACHE=true
REDIS_URL=redis:6379
REDIS_PASSWORD=redispassword
REDIS_DB=0
REDIS_CACHE_DURATION=10 # in seconds
178 changes: 178 additions & 0 deletions server/config/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package config

import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"

"github.com/factly/x/renderx"
"github.com/go-redis/redis"
"github.com/spf13/viper"
)

// GlobalCache: global cache
var GlobalCache *Cache

type Cache struct {
client redis.UniversalClient
ttl time.Duration
}

var AppPrefix = "dega-public:"

func SetupCache() {

client := redis.NewClient(&redis.Options{
Addr: viper.GetString("redis_url"),
Password: viper.GetString("redis_password"),
DB: viper.GetInt("redis_db"),
})

GlobalCache = &Cache{client: client, ttl: time.Duration(viper.GetInt("redis_cache_duration")) * time.Second}

}

func (c *Cache) Set(ctx context.Context, key string, value interface{}) error {
bytes, err := json.Marshal(value)
if err != nil {
return err
}
status := c.client.Set(AppPrefix+key, bytes, c.ttl)
return status.Err()
}

func (c *Cache) Get(ctx context.Context, key string) ([]byte, error) {
s := c.client.Get(AppPrefix + key)
if s.Err() == redis.Nil {
return nil, errors.New("entry not found in cache")
} else {
return s.Bytes()
}
}

func SaveToCache(ctx context.Context, queryStr string, data interface{}) error {
// hash query
h := md5.New()
_, _ = io.WriteString(h, queryStr)
hash := hex.EncodeToString(h.Sum(nil))

err := GlobalCache.Set(ctx, hash, data)
if err != nil {
return nil
}
return nil
}

type CacheResponseWriter struct {
http.ResponseWriter
buf *bytes.Buffer
}

// Here we are implementing a Write() function from ResponseWriter with our custom instructions.
func (myrw *CacheResponseWriter) Write(p []byte) (int, error) {
return myrw.buf.Write(p)
}

func (myrw *CacheResponseWriter) WriteHeader(header int) {
myrw.ResponseWriter.WriteHeader(header)
}

func RespMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Create a response writer:
crw := &CacheResponseWriter{
ResponseWriter: w,
buf: &bytes.Buffer{},
}

body := requestBody{}
bodyBytes, _ := io.ReadAll(r.Body)
spaceId := r.Header.Get("x-space")
err := json.Unmarshal(bodyBytes, &body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
r.Body.Close()
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))

next.ServeHTTP(crw, r)

queryString := body.Query
queryStr := strings.ReplaceAll(queryString, "\n", "")
queryStr = strings.ReplaceAll(queryStr, " ", "")

varBytes, _ := json.Marshal(body.Variables)
varString := string(varBytes)

var data interface{}
saveBytes := crw.buf.Bytes()

_ = json.Unmarshal(saveBytes, &data)

err = SaveToCache(r.Context(), fmt.Sprint(queryStr, varString, spaceId), data)
if err != nil {
log.Println(err.Error())
}

if _, err = io.Copy(w, crw.buf); err != nil {
log.Printf("Failed to send out response: %v", err)
}

})
}

type requestBody struct {
OperationName string `json:"operationName"`
Query string `json:"query"`
Variables interface{} `json:"variables"`
}

func CachingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

body := requestBody{}
spaceId := r.Header.Get("x-space")
bodyBytes, _ := io.ReadAll(r.Body)
err := json.Unmarshal(bodyBytes, &body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

// get query string from the request body
queryString := body.Query
queryStr := strings.ReplaceAll(queryString, "\n", "")
queryStr = strings.ReplaceAll(queryStr, " ", "")

varBytes, _ := json.Marshal(body.Variables)
varString := string(varBytes)

// hash query
h := md5.New()
io.WriteString(h, fmt.Sprint(queryStr, varString, spaceId))
hash := hex.EncodeToString(h.Sum(nil))

respBodyBytes, err := GlobalCache.Get(r.Context(), hash)
if err == nil {
var data interface{}
_ = json.Unmarshal(respBodyBytes, &data)
renderx.JSON(w, http.StatusOK, data)
return
}

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

next.ServeHTTP(w, r)
})
}
23 changes: 23 additions & 0 deletions server/config/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ func SetupVars() {
}
}

if CacheEnabled() {
if !viper.IsSet("redis_address") {
log.Fatal("please provide redis_address config param")
}

if !viper.IsSet("redis_password") {
log.Fatal("please provide redis_password config param")
}

if !viper.IsSet("redis_db") {
log.Fatal("please provide redis_db config param")
}

if !viper.IsSet("redis_cache_duration") {
log.Fatal("please provide redis_cache_duration config param")
}

}

}

func SearchEnabled() bool {
Expand All @@ -84,3 +103,7 @@ func SearchEnabled() bool {
func Sqlite() bool {
return viper.IsSet("use_sqlite") && viper.GetBool("use_sqlite") && false
}

func CacheEnabled() bool {
return viper.IsSet("enable_cache") && viper.GetBool("enable_cache")
}
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gavv/httpexpect/v2 v2.1.0
github.com/go-chi/chi v4.1.2+incompatible
github.com/go-chi/cors v1.2.1
github.com/go-redis/redis v6.15.9+incompatible
github.com/google/uuid v1.5.0
github.com/gorilla/feeds v1.1.1
github.com/jinzhu/gorm v1.9.16
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.6.1 h1:W6TRDXt4WcWp4c4nf/G+6BkGdhiIo0k417gfr+V6u4I=
github.com/go-playground/validator/v10 v10.6.1/go.mod h1:xm76BBt941f7yWdGnI2DVPFFg1UK3YY04qifoXU3lOk=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
Expand Down
29 changes: 0 additions & 29 deletions server/service/core/action/author/all.go

This file was deleted.

17 changes: 5 additions & 12 deletions server/service/core/action/author/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/factly/x/loggerx"
"github.com/factly/x/paginationx"
"github.com/factly/x/renderx"
"github.com/spf13/viper"
)

// list response
Expand All @@ -31,16 +32,8 @@ type paging struct {
// @Param page query string false "page number"
// @Success 200 {object} paging
// @Router /core/authors [get]
func list(w http.ResponseWriter, r *http.Request) {

sID, err := util.GetSpace(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}

oID, err := util.GetOrganisation(r.Context())
func List(w http.ResponseWriter, r *http.Request) {
authCtx, err := util.GetAuthCtx(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
Expand All @@ -58,7 +51,7 @@ func list(w http.ResponseWriter, r *http.Request) {
offset, limit := paginationx.Parse(r.URL.Query())

// get total authors
err = config.DB.Model(&model.SpaceUser{}).Where("space_id = ?", sID).Count(&result.Total).Limit(limit).Offset(offset).Find(&spaceUsers).Error
err = config.DB.Model(&model.SpaceUser{}).Where("space_id = ?", authCtx.SpaceID).Count(&result.Total).Limit(limit).Offset(offset).Find(&spaceUsers).Error

if err != nil {
loggerx.Error(err)
Expand All @@ -73,7 +66,7 @@ func list(w http.ResponseWriter, r *http.Request) {
}

// get users details from zitadel
zitadelUsers, _ := zitadel.GetOrganisationUsers(r.Header.Get("Authorization"), oID, uIDs)
zitadelUsers, _ := zitadel.GetOrganisationUsers(viper.GetString("ZITADEL_PERSONAL_ACCESS_TOKEN"), authCtx.OrganisationID, uIDs)

for _, zitadelUser := range zitadelUsers {
authors = append(authors, model.Author{
Expand Down
46 changes: 0 additions & 46 deletions server/service/core/action/author/mapper.go

This file was deleted.

2 changes: 1 addition & 1 deletion server/service/core/action/author/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func Router() chi.Router {
r := chi.NewRouter()

r.Get("/", list)
r.Get("/", List)
return r

}
Loading

0 comments on commit c8115fb

Please sign in to comment.