refactor: directory structure
This commit is contained in:
parent
521734246e
commit
31652fb3ab
131
api/app/app.go
131
api/app/app.go
|
@ -1,131 +0,0 @@
|
||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"jokes-bapak2-api/app/core/joke"
|
|
||||||
"jokes-bapak2-api/app/platform/database"
|
|
||||||
"jokes-bapak2-api/app/routes"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Masterminds/squirrel"
|
|
||||||
"github.com/allegro/bigcache/v3"
|
|
||||||
"github.com/getsentry/sentry-go"
|
|
||||||
"github.com/go-redis/redis/v8"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/etag"
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/limiter"
|
|
||||||
"github.com/gojek/heimdall/v7/httpclient"
|
|
||||||
"github.com/jackc/pgx/v4/pgxpool"
|
|
||||||
_ "github.com/joho/godotenv/autoload"
|
|
||||||
)
|
|
||||||
|
|
||||||
func New() *fiber.App {
|
|
||||||
|
|
||||||
// Setup PostgreSQL
|
|
||||||
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
|
|
||||||
if err != nil {
|
|
||||||
log.Panicln("Unable to create pool config", err)
|
|
||||||
}
|
|
||||||
poolConfig.MaxConnIdleTime = time.Minute * 3
|
|
||||||
poolConfig.MaxConnLifetime = time.Minute * 5
|
|
||||||
poolConfig.MaxConns = 15
|
|
||||||
poolConfig.MinConns = 4
|
|
||||||
|
|
||||||
db, err := pgxpool.ConnectConfig(context.Background(), poolConfig)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicln("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.Panicln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup Sentry
|
|
||||||
err = sentry.Init(sentry.ClientOptions{
|
|
||||||
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.Panicln(err)
|
|
||||||
}
|
|
||||||
defer sentry.Flush(2 * time.Second)
|
|
||||||
|
|
||||||
// TODO: These sequence below might be better wrapped as a Populate() function.
|
|
||||||
err = database.Setup(db)
|
|
||||||
if err != nil {
|
|
||||||
sentry.CaptureException(err)
|
|
||||||
log.Panicln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = joke.SetAllJSONJoke(db, context.Background(), memory)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicln(err)
|
|
||||||
}
|
|
||||||
err = joke.SetTotalJoke(db, context.Background(), memory)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
timeoutDefault := time.Minute * 1
|
|
||||||
|
|
||||||
app := fiber.New(fiber.Config{
|
|
||||||
ReadTimeout: timeoutDefault,
|
|
||||||
WriteTimeout: timeoutDefault,
|
|
||||||
CaseSensitive: true,
|
|
||||||
DisableKeepalive: true,
|
|
||||||
ErrorHandler: errorHandler,
|
|
||||||
})
|
|
||||||
|
|
||||||
app.Use(limiter.New(limiter.Config{
|
|
||||||
Max: 30,
|
|
||||||
Expiration: 1 * time.Minute,
|
|
||||||
LimitReached: limitHandler,
|
|
||||||
}))
|
|
||||||
|
|
||||||
app.Use(cors.New())
|
|
||||||
app.Use(etag.New())
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
return app
|
|
||||||
}
|
|
||||||
|
|
||||||
func limitHandler(c *fiber.Ctx) error {
|
|
||||||
return c.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{
|
|
||||||
"message": "we only allow up to 15 request per minute",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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",
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -11,11 +11,11 @@ import (
|
||||||
func GetUserID(db *pgxpool.Pool, ctx context.Context, key string) (int, error) {
|
func GetUserID(db *pgxpool.Pool, ctx context.Context, key string) (int, error) {
|
||||||
var query = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
|
var query = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
|
||||||
|
|
||||||
c1, err := db.Acquire(ctx)
|
c, err := db.Acquire(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
defer c1.Release()
|
defer c.Release()
|
||||||
|
|
||||||
sql, args, err := query.
|
sql, args, err := query.
|
||||||
Update("administrators").
|
Update("administrators").
|
||||||
|
@ -25,18 +25,12 @@ func GetUserID(db *pgxpool.Pool, ctx context.Context, key string) (int, error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := c1.Query(context.Background(), sql, args...)
|
r, err := c.Query(context.Background(), sql, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
|
||||||
c2, err := db.Acquire(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
defer c2.Release()
|
|
||||||
|
|
||||||
sql, args, err = query.
|
sql, args, err = query.
|
||||||
Select("id").
|
Select("id").
|
||||||
From("administrators").
|
From("administrators").
|
||||||
|
@ -47,7 +41,7 @@ func GetUserID(db *pgxpool.Pool, ctx context.Context, key string) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var id int
|
var id int
|
||||||
err = c2.QueryRow(context.Background(), sql, args...).Scan(&id)
|
err = c.QueryRow(context.Background(), sql, args...).Scan(&id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
|
@ -3,7 +3,7 @@ package joke
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"jokes-bapak2-api/app/core/schema"
|
"jokes-bapak2-api/core/schema"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
|
@ -2,7 +2,7 @@ package joke_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"jokes-bapak2-api/app/core/joke"
|
"jokes-bapak2-api/core/joke"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v4"
|
"github.com/jackc/pgx/v4"
|
|
@ -2,7 +2,7 @@ package joke
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"jokes-bapak2-api/app/core/schema"
|
"jokes-bapak2-api/core/schema"
|
||||||
|
|
||||||
"github.com/Masterminds/squirrel"
|
"github.com/Masterminds/squirrel"
|
||||||
"github.com/allegro/bigcache/v3"
|
"github.com/allegro/bigcache/v3"
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"jokes-bapak2-api/app/core/schema"
|
"jokes-bapak2-api/core/schema"
|
||||||
"jokes-bapak2-api/app/utils"
|
"jokes-bapak2-api/utils"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
|
@ -2,7 +2,7 @@ package validator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"jokes-bapak2-api/app/utils"
|
"jokes-bapak2-api/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
Before Width: | Height: | Size: 9.7 KiB After Width: | Height: | Size: 9.7 KiB |
|
@ -1,9 +1,9 @@
|
||||||
package joke
|
package joke
|
||||||
|
|
||||||
import (
|
import (
|
||||||
core "jokes-bapak2-api/app/core/joke"
|
core "jokes-bapak2-api/core/joke"
|
||||||
"jokes-bapak2-api/app/core/schema"
|
"jokes-bapak2-api/core/schema"
|
||||||
"jokes-bapak2-api/app/core/validator"
|
"jokes-bapak2-api/core/validator"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
|
@ -1,8 +1,8 @@
|
||||||
package joke
|
package joke
|
||||||
|
|
||||||
import (
|
import (
|
||||||
core "jokes-bapak2-api/app/core/joke"
|
core "jokes-bapak2-api/core/joke"
|
||||||
"jokes-bapak2-api/app/core/validator"
|
"jokes-bapak2-api/core/validator"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
|
@ -3,9 +3,9 @@ package joke
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
core "jokes-bapak2-api/app/core/joke"
|
core "jokes-bapak2-api/core/joke"
|
||||||
"jokes-bapak2-api/app/core/schema"
|
"jokes-bapak2-api/core/schema"
|
||||||
"jokes-bapak2-api/app/utils"
|
"jokes-bapak2-api/utils"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
|
@ -2,7 +2,7 @@ package joke
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
core "jokes-bapak2-api/app/core/joke"
|
core "jokes-bapak2-api/core/joke"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/allegro/bigcache/v3"
|
"github.com/allegro/bigcache/v3"
|
|
@ -1,9 +1,9 @@
|
||||||
package joke
|
package joke
|
||||||
|
|
||||||
import (
|
import (
|
||||||
core "jokes-bapak2-api/app/core/joke"
|
core "jokes-bapak2-api/core/joke"
|
||||||
"jokes-bapak2-api/app/core/schema"
|
"jokes-bapak2-api/core/schema"
|
||||||
"jokes-bapak2-api/app/core/validator"
|
"jokes-bapak2-api/core/validator"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
|
@ -2,8 +2,8 @@ package submit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
core "jokes-bapak2-api/app/core/submit"
|
core "jokes-bapak2-api/core/submit"
|
||||||
"jokes-bapak2-api/app/core/validator"
|
"jokes-bapak2-api/core/validator"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
128
api/main.go
128
api/main.go
|
@ -1,26 +1,146 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
|
"context"
|
||||||
|
"jokes-bapak2-api/core/joke"
|
||||||
|
"jokes-bapak2-api/platform/database"
|
||||||
|
"jokes-bapak2-api/routes"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
_ "github.com/joho/godotenv/autoload"
|
_ "github.com/joho/godotenv/autoload"
|
||||||
|
|
||||||
|
"github.com/Masterminds/squirrel"
|
||||||
|
"github.com/allegro/bigcache/v3"
|
||||||
|
"github.com/getsentry/sentry-go"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/etag"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/limiter"
|
||||||
|
"github.com/gojek/heimdall/v7/httpclient"
|
||||||
|
"github.com/jackc/pgx/v4/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
a := app.New()
|
// Setup PostgreSQL
|
||||||
|
poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln("Unable to create pool config", err)
|
||||||
|
}
|
||||||
|
poolConfig.MaxConnIdleTime = time.Minute * 3
|
||||||
|
poolConfig.MaxConnLifetime = time.Minute * 5
|
||||||
|
poolConfig.MaxConns = 15
|
||||||
|
poolConfig.MinConns = 4
|
||||||
|
|
||||||
|
db, err := pgxpool.ConnectConfig(context.Background(), poolConfig)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln("Unable to create connection", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
// Setup Redis
|
||||||
|
opt, err := redis.ParseURL(os.Getenv("REDIS_URL"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
rdb := redis.NewClient(opt)
|
||||||
|
defer rdb.Close()
|
||||||
|
|
||||||
|
// Setup In Memory
|
||||||
|
memory, err := bigcache.NewBigCache(bigcache.DefaultConfig(6 * time.Hour))
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
defer memory.Close()
|
||||||
|
|
||||||
|
// Setup Sentry
|
||||||
|
err = sentry.Init(sentry.ClientOptions{
|
||||||
|
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.Panicln(err)
|
||||||
|
}
|
||||||
|
defer sentry.Flush(2 * time.Second)
|
||||||
|
|
||||||
|
// TODO: These sequence below might be better wrapped as a Populate() function.
|
||||||
|
err = database.Setup(db)
|
||||||
|
if err != nil {
|
||||||
|
sentry.CaptureException(err)
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = joke.SetAllJSONJoke(db, context.Background(), memory)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
err = joke.SetTotalJoke(db, context.Background(), memory)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeoutDefault := time.Minute * 1
|
||||||
|
|
||||||
|
app := fiber.New(fiber.Config{
|
||||||
|
ReadTimeout: timeoutDefault,
|
||||||
|
WriteTimeout: timeoutDefault,
|
||||||
|
CaseSensitive: true,
|
||||||
|
DisableKeepalive: true,
|
||||||
|
ErrorHandler: errorHandler,
|
||||||
|
})
|
||||||
|
|
||||||
|
app.Use(limiter.New(limiter.Config{
|
||||||
|
Max: 30,
|
||||||
|
Expiration: 1 * time.Minute,
|
||||||
|
LimitReached: limitHandler,
|
||||||
|
}))
|
||||||
|
|
||||||
|
app.Use(cors.New())
|
||||||
|
app.Use(etag.New())
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
// Start server (with or without graceful shutdown).
|
// Start server (with or without graceful shutdown).
|
||||||
if os.Getenv("ENV") == "development" {
|
if os.Getenv("ENV") == "development" {
|
||||||
StartServer(a)
|
StartServer(app)
|
||||||
} else {
|
} else {
|
||||||
StartServerWithGracefulShutdown(a)
|
StartServerWithGracefulShutdown(app)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func limitHandler(c *fiber.Ctx) error {
|
||||||
|
return c.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{
|
||||||
|
"message": "we only allow up to 15 request per minute",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// StartServerWithGracefulShutdown function for starting server with a graceful shutdown.
|
// StartServerWithGracefulShutdown function for starting server with a graceful shutdown.
|
||||||
func StartServerWithGracefulShutdown(a *fiber.App) {
|
func StartServerWithGracefulShutdown(a *fiber.App) {
|
||||||
// Create channel for idle connections.
|
// Create channel for idle connections.
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/core/administrator"
|
"jokes-bapak2-api/core/administrator"
|
||||||
|
|
||||||
phccrypto "github.com/aldy505/phc-crypto"
|
phccrypto "github.com/aldy505/phc-crypto"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
|
@ -9,32 +9,17 @@ import (
|
||||||
|
|
||||||
// Setup the table connection, create table if not exists
|
// Setup the table connection, create table if not exists
|
||||||
func Setup(db *pgxpool.Pool) error {
|
func Setup(db *pgxpool.Pool) error {
|
||||||
conn, err := db.Acquire(context.Background())
|
err := setupAuthTable(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = setupAuthTable(conn)
|
err = setupJokesTable(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err = db.Acquire(context.Background())
|
err = setupSubmissionTable(db)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = setupJokesTable(conn)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err = db.Acquire(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = setupSubmissionTable(conn)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -42,12 +27,16 @@ func Setup(db *pgxpool.Pool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupAuthTable(conn *pgxpool.Conn) error {
|
func setupAuthTable(db *pgxpool.Pool) error {
|
||||||
|
conn, err := db.Acquire(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
defer conn.Release()
|
defer conn.Release()
|
||||||
|
|
||||||
// Check if table exists
|
// Check if table exists
|
||||||
var tableAuthExists bool
|
var tableAuthExists bool
|
||||||
err := conn.QueryRow(context.Background(), `SELECT EXISTS (
|
err = conn.QueryRow(context.Background(), `SELECT EXISTS (
|
||||||
SELECT FROM information_schema.tables
|
SELECT FROM information_schema.tables
|
||||||
WHERE table_schema = 'public'
|
WHERE table_schema = 'public'
|
||||||
AND table_name = 'administrators'
|
AND table_name = 'administrators'
|
||||||
|
@ -68,21 +57,24 @@ func setupAuthTable(conn *pgxpool.Conn) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
q, err := conn.Query(context.Background(), sql)
|
_, err = conn.Exec(context.Background(), sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer q.Close()
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupJokesTable(conn *pgxpool.Conn) error {
|
func setupJokesTable(db *pgxpool.Pool) error {
|
||||||
|
conn, err := db.Acquire(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
defer conn.Release()
|
defer conn.Release()
|
||||||
|
|
||||||
// Check if table exists
|
// Check if table exists
|
||||||
var tableJokesExists bool
|
var tableJokesExists bool
|
||||||
err := conn.QueryRow(context.Background(), `SELECT EXISTS (
|
err = conn.QueryRow(context.Background(), `SELECT EXISTS (
|
||||||
SELECT FROM information_schema.tables
|
SELECT FROM information_schema.tables
|
||||||
WHERE table_schema = 'public'
|
WHERE table_schema = 'public'
|
||||||
AND table_name = 'jokesbapak2'
|
AND table_name = 'jokesbapak2'
|
||||||
|
@ -102,22 +94,25 @@ func setupJokesTable(conn *pgxpool.Conn) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
q, err := conn.Query(context.Background(), sql)
|
_, err = conn.Exec(context.Background(), sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer q.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupSubmissionTable(conn *pgxpool.Conn) error {
|
func setupSubmissionTable(db *pgxpool.Pool) error {
|
||||||
|
conn, err := db.Acquire(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
defer conn.Release()
|
defer conn.Release()
|
||||||
|
|
||||||
//Check if table exists
|
//Check if table exists
|
||||||
var tableSubmissionExists bool
|
var tableSubmissionExists bool
|
||||||
err := conn.QueryRow(context.Background(), `SELECT EXISTS (
|
err = conn.QueryRow(context.Background(), `SELECT EXISTS (
|
||||||
SELECT FROM information_schema.tables
|
SELECT FROM information_schema.tables
|
||||||
WHERE table_schema = 'public'
|
WHERE table_schema = 'public'
|
||||||
AND table_name = 'submission'
|
AND table_name = 'submission'
|
||||||
|
@ -139,11 +134,10 @@ func setupSubmissionTable(conn *pgxpool.Conn) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
q, err := conn.Query(context.Background(), sql)
|
_, err = conn.Exec(context.Background(), sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer q.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
|
@ -1,7 +1,7 @@
|
||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/handler/health"
|
"jokes-bapak2-api/handler/health"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/cache"
|
"github.com/gofiber/fiber/v2/middleware/cache"
|
|
@ -1,8 +1,8 @@
|
||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/handler/joke"
|
"jokes-bapak2-api/handler/joke"
|
||||||
"jokes-bapak2-api/app/middleware"
|
"jokes-bapak2-api/middleware"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2/middleware/cache"
|
"github.com/gofiber/fiber/v2/middleware/cache"
|
|
@ -1,7 +1,7 @@
|
||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/handler/submit"
|
"jokes-bapak2-api/handler/submit"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
|
@ -1,7 +1,7 @@
|
||||||
package utils_test
|
package utils_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/utils"
|
"jokes-bapak2-api/utils"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package utils_test
|
package utils_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/utils"
|
"jokes-bapak2-api/utils"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
|
@ -1,7 +1,7 @@
|
||||||
package utils_test
|
package utils_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/utils"
|
"jokes-bapak2-api/utils"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
|
@ -1,7 +1,7 @@
|
||||||
package utils_test
|
package utils_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/utils"
|
"jokes-bapak2-api/utils"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package utils_test
|
package utils_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"jokes-bapak2-api/app/utils"
|
"jokes-bapak2-api/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
Loading…
Reference in New Issue