From 31652fb3abe83cf7a70da5d9aa021839b0d947c1 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Sat, 30 Oct 2021 10:24:53 +0700 Subject: [PATCH] refactor: directory structure --- api/app/app.go | 131 ------------------ api/{app => }/core/administrator/id.go | 14 +- api/{app => }/core/administrator/verify.go | 0 api/{app => }/core/joke/getter.go | 2 +- api/{app => }/core/joke/getter_test.go | 2 +- api/{app => }/core/joke/init_test.go | 0 api/{app => }/core/joke/setter.go | 2 +- api/{app => }/core/schema/err.go | 0 api/{app => }/core/schema/image_api.go | 0 api/{app => }/core/schema/joke.go | 0 api/{app => }/core/submit/setter.go | 4 +- api/{app => }/core/validator/author.go | 0 api/{app => }/core/validator/image.go | 2 +- api/{app => }/core/validator/joke.go | 0 api/{app => }/favicon.png | Bin api/{app => }/handler/health/health.go | 0 api/{app => }/handler/health/schema.go | 0 api/{app => }/handler/joke/dependencies.go | 0 api/{app => }/handler/joke/joke_add.go | 6 +- api/{app => }/handler/joke/joke_delete.go | 4 +- api/{app => }/handler/joke/joke_get.go | 6 +- api/{app => }/handler/joke/joke_total.go | 2 +- api/{app => }/handler/joke/joke_update.go | 6 +- api/{app => }/handler/joke/schema.go | 0 api/{app => }/handler/submit/dependencies.go | 0 api/{app => }/handler/submit/schema.go | 0 api/{app => }/handler/submit/submit_add.go | 4 +- api/{app => }/handler/submit/submit_get.go | 0 api/main.go | 128 ++++++++++++++++- api/{app => }/middleware/auth.go | 2 +- api/{app => }/middleware/schema.go | 0 api/{app => }/middleware/validation.go | 0 api/{app => }/platform/database/create.go | 54 ++++---- .../platform/database/placeholder.sql | 0 api/{app => }/routes/dependencies.go | 0 api/{app => }/routes/health.go | 2 +- api/{app => }/routes/joke.go | 4 +- api/{app => }/routes/submit.go | 2 +- api/{app => }/utils/array.go | 0 api/{app => }/utils/array_test.go | 2 +- api/{app => }/utils/date.go | 0 api/{app => }/utils/date_test.go | 2 +- api/{app => }/utils/parse.go | 0 api/{app => }/utils/parse_test.go | 2 +- api/{app => }/utils/random.go | 0 api/{app => }/utils/random_test.go | 2 +- api/{app => }/utils/request.go | 0 api/{app => }/utils/request_test.go | 2 +- 48 files changed, 182 insertions(+), 205 deletions(-) delete mode 100644 api/app/app.go rename api/{app => }/core/administrator/id.go (74%) rename api/{app => }/core/administrator/verify.go (100%) rename api/{app => }/core/joke/getter.go (99%) rename api/{app => }/core/joke/getter_test.go (97%) rename api/{app => }/core/joke/init_test.go (100%) rename api/{app => }/core/joke/setter.go (98%) rename api/{app => }/core/schema/err.go (100%) rename api/{app => }/core/schema/image_api.go (100%) rename api/{app => }/core/schema/joke.go (100%) rename api/{app => }/core/submit/setter.go (95%) rename api/{app => }/core/validator/author.go (100%) rename api/{app => }/core/validator/image.go (96%) rename api/{app => }/core/validator/joke.go (100%) rename api/{app => }/favicon.png (100%) rename api/{app => }/handler/health/health.go (100%) rename api/{app => }/handler/health/schema.go (100%) rename api/{app => }/handler/joke/dependencies.go (100%) rename api/{app => }/handler/joke/joke_add.go (90%) rename api/{app => }/handler/joke/joke_delete.go (91%) rename api/{app => }/handler/joke/joke_get.go (96%) rename api/{app => }/handler/joke/joke_total.go (94%) rename api/{app => }/handler/joke/joke_update.go (91%) rename api/{app => }/handler/joke/schema.go (100%) rename api/{app => }/handler/submit/dependencies.go (100%) rename api/{app => }/handler/submit/schema.go (100%) rename api/{app => }/handler/submit/submit_add.go (97%) rename api/{app => }/handler/submit/submit_get.go (100%) rename api/{app => }/middleware/auth.go (95%) rename api/{app => }/middleware/schema.go (100%) rename api/{app => }/middleware/validation.go (100%) rename api/{app => }/platform/database/create.go (77%) rename api/{app => }/platform/database/placeholder.sql (100%) rename api/{app => }/routes/dependencies.go (100%) rename api/{app => }/routes/health.go (90%) rename api/{app => }/routes/joke.go (94%) rename api/{app => }/routes/submit.go (93%) rename api/{app => }/utils/array.go (100%) rename api/{app => }/utils/array_test.go (93%) rename api/{app => }/utils/date.go (100%) rename api/{app => }/utils/date_test.go (96%) rename api/{app => }/utils/parse.go (100%) rename api/{app => }/utils/parse_test.go (96%) rename api/{app => }/utils/random.go (100%) rename api/{app => }/utils/random_test.go (93%) rename api/{app => }/utils/request.go (100%) rename api/{app => }/utils/request_test.go (94%) diff --git a/api/app/app.go b/api/app/app.go deleted file mode 100644 index 9e02c8a..0000000 --- a/api/app/app.go +++ /dev/null @@ -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", - }) -} diff --git a/api/app/core/administrator/id.go b/api/core/administrator/id.go similarity index 74% rename from api/app/core/administrator/id.go rename to api/core/administrator/id.go index 8ff3f46..3eb83fb 100644 --- a/api/app/core/administrator/id.go +++ b/api/core/administrator/id.go @@ -11,11 +11,11 @@ import ( func GetUserID(db *pgxpool.Pool, ctx context.Context, key string) (int, error) { var query = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) - c1, err := db.Acquire(ctx) + c, err := db.Acquire(ctx) if err != nil { return 0, err } - defer c1.Release() + defer c.Release() sql, args, err := query. Update("administrators"). @@ -25,18 +25,12 @@ func GetUserID(db *pgxpool.Pool, ctx context.Context, key string) (int, error) { return 0, err } - r, err := c1.Query(context.Background(), sql, args...) + r, err := c.Query(context.Background(), sql, args...) if err != nil { return 0, err } defer r.Close() - c2, err := db.Acquire(ctx) - if err != nil { - return 0, err - } - defer c2.Release() - sql, args, err = query. Select("id"). From("administrators"). @@ -47,7 +41,7 @@ func GetUserID(db *pgxpool.Pool, ctx context.Context, key string) (int, error) { } var id int - err = c2.QueryRow(context.Background(), sql, args...).Scan(&id) + err = c.QueryRow(context.Background(), sql, args...).Scan(&id) if err != nil { return 0, err } diff --git a/api/app/core/administrator/verify.go b/api/core/administrator/verify.go similarity index 100% rename from api/app/core/administrator/verify.go rename to api/core/administrator/verify.go diff --git a/api/app/core/joke/getter.go b/api/core/joke/getter.go similarity index 99% rename from api/app/core/joke/getter.go rename to api/core/joke/getter.go index 78a27a3..5d23316 100644 --- a/api/app/core/joke/getter.go +++ b/api/core/joke/getter.go @@ -3,7 +3,7 @@ package joke import ( "context" "errors" - "jokes-bapak2-api/app/core/schema" + "jokes-bapak2-api/core/schema" "math/rand" "strconv" diff --git a/api/app/core/joke/getter_test.go b/api/core/joke/getter_test.go similarity index 97% rename from api/app/core/joke/getter_test.go rename to api/core/joke/getter_test.go index 5bd4be7..68d26a4 100644 --- a/api/app/core/joke/getter_test.go +++ b/api/core/joke/getter_test.go @@ -2,7 +2,7 @@ package joke_test import ( "context" - "jokes-bapak2-api/app/core/joke" + "jokes-bapak2-api/core/joke" "testing" "github.com/jackc/pgx/v4" diff --git a/api/app/core/joke/init_test.go b/api/core/joke/init_test.go similarity index 100% rename from api/app/core/joke/init_test.go rename to api/core/joke/init_test.go diff --git a/api/app/core/joke/setter.go b/api/core/joke/setter.go similarity index 98% rename from api/app/core/joke/setter.go rename to api/core/joke/setter.go index 3068ec4..48bbd4b 100644 --- a/api/app/core/joke/setter.go +++ b/api/core/joke/setter.go @@ -2,7 +2,7 @@ package joke import ( "context" - "jokes-bapak2-api/app/core/schema" + "jokes-bapak2-api/core/schema" "github.com/Masterminds/squirrel" "github.com/allegro/bigcache/v3" diff --git a/api/app/core/schema/err.go b/api/core/schema/err.go similarity index 100% rename from api/app/core/schema/err.go rename to api/core/schema/err.go diff --git a/api/app/core/schema/image_api.go b/api/core/schema/image_api.go similarity index 100% rename from api/app/core/schema/image_api.go rename to api/core/schema/image_api.go diff --git a/api/app/core/schema/joke.go b/api/core/schema/joke.go similarity index 100% rename from api/app/core/schema/joke.go rename to api/core/schema/joke.go diff --git a/api/app/core/submit/setter.go b/api/core/submit/setter.go similarity index 95% rename from api/app/core/submit/setter.go rename to api/core/submit/setter.go index 49bcff2..9607114 100644 --- a/api/app/core/submit/setter.go +++ b/api/core/submit/setter.go @@ -4,8 +4,8 @@ import ( "bytes" "io" "io/ioutil" - "jokes-bapak2-api/app/core/schema" - "jokes-bapak2-api/app/utils" + "jokes-bapak2-api/core/schema" + "jokes-bapak2-api/utils" "mime/multipart" "net/http" "net/url" diff --git a/api/app/core/validator/author.go b/api/core/validator/author.go similarity index 100% rename from api/app/core/validator/author.go rename to api/core/validator/author.go diff --git a/api/app/core/validator/image.go b/api/core/validator/image.go similarity index 96% rename from api/app/core/validator/image.go rename to api/core/validator/image.go index 2f5dda0..eff0a7a 100644 --- a/api/app/core/validator/image.go +++ b/api/core/validator/image.go @@ -2,7 +2,7 @@ package validator import ( "errors" - "jokes-bapak2-api/app/utils" + "jokes-bapak2-api/utils" "net/http" "strings" diff --git a/api/app/core/validator/joke.go b/api/core/validator/joke.go similarity index 100% rename from api/app/core/validator/joke.go rename to api/core/validator/joke.go diff --git a/api/app/favicon.png b/api/favicon.png similarity index 100% rename from api/app/favicon.png rename to api/favicon.png diff --git a/api/app/handler/health/health.go b/api/handler/health/health.go similarity index 100% rename from api/app/handler/health/health.go rename to api/handler/health/health.go diff --git a/api/app/handler/health/schema.go b/api/handler/health/schema.go similarity index 100% rename from api/app/handler/health/schema.go rename to api/handler/health/schema.go diff --git a/api/app/handler/joke/dependencies.go b/api/handler/joke/dependencies.go similarity index 100% rename from api/app/handler/joke/dependencies.go rename to api/handler/joke/dependencies.go diff --git a/api/app/handler/joke/joke_add.go b/api/handler/joke/joke_add.go similarity index 90% rename from api/app/handler/joke/joke_add.go rename to api/handler/joke/joke_add.go index 841c83b..97af719 100644 --- a/api/app/handler/joke/joke_add.go +++ b/api/handler/joke/joke_add.go @@ -1,9 +1,9 @@ package joke import ( - core "jokes-bapak2-api/app/core/joke" - "jokes-bapak2-api/app/core/schema" - "jokes-bapak2-api/app/core/validator" + core "jokes-bapak2-api/core/joke" + "jokes-bapak2-api/core/schema" + "jokes-bapak2-api/core/validator" "github.com/gofiber/fiber/v2" ) diff --git a/api/app/handler/joke/joke_delete.go b/api/handler/joke/joke_delete.go similarity index 91% rename from api/app/handler/joke/joke_delete.go rename to api/handler/joke/joke_delete.go index 3aad05d..2937b99 100644 --- a/api/app/handler/joke/joke_delete.go +++ b/api/handler/joke/joke_delete.go @@ -1,8 +1,8 @@ package joke import ( - core "jokes-bapak2-api/app/core/joke" - "jokes-bapak2-api/app/core/validator" + core "jokes-bapak2-api/core/joke" + "jokes-bapak2-api/core/validator" "strconv" "github.com/gofiber/fiber/v2" diff --git a/api/app/handler/joke/joke_get.go b/api/handler/joke/joke_get.go similarity index 96% rename from api/app/handler/joke/joke_get.go rename to api/handler/joke/joke_get.go index 33efa43..ea75bfb 100644 --- a/api/app/handler/joke/joke_get.go +++ b/api/handler/joke/joke_get.go @@ -3,9 +3,9 @@ package joke import ( "errors" "io/ioutil" - core "jokes-bapak2-api/app/core/joke" - "jokes-bapak2-api/app/core/schema" - "jokes-bapak2-api/app/utils" + core "jokes-bapak2-api/core/joke" + "jokes-bapak2-api/core/schema" + "jokes-bapak2-api/utils" "strconv" "time" diff --git a/api/app/handler/joke/joke_total.go b/api/handler/joke/joke_total.go similarity index 94% rename from api/app/handler/joke/joke_total.go rename to api/handler/joke/joke_total.go index ea38489..a820134 100644 --- a/api/app/handler/joke/joke_total.go +++ b/api/handler/joke/joke_total.go @@ -2,7 +2,7 @@ package joke import ( "errors" - core "jokes-bapak2-api/app/core/joke" + core "jokes-bapak2-api/core/joke" "strconv" "github.com/allegro/bigcache/v3" diff --git a/api/app/handler/joke/joke_update.go b/api/handler/joke/joke_update.go similarity index 91% rename from api/app/handler/joke/joke_update.go rename to api/handler/joke/joke_update.go index a275aed..d586553 100644 --- a/api/app/handler/joke/joke_update.go +++ b/api/handler/joke/joke_update.go @@ -1,9 +1,9 @@ package joke import ( - core "jokes-bapak2-api/app/core/joke" - "jokes-bapak2-api/app/core/schema" - "jokes-bapak2-api/app/core/validator" + core "jokes-bapak2-api/core/joke" + "jokes-bapak2-api/core/schema" + "jokes-bapak2-api/core/validator" "github.com/gofiber/fiber/v2" ) diff --git a/api/app/handler/joke/schema.go b/api/handler/joke/schema.go similarity index 100% rename from api/app/handler/joke/schema.go rename to api/handler/joke/schema.go diff --git a/api/app/handler/submit/dependencies.go b/api/handler/submit/dependencies.go similarity index 100% rename from api/app/handler/submit/dependencies.go rename to api/handler/submit/dependencies.go diff --git a/api/app/handler/submit/schema.go b/api/handler/submit/schema.go similarity index 100% rename from api/app/handler/submit/schema.go rename to api/handler/submit/schema.go diff --git a/api/app/handler/submit/submit_add.go b/api/handler/submit/submit_add.go similarity index 97% rename from api/app/handler/submit/submit_add.go rename to api/handler/submit/submit_add.go index d3e4a6c..69b8119 100644 --- a/api/app/handler/submit/submit_add.go +++ b/api/handler/submit/submit_add.go @@ -2,8 +2,8 @@ package submit import ( "context" - core "jokes-bapak2-api/app/core/submit" - "jokes-bapak2-api/app/core/validator" + core "jokes-bapak2-api/core/submit" + "jokes-bapak2-api/core/validator" "net/url" "strings" "time" diff --git a/api/app/handler/submit/submit_get.go b/api/handler/submit/submit_get.go similarity index 100% rename from api/app/handler/submit/submit_get.go rename to api/handler/submit/submit_get.go diff --git a/api/main.go b/api/main.go index d4cb1b5..4091d04 100644 --- a/api/main.go +++ b/api/main.go @@ -1,26 +1,146 @@ package main import ( - "jokes-bapak2-api/app" "log" "os" "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/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() { - 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). if os.Getenv("ENV") == "development" { - StartServer(a) + StartServer(app) } 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. func StartServerWithGracefulShutdown(a *fiber.App) { // Create channel for idle connections. diff --git a/api/app/middleware/auth.go b/api/middleware/auth.go similarity index 95% rename from api/app/middleware/auth.go rename to api/middleware/auth.go index 1076ecb..3ef847d 100644 --- a/api/app/middleware/auth.go +++ b/api/middleware/auth.go @@ -1,7 +1,7 @@ package middleware import ( - "jokes-bapak2-api/app/core/administrator" + "jokes-bapak2-api/core/administrator" phccrypto "github.com/aldy505/phc-crypto" "github.com/gofiber/fiber/v2" diff --git a/api/app/middleware/schema.go b/api/middleware/schema.go similarity index 100% rename from api/app/middleware/schema.go rename to api/middleware/schema.go diff --git a/api/app/middleware/validation.go b/api/middleware/validation.go similarity index 100% rename from api/app/middleware/validation.go rename to api/middleware/validation.go diff --git a/api/app/platform/database/create.go b/api/platform/database/create.go similarity index 77% rename from api/app/platform/database/create.go rename to api/platform/database/create.go index 684b589..e8d2da0 100644 --- a/api/app/platform/database/create.go +++ b/api/platform/database/create.go @@ -9,32 +9,17 @@ import ( // Setup the table connection, create table if not exists func Setup(db *pgxpool.Pool) error { - conn, err := db.Acquire(context.Background()) + err := setupAuthTable(db) if err != nil { return err } - err = setupAuthTable(conn) + err = setupJokesTable(db) if err != nil { return err } - conn, err = db.Acquire(context.Background()) - 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) + err = setupSubmissionTable(db) if err != nil { return err } @@ -42,12 +27,16 @@ func Setup(db *pgxpool.Pool) error { 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() // Check if table exists var tableAuthExists bool - err := conn.QueryRow(context.Background(), `SELECT EXISTS ( + err = conn.QueryRow(context.Background(), `SELECT EXISTS ( SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'administrators' @@ -68,21 +57,24 @@ func setupAuthTable(conn *pgxpool.Conn) error { return err } - q, err := conn.Query(context.Background(), sql) + _, err = conn.Exec(context.Background(), sql) if err != nil { return err } - defer q.Close() } 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() // Check if table exists var tableJokesExists bool - err := conn.QueryRow(context.Background(), `SELECT EXISTS ( + err = conn.QueryRow(context.Background(), `SELECT EXISTS ( SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'jokesbapak2' @@ -102,22 +94,25 @@ func setupJokesTable(conn *pgxpool.Conn) error { return err } - q, err := conn.Query(context.Background(), sql) + _, err = conn.Exec(context.Background(), sql) if err != nil { return err } - defer q.Close() } 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() //Check if table exists var tableSubmissionExists bool - err := conn.QueryRow(context.Background(), `SELECT EXISTS ( + err = conn.QueryRow(context.Background(), `SELECT EXISTS ( SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'submission' @@ -139,11 +134,10 @@ func setupSubmissionTable(conn *pgxpool.Conn) error { return err } - q, err := conn.Query(context.Background(), sql) + _, err = conn.Exec(context.Background(), sql) if err != nil { return err } - defer q.Close() } return nil diff --git a/api/app/platform/database/placeholder.sql b/api/platform/database/placeholder.sql similarity index 100% rename from api/app/platform/database/placeholder.sql rename to api/platform/database/placeholder.sql diff --git a/api/app/routes/dependencies.go b/api/routes/dependencies.go similarity index 100% rename from api/app/routes/dependencies.go rename to api/routes/dependencies.go diff --git a/api/app/routes/health.go b/api/routes/health.go similarity index 90% rename from api/app/routes/health.go rename to api/routes/health.go index cf9e85c..3b274b3 100644 --- a/api/app/routes/health.go +++ b/api/routes/health.go @@ -1,7 +1,7 @@ package routes import ( - "jokes-bapak2-api/app/handler/health" + "jokes-bapak2-api/handler/health" "time" "github.com/gofiber/fiber/v2/middleware/cache" diff --git a/api/app/routes/joke.go b/api/routes/joke.go similarity index 94% rename from api/app/routes/joke.go rename to api/routes/joke.go index c6c0766..88d531f 100644 --- a/api/app/routes/joke.go +++ b/api/routes/joke.go @@ -1,8 +1,8 @@ package routes import ( - "jokes-bapak2-api/app/handler/joke" - "jokes-bapak2-api/app/middleware" + "jokes-bapak2-api/handler/joke" + "jokes-bapak2-api/middleware" "time" "github.com/gofiber/fiber/v2/middleware/cache" diff --git a/api/app/routes/submit.go b/api/routes/submit.go similarity index 93% rename from api/app/routes/submit.go rename to api/routes/submit.go index ce9822b..f880e8b 100644 --- a/api/app/routes/submit.go +++ b/api/routes/submit.go @@ -1,7 +1,7 @@ package routes import ( - "jokes-bapak2-api/app/handler/submit" + "jokes-bapak2-api/handler/submit" "time" "github.com/gofiber/fiber/v2" diff --git a/api/app/utils/array.go b/api/utils/array.go similarity index 100% rename from api/app/utils/array.go rename to api/utils/array.go diff --git a/api/app/utils/array_test.go b/api/utils/array_test.go similarity index 93% rename from api/app/utils/array_test.go rename to api/utils/array_test.go index 8be25de..74c36af 100644 --- a/api/app/utils/array_test.go +++ b/api/utils/array_test.go @@ -1,7 +1,7 @@ package utils_test import ( - "jokes-bapak2-api/app/utils" + "jokes-bapak2-api/utils" "testing" ) diff --git a/api/app/utils/date.go b/api/utils/date.go similarity index 100% rename from api/app/utils/date.go rename to api/utils/date.go diff --git a/api/app/utils/date_test.go b/api/utils/date_test.go similarity index 96% rename from api/app/utils/date_test.go rename to api/utils/date_test.go index a3c6c27..de59927 100644 --- a/api/app/utils/date_test.go +++ b/api/utils/date_test.go @@ -1,7 +1,7 @@ package utils_test import ( - "jokes-bapak2-api/app/utils" + "jokes-bapak2-api/utils" "testing" "time" ) diff --git a/api/app/utils/parse.go b/api/utils/parse.go similarity index 100% rename from api/app/utils/parse.go rename to api/utils/parse.go diff --git a/api/app/utils/parse_test.go b/api/utils/parse_test.go similarity index 96% rename from api/app/utils/parse_test.go rename to api/utils/parse_test.go index 7c59a8b..4388182 100644 --- a/api/app/utils/parse_test.go +++ b/api/utils/parse_test.go @@ -1,7 +1,7 @@ package utils_test import ( - "jokes-bapak2-api/app/utils" + "jokes-bapak2-api/utils" "strings" "testing" ) diff --git a/api/app/utils/random.go b/api/utils/random.go similarity index 100% rename from api/app/utils/random.go rename to api/utils/random.go diff --git a/api/app/utils/random_test.go b/api/utils/random_test.go similarity index 93% rename from api/app/utils/random_test.go rename to api/utils/random_test.go index 8f36d66..c891510 100644 --- a/api/app/utils/random_test.go +++ b/api/utils/random_test.go @@ -1,7 +1,7 @@ package utils_test import ( - "jokes-bapak2-api/app/utils" + "jokes-bapak2-api/utils" "testing" ) diff --git a/api/app/utils/request.go b/api/utils/request.go similarity index 100% rename from api/app/utils/request.go rename to api/utils/request.go diff --git a/api/app/utils/request_test.go b/api/utils/request_test.go similarity index 94% rename from api/app/utils/request_test.go rename to api/utils/request_test.go index d47bed7..95750bb 100644 --- a/api/app/utils/request_test.go +++ b/api/utils/request_test.go @@ -1,7 +1,7 @@ package utils_test import ( - "jokes-bapak2-api/app/utils" + "jokes-bapak2-api/utils" "net/http" "testing" )