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

67 lines
1.2 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-05-18 09:17:08 -05:00
"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(),
})
}
}
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-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-05-31 18:31:25 -05:00
router.Use(gin.Recovery())
go chat.BeginTwitchChatConnection()
router.Use(ErrorHandler)
2023-05-18 09:17:08 -05:00
routes.SetRoutes(router)
router.NoRoute(notFoundHandler)
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)
} else {
router.Run()
}
2023-05-18 09:17:08 -05:00
}