jokes-bapak2/api/app/v1/app.go

74 lines
1.5 KiB
Go
Raw Normal View History

2021-07-09 12:13:19 +00:00
package v1
import (
"jokes-bapak2-api/app/v1/core"
"jokes-bapak2-api/app/v1/platform/cache"
"jokes-bapak2-api/app/v1/platform/database"
2021-07-14 18:17:01 +00:00
"jokes-bapak2-api/app/v1/routes"
"log"
"os"
"time"
2021-07-14 18:17:01 +00:00
"github.com/getsentry/sentry-go"
2021-07-09 12:13:19 +00:00
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/etag"
2021-07-09 12:13:19 +00:00
)
var memory = cache.InMemory()
var db = database.New()
2021-07-09 12:13:19 +00:00
func New() *fiber.App {
app := fiber.New(fiber.Config{
DisableKeepalive: true,
CaseSensitive: true,
ErrorHandler: errorHandler,
2021-07-09 12:13:19 +00:00
})
err := sentry.Init(sentry.ClientOptions{
2021-08-04 05:56:14 +00:00
Dsn: os.Getenv("SENTRY_DSN"),
Environment: os.Getenv("ENV"),
AttachStacktrace: true,
// Enable printing of SDK debug messages.
// Useful when getting started or trying to figure something out.
Debug: true,
})
if err != nil {
log.Fatal(err)
}
defer sentry.Flush(2 * time.Second)
err = database.Setup()
if err != nil {
sentry.CaptureException(err)
log.Fatal(err)
}
2021-07-19 10:21:08 +00:00
err = core.SetAllJSONJoke(db, memory)
if err != nil {
log.Fatalln(err)
}
2021-07-19 10:21:08 +00:00
err = core.SetTotalJoke(db, memory)
if err != nil {
log.Fatalln(err)
}
app.Use(cors.New())
app.Use(etag.New())
2021-07-09 12:13:19 +00:00
routes.Health(app)
routes.Joke(app)
2021-08-04 05:56:14 +00:00
routes.Submit(app)
2021-07-09 12:13:19 +00:00
return app
}
func errorHandler(c *fiber.Ctx, err error) error {
log.Println(err)
sentry.CaptureException(err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Something went wrong on our end",
})
}