2023-05-18 09:17:08 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-20 21:18:10 -05:00
|
|
|
"fmt"
|
2023-05-22 09:27:44 -05:00
|
|
|
"os"
|
2023-05-18 09:17:08 -05:00
|
|
|
"safetwitch-backend/routes"
|
|
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"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(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
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 main() {
|
2023-05-22 09:27:44 -05:00
|
|
|
// check for env
|
|
|
|
env := os.Getenv("URL")
|
|
|
|
if env == "" {
|
|
|
|
fmt.Println("ENV Variable 'URL' is not present")
|
|
|
|
os.Exit(10)
|
|
|
|
}
|
|
|
|
|
2023-05-18 09:17:08 -05:00
|
|
|
router := gin.Default()
|
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)
|
|
|
|
router.Run()
|
|
|
|
}
|