2021-07-09 12:13:19 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2021-09-26 19:13:38 +00:00
|
|
|
"context"
|
2021-07-17 09:06:33 +00:00
|
|
|
"jokes-bapak2-api/app/v1/core"
|
|
|
|
"jokes-bapak2-api/app/v1/platform/database"
|
2021-07-14 18:17:01 +00:00
|
|
|
"jokes-bapak2-api/app/v1/routes"
|
2021-07-17 09:06:33 +00:00
|
|
|
"log"
|
2021-07-18 05:28:24 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
2021-07-14 18:17:01 +00:00
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
"github.com/Masterminds/squirrel"
|
|
|
|
"github.com/allegro/bigcache/v3"
|
2021-07-18 05:28:24 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-09-26 19:13:38 +00:00
|
|
|
"github.com/go-redis/redis/v8"
|
2021-07-09 12:13:19 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-07-18 05:28:24 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/etag"
|
2021-09-26 19:13:38 +00:00
|
|
|
"github.com/gojek/heimdall/v7/httpclient"
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
2021-07-09 12:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func New() *fiber.App {
|
2021-09-26 19:13:38 +00:00
|
|
|
// Setup PostgreSQL
|
|
|
|
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Unable to create pool config", err)
|
|
|
|
}
|
|
|
|
poolConfig.MaxConns = 15
|
|
|
|
poolConfig.MinConns = 2
|
|
|
|
|
|
|
|
db, err := pgxpool.ConnectConfig(context.Background(), poolConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Unable to create connection", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Redis
|
|
|
|
opt, err := redis.ParseURL(os.Getenv("REDIS_URL"))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
rdb := redis.NewClient(opt)
|
|
|
|
|
|
|
|
// Setup In Memory
|
|
|
|
memory, err := bigcache.NewBigCache(bigcache.DefaultConfig(6 * time.Hour))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2021-07-09 12:13:19 +00:00
|
|
|
app := fiber.New(fiber.Config{
|
|
|
|
DisableKeepalive: true,
|
|
|
|
CaseSensitive: true,
|
2021-07-18 05:28:24 +00:00
|
|
|
ErrorHandler: errorHandler,
|
2021-07-09 12:13:19 +00:00
|
|
|
})
|
|
|
|
|
2021-09-26 19:13:38 +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,
|
2021-07-18 05:28:24 +00:00
|
|
|
// 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)
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
err = database.Setup(db)
|
2021-07-18 05:28:24 +00:00
|
|
|
if err != nil {
|
|
|
|
sentry.CaptureException(err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-07-19 10:21:08 +00:00
|
|
|
err = core.SetAllJSONJoke(db, memory)
|
2021-07-18 05:28:24 +00:00
|
|
|
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)
|
2021-07-17 09:06:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 05:28:24 +00:00
|
|
|
app.Use(cors.New())
|
|
|
|
app.Use(etag.New())
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
route := routes.Dependencies{
|
|
|
|
DB: db,
|
|
|
|
Redis: rdb,
|
|
|
|
Memory: memory,
|
|
|
|
HTTP: httpclient.NewClient(httpclient.WithHTTPTimeout(10 * time.Second)),
|
|
|
|
Query: squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar),
|
|
|
|
App: app,
|
|
|
|
}
|
|
|
|
route.Health()
|
|
|
|
route.Joke()
|
|
|
|
route.Submit()
|
2021-07-09 12:13:19 +00:00
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
2021-07-18 05:28:24 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
})
|
|
|
|
}
|