0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-23 05:32:59 -05:00
safetwitch-backend/routes/proxy/proxy.go

45 lines
974 B
Go

package proxy
import (
b64 "encoding/base64"
"io"
"net/http"
"safetwitch-backend/extractor/twitch"
"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)
}
body, err := io.ReadAll(imageResp.Body)
if err != nil {
context.Error(err)
}
contentType := imageResp.Header.Get("Content-Type")
context.Data(200, contentType, body)
context.JSON(imageResp.StatusCode, imageResp.Body)
})
auth.GET("/stream/:username/hls.m3u8", func(context *gin.Context) {
streamer := context.Param("username")
playlistFile, err := twitch.GetStream(streamer)
if err != nil {
context.Error(err)
}
context.Data(200, "application/text", []byte(playlistFile))
})
}