From ae11ee7ed8d61bd81643d577adbfa6152daee373 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Tue, 28 Sep 2021 11:29:39 +0700 Subject: [PATCH] fix: verification cleanup --- api/Dockerfile | 2 +- api/app/app.go | 18 +++++----- api/app/handler/joke/joke_add.go | 9 +++-- api/app/handler/joke/joke_update.go | 5 +-- api/app/handler/submit/submit_add.go | 6 ++-- api/app/handler/submit/submit_get.go | 9 +++-- api/app/platform/database/create.go | 28 +++++++++++---- api/main_test.go | 51 ++++++++++++---------------- 8 files changed, 71 insertions(+), 57 deletions(-) diff --git a/api/Dockerfile b/api/Dockerfile index 6599306..b0c07de 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.16.6-buster +FROM golang:1.17-buster WORKDIR /app diff --git a/api/app/app.go b/api/app/app.go index 9b4972b..79aef00 100644 --- a/api/app/app.go +++ b/api/app/app.go @@ -29,14 +29,16 @@ func New() *fiber.App { // Setup PostgreSQL poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL")) if err != nil { - log.Fatalln("Unable to create pool config", err) + log.Panicln("Unable to create pool config", err) } + poolConfig.MaxConnIdleTime = time.Minute * 3 + poolConfig.MaxConnLifetime = time.Minute * 5 poolConfig.MaxConns = 15 - poolConfig.MinConns = 2 + poolConfig.MinConns = 4 db, err := pgxpool.ConnectConfig(ctx, poolConfig) if err != nil { - log.Fatalln("Unable to create connection", err) + log.Panicln("Unable to create connection", err) } // Setup Redis @@ -49,7 +51,7 @@ func New() *fiber.App { // Setup In Memory memory, err := bigcache.NewBigCache(bigcache.DefaultConfig(6 * time.Hour)) if err != nil { - log.Fatalln(err) + log.Panicln(err) } // Setup Sentry @@ -62,23 +64,23 @@ func New() *fiber.App { Debug: true, }) if err != nil { - log.Fatalln(err) + log.Panicln(err) } defer sentry.Flush(2 * time.Second) err = database.Setup(db, &ctx) if err != nil { sentry.CaptureException(err) - log.Fatalln(err) + log.Panicln(err) } err = core.SetAllJSONJoke(db, memory, &ctx) if err != nil { - log.Fatalln(err) + log.Panicln(err) } err = core.SetTotalJoke(db, memory, &ctx) if err != nil { - log.Fatalln(err) + log.Panicln(err) } timeoutDefault := time.Minute * 1 diff --git a/api/app/handler/joke/joke_add.go b/api/app/handler/joke/joke_add.go index f5ae547..baa6ec8 100644 --- a/api/app/handler/joke/joke_add.go +++ b/api/app/handler/joke/joke_add.go @@ -49,14 +49,13 @@ func (d *Dependencies) AddNewJoke(c *fiber.Ctx) error { if err != nil { return err } - - v, err := conn.Query(*d.Context, sql, args...) + var validateLink string + err = conn.QueryRow(*d.Context, sql, args...).Scan(&validateLink) if err != nil && err != pgx.ErrNoRows { return err } - defer v.Close() - - if err == nil { + + if err == nil && validateLink != "" { return c.Status(fiber.StatusConflict).JSON(Error{ Error: "Given link is already on the jokesbapak2 database", }) diff --git a/api/app/handler/joke/joke_update.go b/api/app/handler/joke/joke_update.go index ffc426d..6ab0cd3 100644 --- a/api/app/handler/joke/joke_update.go +++ b/api/app/handler/joke/joke_update.go @@ -2,6 +2,7 @@ package joke import ( "jokes-bapak2-api/app/core" + "strconv" "github.com/Masterminds/squirrel" "github.com/gofiber/fiber/v2" @@ -26,13 +27,13 @@ func (d *Dependencies) UpdateJoke(c *fiber.Ctx) error { return err } - var jokeID string + var jokeID int err = conn.QueryRow(*d.Context, sql, args...).Scan(&jokeID) if err != nil && err != pgx.ErrNoRows { return err } - if jokeID == id { + if strconv.Itoa(jokeID) == id { body := new(core.Joke) err = c.BodyParser(&body) if err != nil { diff --git a/api/app/handler/submit/submit_add.go b/api/app/handler/submit/submit_add.go index 66ea9f0..6afd4e5 100644 --- a/api/app/handler/submit/submit_add.go +++ b/api/app/handler/submit/submit_add.go @@ -83,13 +83,13 @@ func (d *Dependencies) SubmitJoke(c *fiber.Ctx) error { return err } - v, err := conn.Query(*d.Context, sql, args...) + var validateLink string + err = conn.QueryRow(*d.Context, sql, args...).Scan(&validateLink) if err != nil && err != pgx.ErrNoRows { return err } - defer v.Close() - if err == nil { + if err == nil && validateLink != "" { return c.Status(fiber.StatusConflict).JSON(Error{ Error: "Given link is already on the submission queue.", }) diff --git a/api/app/handler/submit/submit_get.go b/api/app/handler/submit/submit_get.go index cb26198..2b16163 100644 --- a/api/app/handler/submit/submit_get.go +++ b/api/app/handler/submit/submit_get.go @@ -2,7 +2,7 @@ package submit import ( "bytes" - "log" + "net/url" "strconv" "github.com/aldy505/bob" @@ -58,7 +58,11 @@ func (d *Dependencies) GetSubmission(c *fiber.Ctx) error { if query.Author != "" { sqlQuery.WriteString(" AND author = ?") - args = append(args, query.Author) + escapedAuthor, err := url.QueryUnescape(query.Author) + if err != nil { + return err + } + args = append(args, escapedAuthor) } if query.Approved != "" { @@ -81,7 +85,6 @@ func (d *Dependencies) GetSubmission(c *fiber.Ctx) error { var submissions []Submission results, err := d.DB.Query(*d.Context, sql, args...) if err != nil { - log.Println(err) return err } diff --git a/api/app/platform/database/create.go b/api/app/platform/database/create.go index 44b2cfa..1b4ea70 100644 --- a/api/app/platform/database/create.go +++ b/api/app/platform/database/create.go @@ -12,6 +12,7 @@ import ( func Setup(db *pgxpool.Pool, ctx *context.Context) error { conn, err := db.Acquire(*ctx) if err != nil { + log.Fatalln("30 - err here") return err } defer conn.Release() @@ -41,18 +42,26 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error { return err } - _, err = conn.Query(*ctx, sql) + q, err := conn.Query(*ctx, sql) if err != nil { log.Fatalln("18 - failed on table creation: ", err) return err } + defer q.Close() } + conn2, err := db.Acquire(*ctx) + if err != nil { + log.Fatalln("32 - err here") + return err + } + defer conn2.Release() + // Jokesbapak2 table // Check if table exists var tableJokesExists bool - err = conn.QueryRow(*ctx, `SELECT EXISTS ( + err = conn2.QueryRow(*ctx, `SELECT EXISTS ( SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'jokesbapak2' @@ -74,22 +83,28 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error { return err } - _, err = conn.Query(*ctx, sql) + q, err := conn2.Query(*ctx, sql) if err != nil { log.Fatalln("12 - failed on table creation: ", err) return err } + defer q.Close() } // Submission table + conn3, err := db.Acquire(*ctx) + if err != nil { + return err + } + defer conn3.Release() //Check if table exists var tableSubmissionExists bool - err = conn.QueryRow(*ctx, `SELECT EXISTS ( + err = conn3.QueryRow(*ctx, `SELECT EXISTS ( SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'submission' - );`).Scan(&tableJokesExists) + );`).Scan(&tableSubmissionExists) if err != nil { log.Fatalln("13 - failed on checking table: ", err) return err @@ -108,10 +123,11 @@ func Setup(db *pgxpool.Pool, ctx *context.Context) error { log.Fatalln("14 - failed on table creation: ", err) } - _, err = conn.Query(*ctx, sql) + q, err := conn3.Query(*ctx, sql) if err != nil { log.Fatalln("15 - failed on table creation: ", err) } + defer q.Close() } return nil diff --git a/api/main_test.go b/api/main_test.go index 75df18b..6d43f5d 100644 --- a/api/main_test.go +++ b/api/main_test.go @@ -23,7 +23,7 @@ func TestMain(m *testing.M) { log.Println("---- Preparing for integration test") err := setup() if err != nil { - log.Fatalln(err) + log.Panicln(err) } log.Println("---- Preparation complete") log.Print("\n") @@ -37,8 +37,6 @@ func setup() error { if err != nil { return errors.New("Unable to create pool config: " + err.Error()) } - poolConfig.MaxConns = 15 - poolConfig.MinConns = 2 db, err := pgxpool.ConnectConfig(ctx, poolConfig) if err != nil { @@ -46,32 +44,27 @@ func setup() error { } defer db.Close() - conn, err := db.Acquire(ctx) + + dj, err := db.Query(ctx, "DROP TABLE \"jokesbapak2\"") + if err != nil { + log.Println("busy here - 57") + return err + } + defer dj.Close() + + ds, err := db.Query(ctx, "DROP TABLE \"submission\"") + if err != nil { + log.Println("busy here - 67") + return err + } + defer ds.Close() + + da, err := db.Query(ctx, "DROP TABLE \"administrators\"") if err != nil { + log.Println("busy here - 62") return err } - defer conn.Release() - - // dj, err := conn.Query(ctx, "DROP TABLE \"jokesbapak2\"") - // if err != nil { - // log.Println("busy here - 57") - // return err - // } - // defer dj.Close() - - // ds, err := conn.Query(ctx, "DROP TABLE \"submission\"") - // if err != nil { - // log.Println("busy here - 67") - // return err - // } - // defer ds.Close() - - // da, err := conn.Query(ctx, "DROP TABLE \"administrators\"") - // if err != nil { - // log.Println("busy here - 62") - // return err - // } - // defer da.Close() + defer da.Close() err = database.Setup(db, &ctx) if err != nil { @@ -79,19 +72,19 @@ func setup() error { return err } - ia, err := conn.Query(ctx, "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4), ($5, $6, $7, $8);", administratorsData...) + ia, err := db.Query(ctx, "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4), ($5, $6, $7, $8);", administratorsData...) if err != nil { return err } defer ia.Close() - ij, err := conn.Query(ctx, "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...) + ij, err := db.Query(ctx, "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...) if err != nil { return err } defer ij.Close() - is, err := conn.Query(ctx, "INSERT INTO \"submission\" (id, link, created_at, author, status) VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10);", submissionData...) + is, err := db.Query(ctx, "INSERT INTO \"submission\" (id, link, created_at, author, status) VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10);", submissionData...) if err != nil { return err }