2023-05-18 09:17:08 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-31 07:11:10 -05:00
|
|
|
"log"
|
2023-05-22 09:27:44 -05:00
|
|
|
"os"
|
2023-06-07 11:34:20 -05:00
|
|
|
"safetwitch-backend/extractor/chat"
|
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"
|
|
|
|
)
|
|
|
|
|
2023-05-20 21:18:10 -05:00
|
|
|
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, " "),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-07 11:25:37 -05:00
|
|
|
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() {
|
2023-05-31 07:11:10 -05:00
|
|
|
log.Println("Starting Safetwitch...")
|
2023-05-22 09:27:44 -05:00
|
|
|
// check for env
|
|
|
|
env := os.Getenv("URL")
|
|
|
|
if env == "" {
|
2023-05-31 07:11:10 -05:00
|
|
|
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()
|
2023-06-07 11:25:37 -05:00
|
|
|
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())
|
|
|
|
|
2023-06-07 11:34:20 -05:00
|
|
|
go chat.BeginTwitchChatConnection()
|
|
|
|
|
2023-05-20 21:18:10 -05:00
|
|
|
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
|
|
|
|
2023-05-31 07:11:10 -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
|
|
|
}
|