package main import ( "log" "os" "safetwitch-backend/extractor/chat" "safetwitch-backend/extractor/twitch" "safetwitch-backend/routes" "strings" "github.com/gin-contrib/gzip" "github.com/gin-gonic/gin" ) func ErrorHandler(c *gin.Context) { c.Next() err := c.Errors.Last() if err != nil { c.JSON(500, gin.H{ "status": "error", "message": err.Error(), }) } } func notFoundHandler(context *gin.Context) { message := []string{"path", context.Request.URL.Path, "was not found."} context.JSON(404, gin.H{ "status": "error", "message": strings.Join(message, " "), }) } func CORS() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set("Access-Control-Allow-Origin", "*") c.Next() } } func setLang(c *gin.Context) { twitch.SetLanguage(c.GetHeader("Accept-Language")) } func main() { log.Println("Starting Safetwitch...") // check for env env := os.Getenv("URL") if env == "" { log.Fatalln("ENV Variable 'URL' is not present") } router := gin.New() router.Use(CORS()) router.Use(gzip.Gzip(gzip.BestCompression)) router.Use(gin.Recovery()) go chat.BeginTwitchChatConnection() router.Use(setLang) router.Use(ErrorHandler) routes.SetRoutes(router) router.NoRoute(notFoundHandler) router.UseRawPath = true router.UnescapePathValues = false log.Println("Safetwitch API running") env = os.Getenv("PORT") if strings.Contains(env, "/") { router.RunUnix(env) } else { router.Run() } }