0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-21 20:53:00 -05:00
safetwitch-backend/main.go

78 lines
1.4 KiB
Go
Raw Normal View History

2023-05-18 09:17:08 -05:00
package main
import (
"log"
2023-05-22 09:27:44 -05:00
"os"
"safetwitch-backend/extractor/chat"
2023-07-03 18:09:39 -05:00
"safetwitch-backend/extractor/twitch"
2023-05-18 09:17:08 -05:00
"safetwitch-backend/routes"
"strings"
2023-06-11 17:14:34 -05:00
"github.com/gin-contrib/gzip"
2023-05-18 09:17:08 -05:00
"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(),
})
}
}
2023-05-18 09:17:08 -05:00
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()
}
}
2023-07-03 18:09:39 -05:00
func setLang(c *gin.Context) {
twitch.SetLanguage(c.GetHeader("Accept-Language"))
}
2023-05-18 09:17:08 -05:00
func main() {
log.Println("Starting Safetwitch...")
2023-05-22 09:27:44 -05:00
// check for env
env := os.Getenv("URL")
if env == "" {
log.Fatalln("ENV Variable 'URL' is not present")
2023-05-22 09:27:44 -05:00
}
2023-05-31 18:31:25 -05:00
router := gin.New()
router.Use(CORS())
2023-06-11 17:14:34 -05:00
router.Use(gzip.Gzip(gzip.BestCompression))
2023-05-31 18:31:25 -05:00
router.Use(gin.Recovery())
go chat.BeginTwitchChatConnection()
2023-07-03 18:09:39 -05:00
router.Use(setLang)
router.Use(ErrorHandler)
2023-05-18 09:17:08 -05:00
routes.SetRoutes(router)
router.NoRoute(notFoundHandler)
2023-09-16 11:01:05 -05:00
router.UseRawPath = true
router.UnescapePathValues = false
2023-06-06 12:37:08 -05:00
log.Println("Safetwitch API running")
2023-06-06 12:37:08 -05:00
env = os.Getenv("PORT")
if strings.Contains(env, "/") {
router.RunUnix(env)
2023-09-27 18:27:25 -05:00
os.Chmod(env, 660)
2023-06-06 12:37:08 -05:00
} else {
router.Run()
}
2023-05-18 09:17:08 -05:00
}