refactor: directory structure

This commit is contained in:
Reinaldy Rafli 2021-10-30 10:24:53 +07:00
parent 521734246e
commit 31652fb3ab
No known key found for this signature in database
GPG Key ID: CFDB9400255D8CB6
48 changed files with 182 additions and 205 deletions

View File

@ -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",
})
}

View File

@ -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
}

View File

@ -3,7 +3,7 @@ package joke
import (
"context"
"errors"
"jokes-bapak2-api/app/core/schema"
"jokes-bapak2-api/core/schema"
"math/rand"
"strconv"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -2,7 +2,7 @@ package validator
import (
"errors"
"jokes-bapak2-api/app/utils"
"jokes-bapak2-api/utils"
"net/http"
"strings"

View File

Before

Width:  |  Height:  |  Size: 9.7 KiB

After

Width:  |  Height:  |  Size: 9.7 KiB

View File

@ -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"
)

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"
)

View File

@ -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"

View File

@ -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.

View File

@ -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"

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -1,7 +1,7 @@
package routes
import (
"jokes-bapak2-api/app/handler/submit"
"jokes-bapak2-api/handler/submit"
"time"
"github.com/gofiber/fiber/v2"

View File

@ -1,7 +1,7 @@
package utils_test
import (
"jokes-bapak2-api/app/utils"
"jokes-bapak2-api/utils"
"testing"
)

View File

@ -1,7 +1,7 @@
package utils_test
import (
"jokes-bapak2-api/app/utils"
"jokes-bapak2-api/utils"
"testing"
"time"
)

View File

@ -1,7 +1,7 @@
package utils_test
import (
"jokes-bapak2-api/app/utils"
"jokes-bapak2-api/utils"
"strings"
"testing"
)

View File

@ -1,7 +1,7 @@
package utils_test
import (
"jokes-bapak2-api/app/utils"
"jokes-bapak2-api/utils"
"testing"
)

View File

@ -1,7 +1,7 @@
package utils_test
import (
"jokes-bapak2-api/app/utils"
"jokes-bapak2-api/utils"
"net/http"
"testing"
)