From 8b37fa1e025a6de2f214ca3802cb13a88a32c6d4 Mon Sep 17 00:00:00 2001 From: mrmissx Date: Sun, 3 Mar 2024 15:25:36 +0700 Subject: [PATCH] refactor(model): move to database dir added database connection pool config --- database/database.go | 6 ++++++ {model => database/model}/document.go | 0 handler/api/document.go | 4 ++-- handler/api/health.go | 2 +- handler/page/about.go | 2 +- handler/page/document.go | 4 ++-- handler/page/index.go | 2 +- handler/page/raw.go | 2 +- {response => utils/response}/error.go | 0 {response => utils/response}/html.go | 0 {response => utils/response}/json.go | 0 11 files changed, 14 insertions(+), 8 deletions(-) rename {model => database/model}/document.go (100%) rename {response => utils/response}/error.go (100%) rename {response => utils/response}/html.go (100%) rename {response => utils/response}/json.go (100%) diff --git a/database/database.go b/database/database.go index a33990d..b44dfed 100644 --- a/database/database.go +++ b/database/database.go @@ -1,6 +1,8 @@ package database import ( + "time" + "github.com/jmoiron/sqlx" _ "github.com/lib/pq" "github.com/mrmissx/stashbin/utils" @@ -17,6 +19,10 @@ func ConnectDb() { utils.Logger.Info("Connecting to database...") db := sqlx.MustConnect("postgres", dbUri) + db.SetMaxIdleConns(5) + db.SetMaxOpenConns(10) + db.SetConnMaxIdleTime(10 * time.Minute) + db.SetConnMaxLifetime(1 * time.Hour) session = db utils.Logger.Info("Successfuly connected database") diff --git a/model/document.go b/database/model/document.go similarity index 100% rename from model/document.go rename to database/model/document.go diff --git a/handler/api/document.go b/handler/api/document.go index a15cfdd..b0cedf7 100644 --- a/handler/api/document.go +++ b/handler/api/document.go @@ -5,8 +5,8 @@ import ( "github.com/jmoiron/sqlx" "github.com/labstack/echo/v4" - "github.com/mrmissx/stashbin/model" - "github.com/mrmissx/stashbin/response" + "github.com/mrmissx/stashbin/database/model" + "github.com/mrmissx/stashbin/utils/response" ) func GetDocumentBySlug(c echo.Context) error { diff --git a/handler/api/health.go b/handler/api/health.go index 866a6c6..2dad31a 100644 --- a/handler/api/health.go +++ b/handler/api/health.go @@ -4,7 +4,7 @@ import ( "net/http" "github.com/labstack/echo/v4" - "github.com/mrmissx/stashbin/response" + "github.com/mrmissx/stashbin/utils/response" ) func HealthCheck(c echo.Context) error { diff --git a/handler/page/about.go b/handler/page/about.go index 57a6d5a..9f48029 100644 --- a/handler/page/about.go +++ b/handler/page/about.go @@ -5,7 +5,7 @@ import ( "github.com/labstack/echo/v4" - "github.com/mrmissx/stashbin/response" + "github.com/mrmissx/stashbin/utils/response" "github.com/mrmissx/stashbin/view" ) diff --git a/handler/page/document.go b/handler/page/document.go index 00619b2..e78ca9b 100644 --- a/handler/page/document.go +++ b/handler/page/document.go @@ -6,8 +6,8 @@ import ( "github.com/jmoiron/sqlx" "github.com/labstack/echo/v4" - "github.com/mrmissx/stashbin/model" - "github.com/mrmissx/stashbin/response" + "github.com/mrmissx/stashbin/database/model" + "github.com/mrmissx/stashbin/utils/response" "github.com/mrmissx/stashbin/view" ) diff --git a/handler/page/index.go b/handler/page/index.go index 721b791..820502c 100644 --- a/handler/page/index.go +++ b/handler/page/index.go @@ -5,7 +5,7 @@ import ( "github.com/labstack/echo/v4" - "github.com/mrmissx/stashbin/response" + "github.com/mrmissx/stashbin/utils/response" "github.com/mrmissx/stashbin/view" ) diff --git a/handler/page/raw.go b/handler/page/raw.go index 63ab3aa..11f5a53 100644 --- a/handler/page/raw.go +++ b/handler/page/raw.go @@ -6,7 +6,7 @@ import ( "github.com/jmoiron/sqlx" "github.com/labstack/echo/v4" - "github.com/mrmissx/stashbin/model" + "github.com/mrmissx/stashbin/database/model" ) func RawPageHandler(c echo.Context) error { diff --git a/response/error.go b/utils/response/error.go similarity index 100% rename from response/error.go rename to utils/response/error.go diff --git a/response/html.go b/utils/response/html.go similarity index 100% rename from response/html.go rename to utils/response/html.go diff --git a/response/json.go b/utils/response/json.go similarity index 100% rename from response/json.go rename to utils/response/json.go