Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ChaosCenter]: Migrating graphql-server from Gorilla mux to Gin (#3923) #3923

Merged
merged 13 commits into from
Apr 7, 2023
Merged
4 changes: 2 additions & 2 deletions litmus-portal/graphql-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ require (
github.com/99designs/gqlgen v0.11.3
github.com/argoproj/argo-workflows/v3 v3.3.1
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.7.7
Copy link
Member

@imrajdas imrajdas Apr 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.7.7 is released in 2021. Can we use the latest one? If no major code change is required

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imrajdas let's take it in a different PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.9.0 is the latest version.
But higher than 1.7.7, We need to upgrade the version of Go 1.17.
So I just use that version like the authentication server in litmusChaos

스크린샷 2023-04-07 오후 9 25 40

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, lets do it in other PR

github.com/go-git/go-git/v5 v5.4.2
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/jinzhu/copier v0.3.5
github.com/kelseyhightower/envconfig v1.4.0
Expand All @@ -20,7 +21,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/common v0.32.1
github.com/rs/cors v1.8.2
github.com/sirupsen/logrus v1.8.1
github.com/tidwall/gjson v1.14.0
github.com/tidwall/sjson v1.2.4
Expand Down
30 changes: 27 additions & 3 deletions litmus-portal/graphql-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,13 @@ github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2H
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 h1:Mn26/9ZMNWSw9C9ERFA1PUxfmGpolnw2v0bKOREu5ew=
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I=
github.com/gin-contrib/cors v1.3.1 h1:doAsuITavI4IOcd0Y19U4B+O0dNWihRyX//nn4sEmgA=
github.com/gin-contrib/cors v1.3.1/go.mod h1:jjEJ4268OPZUcU7k9Pm653S7lXUGcqMADzFA61xsmDk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
github.com/gizak/termui/v3 v3.1.0/go.mod h1:bXQEBkJpzxUAKf0+xq9MSWAvWZlE7c+aidmyFlkYTrY=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
Expand Down Expand Up @@ -716,6 +723,16 @@ github.com/go-openapi/validate v0.19.15/go.mod h1:tbn/fdOwYHgrhPBzidZfJC2MIVvs9G
github.com/go-openapi/validate v0.20.1/go.mod h1:b60iJT+xNNLfaQJUqLI7946tYiFEOuE9E4k54HpKcJ0=
github.com/go-openapi/validate v0.20.3/go.mod h1:goDdqVGiigM3jChcrYJxD2joalke3ZXeftD16byIjA4=
github.com/go-ozzo/ozzo-validation v3.5.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
Expand Down Expand Up @@ -962,7 +979,6 @@ github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z
github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
Expand Down Expand Up @@ -1204,6 +1220,9 @@ github.com/labstack/gommon v0.2.7/go.mod h1:/tj9csK2iPSBvn+3NLM9e52usepMtrd5ilFY
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/leanovate/gopter v0.2.4/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
Expand Down Expand Up @@ -1276,6 +1295,7 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
Expand Down Expand Up @@ -1604,8 +1624,6 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rubenv/sql-migrate v0.0.0-20191025130928-9355dd04f4b3/go.mod h1:WS0rl9eEliYI8DPnr3TOwz4439pay+qNgzJoVya/DmY=
github.com/rubenv/sql-migrate v0.0.0-20200212082348-64f95ea68aa3/go.mod h1:rtQlpHw+eR6UrqaS3kX1VYeaCxzCVdimDS7g5Ln4pPc=
Expand Down Expand Up @@ -1755,7 +1773,11 @@ github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMW
github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ultraware/funlen v0.0.1/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
github.com/ultraware/funlen v0.0.2/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
Expand Down Expand Up @@ -2642,6 +2664,8 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy
gopkg.in/fsnotify/fsnotify.v1 v1.4.7/go.mod h1:Fyux9zXlo4rWoMSIzpn9fDAYjalPqJ/K1qJ27s+7ltE=
gopkg.in/gcfg.v1 v1.2.0/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/go-playground/webhooks.v5 v5.17.0/go.mod h1:LZbya/qLVdbqDR1aKrGuWV6qbia2zCYSR5dpom2SInQ=
gopkg.in/gorp.v1 v1.7.2/go.mod h1:Wo3h+DBQZIxATwftsglhdD/62zRFPhGhTiu5jUJmCaw=
gopkg.in/imdario/mergo.v0 v0.3.7/go.mod h1:9qPP6AGrlC1G2PTNXko614FwGZvorN7MiBU0Eppok+U=
Expand Down
42 changes: 22 additions & 20 deletions litmus-portal/graphql-server/pkg/authorization/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

Expand All @@ -15,50 +16,51 @@ const (
CookieName = "litmus-cc-token"
)

func Middleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Middleware verifies jwt and checks if user has enough privilege to access route (no roles' info needed)
func Middleware(handler http.Handler) gin.HandlerFunc {
return func(c *gin.Context) {
jwt := ""
auth, err := r.Cookie(CookieName)
auth, err := c.Request.Cookie(CookieName)
if err == nil {
jwt = auth.Value
} else if r.Header.Get("Authorization") != "" {
jwt = r.Header.Get("Authorization")
} else if c.Request.Header.Get("Authorization") != "" {
jwt = c.Request.Header.Get("Authorization")
}

ctx := context.WithValue(r.Context(), AuthKey, jwt)
r = r.WithContext(ctx)
handler.ServeHTTP(w, r)
})
ctx := context.WithValue(c.Request.Context(), AuthKey, jwt)
c.Request = c.Request.WithContext(ctx)
handler.ServeHTTP(c.Writer, c.Request)
}
}

// RestMiddlewareWithRole verifies jwt and checks if user has enough privilege to access route
func RestMiddlewareWithRole(handler http.Handler, roles []string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
func RestMiddlewareWithRole(handler gin.HandlerFunc, roles []string) gin.HandlerFunc {
return func(c *gin.Context) {
jwt := ""
auth, err := r.Cookie(CookieName)
auth, err := c.Request.Cookie(CookieName)
if err == nil {
jwt = auth.Value
} else if r.Header.Get("Authorization") != "" {
jwt = r.Header.Get("Authorization")
} else if c.Request.Header.Get("Authorization") != "" {
jwt = c.Request.Header.Get("Authorization")
}
user, err := UserValidateJWT(jwt)
if err != nil {
logrus.WithError(err).Error("Invalid Auth Cookie")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Error verifying JWT token: " + err.Error()))
c.Writer.WriteHeader(http.StatusUnauthorized)
c.Writer.Write([]byte("Error verifying JWT token: " + err.Error()))
return
}
if len(roles) == 0 {
handler.ServeHTTP(w, r)
handler(c)
return
}
for _, role := range roles {
if role == user["role"] {
handler.ServeHTTP(w, r)
handler(c)
return
}
}
w.WriteHeader(http.StatusUnauthorized)
c.Writer.WriteHeader(http.StatusUnauthorized)
return
})
}
}
40 changes: 22 additions & 18 deletions litmus-portal/graphql-server/pkg/chaoshub/chaoshub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/jinzhu/copier"
"go.mongodb.org/mongo-driver/bson"

