0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 05:02:58 -05:00
safetwitch-backend/main.go
2023-06-06 13:38:16 -04:00

61 lines
1.1 KiB
Go

package main
import (
"log"
"os"
"safetwitch-backend/extractor/chat"
"safetwitch-backend/routes"
"strings"
"github.com/gin-contrib/cors"
"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 main() {
log.Println("Starting Safetwitch...")
// check for env
env := os.Getenv("URL")
if env == "" {
log.Fatalln("ENV Variable 'URL' is not present")
}
go chat.BeginTwitchChatConnection()
router := gin.New()
router.Use(gin.Recovery())
router.Use(cors.Default())
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()
}
}