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

66 lines
1.2 KiB
Go

package main
import (
"log"
"os"
"safetwitch-backend/extractor/chat"
"safetwitch-backend/routes"
"strings"
"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 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(gin.Recovery())
go chat.BeginTwitchChatConnection()
router.Use(ErrorHandler)
routes.SetRoutes(router)
router.NoRoute(notFoundHandler)
log.Println("Safetwitch API running")
env = os.Getenv("PORT")
if strings.Contains(env, "/") {
router.RunUnix(env)
} else {
router.Run()
}
}