"github.com/litmuschaos/litmus/litmus-portal/graphql-server/graph/model"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaoshub/handler"
chaosHubOps "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/chaoshub/ops"
dbOperationsChaosHub "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/chaoshub"
dbSchemaChaosHub "github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/database/mongodb/chaoshub"
"go.mongodb.org/mongo-driver/bson"
)

const (
Expand Down Expand Up @@ -515,31 +513,37 @@ func DeleteChaosHub(ctx context.Context, hubID string, projectID string) (bool,
}

// GetIconHandler ...
var GetIconHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var img *os.File
var err error
var responseStatusCode int
if strings.ToLower(vars["ChartName"]) == "predefined" {
img, err = os.Open("/tmp/version/" + vars["ProjectID"] + "/" + vars["HubName"] + "/workflows/icons/" + vars["IconName"])
var GetIconHandler = gin.HandlerFunc(func(c *gin.Context) {
var (
projectID = c.Param("ProjectID")
hubName = c.Param("HubName")
chartName = c.Param("ChartName")
iconName = c.Param("IconName")
img *os.File
err error
responseStatusCode int
)

if strings.ToLower(chartName) == "predefined" {
img, err = os.Open("/tmp/version/" + projectID + "/" + hubName + "/workflows/icons/" + iconName)
namkyu1999 marked this conversation as resolved.
Show resolved Hide resolved
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
responseStatusCode = 200
if err != nil {
responseStatusCode = 500
fmt.Fprint(w, "icon cannot be fetched, err : "+err.Error())
fmt.Fprint(c.Writer, "icon cannot be fetched, err : "+err.Error())
namkyu1999 marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
img, err = os.Open("/tmp/version/" + vars["ProjectID"] + "/" + vars["HubName"] + "/charts/" + vars["ChartName"] + "/icons/" + vars["IconName"])
img, err = os.Open("/tmp/version/" + projectID + "/" + hubName + "/charts/" + chartName + "/icons/" + iconName)
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
responseStatusCode = 200
if err != nil {
responseStatusCode = 500
fmt.Fprint(w, "icon cannot be fetched, err : "+err.Error())
fmt.Fprint(c.Writer, "icon cannot be fetched, err : "+err.Error())
}
}
defer img.Close()
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(responseStatusCode)
w.Header().Set("Content-Type", "image/png") // <-- set the content-type header
io.Copy(w, img)
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.WriteHeader(responseStatusCode)
c.Writer.Header().Set("Content-Type", "image/png") // <-- set the content-type header
io.Copy(c.Writer, img)
})

// RecurringHubSync is used for syncing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/gin-gonic/gin"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
"github.com/sirupsen/logrus"
)
Expand All @@ -12,16 +13,17 @@ type WorkflowHelperImageVersion struct {
Version string `json:"version"`
}

func WorkflowHelperImageVersionHandler(w http.ResponseWriter, r *http.Request) {
// WorkflowHelperImageVersionHandler is used to provide workflow helper's image version
func WorkflowHelperImageVersionHandler(c *gin.Context) {
versionDetails := utils.Config.WorkflowHelperImageVersion
version := WorkflowHelperImageVersion{Version: versionDetails}
versionByte, err := json.Marshal(version)
if err != nil {
logrus.Error(err)
utils.WriteHeaders(&w, http.StatusBadRequest)
utils.WriteHeaders(&c.Writer, http.StatusBadRequest)
return
}

utils.WriteHeaders(&w, http.StatusOK)
w.Write(versionByte)
utils.WriteHeaders(&c.Writer, http.StatusOK)
c.Writer.Write(versionByte)
}
23 changes: 9 additions & 14 deletions litmus-portal/graphql-server/pkg/handlers/file_handler.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package handlers

import (
"net/http"
"strings"

"github.com/gorilla/mux"
"github.com/gin-gonic/gin"
"github.com/litmuschaos/litmus/litmus-portal/graphql-server/pkg/cluster"
"github.com/sirupsen/logrus"

"github.com/litmuschaos/litmus/litmus-portal/graphql-server/utils"
"github.com/sirupsen/logrus"
)

// FileHandler dynamically generates the manifest file and sends it as a response
func FileHandler(w http.ResponseWriter, r *http.Request) {
var (
vars = mux.Vars(r)
token = vars["key"]
)

func FileHandler(c *gin.Context) {
token := strings.TrimSuffix(c.Param("key"), ".yaml")
response, statusCode, err := cluster.GetManifest(token)
if err != nil {
logrus.WithError(err).Error("error while generating manifest file")
utils.WriteHeaders(&w, statusCode)
w.Write([]byte(err.Error()))
utils.WriteHeaders(&c.Writer, statusCode)
c.Writer.Write([]byte(err.Error()))
}

utils.WriteHeaders(&w, statusCode)
w.Write(response)
utils.WriteHeaders(&c.Writer, statusCode)
c.Writer.Write(response)
}
15 changes: 15 additions & 0 deletions litmus-portal/graphql-server/pkg/handlers/playground_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package handlers
namkyu1999 marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gin-gonic/gin"
)

// PlaygroundHandler returns GraphQL playground ui
func PlaygroundHandler() gin.HandlerFunc {
h := playground.Handler("GraphQL playground", "/query")

return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
Loading