diff --git a/routes/proxy/proxy.go b/routes/proxy/proxy.go new file mode 100644 index 0000000..2eff5c4 --- /dev/null +++ b/routes/proxy/proxy.go @@ -0,0 +1,31 @@ +package proxy + +import ( + b64 "encoding/base64" + "net/http" + + "github.com/gin-gonic/gin" +) + +func Routes(route *gin.Engine) { + auth := route.Group("/proxy") + + auth.GET("/img/:url", func(context *gin.Context) { + decodedUrl, err := b64.StdEncoding.DecodeString(context.Param("url")) + if err != nil { + context.Error(err) + } + + imageResp, err := http.Get(string(decodedUrl)) + if err != nil { + context.Error(err) + } + + reader := imageResp.Body + contentLength := imageResp.ContentLength + contentType := imageResp.Header.Get("Content-Type") + + context.DataFromReader(200, contentLength, contentType, reader, map[string]string{}) + context.JSON(imageResp.StatusCode, imageResp.Body) + }) +} diff --git a/routes/routes.go b/routes/routes.go index d74a296..ec70a3c 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,6 +1,7 @@ package routes import ( + "safetwitch-backend/routes/proxy" "safetwitch-backend/routes/users" "github.com/gin-gonic/gin" @@ -8,4 +9,5 @@ import ( func SetRoutes(router *gin.Engine) { users.Routes(router) + proxy.Routes(router) }