Skip to content

Commit 268b40d

Browse files
committed
✨ feat: logging system
1 parent 88258b4 commit 268b40d

File tree

30 files changed

+307
-136
lines changed

30 files changed

+307
-136
lines changed

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/shurco/litecart
22

3-
go 1.21.3
3+
go 1.21.4
44

55
require (
66
github.com/disintegration/imaging v1.6.2
@@ -9,6 +9,7 @@ require (
99
github.com/gofiber/contrib/jwt v1.0.8
1010
github.com/gofiber/fiber/v2 v2.51.0
1111
github.com/gofiber/template/html/v2 v2.0.5
12+
github.com/gofiber/utils v1.1.0
1213
github.com/golang-jwt/jwt/v5 v5.2.0
1314
github.com/google/uuid v1.4.0
1415
github.com/pressly/goose/v3 v3.16.0
@@ -28,14 +29,14 @@ require (
2829
github.com/dustin/go-humanize v1.0.1 // indirect
2930
github.com/go-test/deep v1.1.0 // indirect
3031
github.com/gofiber/template v1.8.2 // indirect
31-
github.com/gofiber/utils v1.1.0 // indirect
3232
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3333
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
3434
github.com/klauspost/compress v1.17.4 // indirect
3535
github.com/mattn/go-colorable v0.1.13 // indirect
3636
github.com/mattn/go-isatty v0.0.20 // indirect
3737
github.com/mattn/go-runewidth v0.0.15 // indirect
3838
github.com/mattn/go-sqlite3 v1.14.18 // indirect
39+
github.com/pkg/errors v0.9.1 // indirect
3940
github.com/pmezard/go-difflib v1.0.0 // indirect
4041
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
4142
github.com/rivo/uniseg v0.4.4 // indirect

internal/app.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/rs/zerolog"
1413
"golang.org/x/crypto/acme/autocert"
1514

1615
"github.com/gofiber/contrib/fiberzerolog"
@@ -30,14 +29,13 @@ import (
3029

3130
var (
3231
DevMode bool
33-
// MainDomain string
34-
log zerolog.Logger
32+
log *logging.Log
3533
)
3634

3735
// NewApp is ...
3836
func NewApp(httpAddr, httpsAddr string, noSite, appDev bool) error {
3937
DevMode = appDev
40-
log = logging.Log()
38+
log = logging.New()
4139

4240
schema := "http"
4341
mainAddr := httpAddr
@@ -80,7 +78,7 @@ func NewApp(httpAddr, httpsAddr string, noSite, appDev bool) error {
8078
Level: compress.LevelBestSpeed,
8179
}))
8280
app.Use(fiberzerolog.New(fiberzerolog.Config{
83-
Logger: &log,
81+
Logger: log.Logger,
8482
}))
8583

8684
// init structure
@@ -163,11 +161,11 @@ func InstallCheck(c *fiber.Ctx) error {
163161
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
164162
defer cancel()
165163

166-
response, err := db.GetSettingByKey(ctx, "installed", "password")
164+
response, err := db.GetSettingByKey(ctx, "installed")
167165
if err != nil {
168166
return webutil.StatusBadRequest(c, err.Error())
169167
}
170-
install, _ := strconv.ParseBool(response[0].Value.(string))
168+
install, _ := strconv.ParseBool(response["installed"].Value.(string))
171169

172170
if !install {
173171
if !strings.HasPrefix(c.Path(), "/_/install") && !strings.HasPrefix(c.Path(), "/_/assets") && !strings.HasPrefix(c.Path(), "/api") {

internal/handlers/private/auth.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/shurco/litecart/internal/models"
1010
"github.com/shurco/litecart/internal/queries"
1111
"github.com/shurco/litecart/pkg/jwtutil"
12+
"github.com/shurco/litecart/pkg/logging"
1213
"github.com/shurco/litecart/pkg/security"
1314
"github.com/shurco/litecart/pkg/webutil"
1415
)
@@ -17,19 +18,23 @@ import (
1718
// [post] /api/sign/in
1819
func SignIn(c *fiber.Ctx) error {
1920
db := queries.DB()
21+
log := logging.New()
2022
request := new(models.SignIn)
2123

2224
if err := c.BodyParser(request); err != nil {
25+
log.ErrorStack(err)
2326
return webutil.StatusBadRequest(c, err.Error())
2427
}
2528

2629
if err := request.Validate(); err != nil {
30+
log.ErrorStack(err)
2731
return webutil.StatusBadRequest(c, err.Error())
2832
}
2933

3034
passwordHash, err := db.GetPasswordByEmail(c.Context(), request.Email)
3135
if err != nil {
32-
return webutil.StatusBadRequest(c, err.Error())
36+
log.ErrorStack(err)
37+
return webutil.StatusInternalServerError(c)
3338
}
3439

3540
compareUserPassword := security.ComparePasswords(passwordHash, request.Password)
@@ -40,19 +45,22 @@ func SignIn(c *fiber.Ctx) error {
4045
// Generate a new pair of access and refresh tokens.
4146
settingJWT, err := queries.GetSettingByGroup[models.JWT](c.Context(), db)
4247
if err != nil {
43-
return err
48+
log.ErrorStack(err)
49+
return webutil.StatusInternalServerError(c)
4450
}
4551

4652
userID := uuid.New()
4753
expires := time.Now().Add(time.Hour * time.Duration(settingJWT.ExpireHours)).Unix()
4854
token, err := jwtutil.GenerateNewToken(settingJWT.Secret, userID.String(), expires, nil)
4955
if err != nil {
50-
return webutil.Response(c, fiber.StatusInternalServerError, "Internal server error", err.Error())
56+
log.ErrorStack(err)
57+
return webutil.StatusInternalServerError(c)
5158
}
5259

5360
// Add session record
5461
if err := db.AddSession(c.Context(), userID.String(), "admin", expires); err != nil {
55-
return webutil.Response(c, fiber.StatusInternalServerError, "Failed to save token", err.Error())
62+
log.ErrorStack(err)
63+
return webutil.StatusInternalServerError(c)
5664
}
5765

5866
c.Cookie(&fiber.Cookie{
@@ -69,18 +77,23 @@ func SignIn(c *fiber.Ctx) error {
6977
// [post] /api/sign/out
7078
func SignOut(c *fiber.Ctx) error {
7179
db := queries.DB()
80+
log := logging.New()
81+
7282
settingJWT, err := queries.GetSettingByGroup[models.JWT](c.Context(), db)
7383
if err != nil {
74-
return err
84+
log.ErrorStack(err)
85+
return webutil.StatusInternalServerError(c)
7586
}
7687

7788
claims, err := jwtutil.ExtractTokenMetadata(c, settingJWT.Secret)
7889
if err != nil {
79-
return webutil.Response(c, fiber.StatusInternalServerError, "Internal server error", err.Error())
90+
log.ErrorStack(err)
91+
return webutil.StatusInternalServerError(c)
8092
}
8193

8294
if err := db.DeleteSession(c.Context(), claims.ID); err != nil {
83-
return webutil.Response(c, fiber.StatusInternalServerError, "Failed to delete token", err.Error())
95+
log.ErrorStack(err)
96+
return webutil.StatusInternalServerError(c)
8497
}
8598

8699
c.Cookie(&fiber.Cookie{

internal/handlers/private/cart.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ import (
44
"github.com/gofiber/fiber/v2"
55
"github.com/shurco/litecart/internal/mailer"
66
"github.com/shurco/litecart/internal/queries"
7+
"github.com/shurco/litecart/pkg/logging"
78
"github.com/shurco/litecart/pkg/webutil"
89
)
910

1011
// Carts is ...
1112
// [get] /api/_/carts
1213
func Carts(c *fiber.Ctx) error {
1314
db := queries.DB()
15+
log := logging.New()
1416

1517
products, err := db.Carts(c.Context())
1618
if err != nil {
17-
return webutil.StatusBadRequest(c, err.Error())
19+
log.ErrorStack(err)
20+
return webutil.StatusInternalServerError(c)
1821
}
1922

2023
return webutil.Response(c, fiber.StatusOK, "Carts", products)
@@ -24,9 +27,11 @@ func Carts(c *fiber.Ctx) error {
2427
// [post] /api/_/carts/:cart_id/mail
2528
func CartSendMail(c *fiber.Ctx) error {
2629
cartID := c.Params("cart_id")
30+
log := logging.New()
2731

2832
if err := mailer.SendCartLetter(cartID); err != nil {
29-
return webutil.StatusBadRequest(c, err.Error())
33+
log.ErrorStack(err)
34+
return webutil.StatusInternalServerError(c)
3035
}
3136

3237
return webutil.Response(c, fiber.StatusOK, "Mail sended", nil)

internal/handlers/private/install.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,30 @@ import (
55

66
"github.com/shurco/litecart/internal/models"
77
"github.com/shurco/litecart/internal/queries"
8+
"github.com/shurco/litecart/pkg/logging"
89
"github.com/shurco/litecart/pkg/webutil"
910
)
1011

1112
// Install is ...
1213
// [post] /api/install
1314
func Install(c *fiber.Ctx) error {
1415
db := queries.DB()
16+
log := logging.New()
1517
request := new(models.Install)
1618

1719
if err := c.BodyParser(request); err != nil {
20+
log.ErrorStack(err)
1821
return webutil.StatusBadRequest(c, err.Error())
1922
}
2023

2124
if err := request.Validate(); err != nil {
25+
log.ErrorStack(err)
2226
return webutil.StatusBadRequest(c, err.Error())
2327
}
2428

2529
if err := db.Install(c.Context(), request); err != nil {
26-
return webutil.StatusBadRequest(c, err.Error())
30+
log.ErrorStack(err)
31+
return webutil.StatusInternalServerError(c)
2732
}
2833

2934
return webutil.Response(c, fiber.StatusOK, "Cart installed", nil)

internal/handlers/private/page.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ import (
55

66
"github.com/shurco/litecart/internal/models"
77
"github.com/shurco/litecart/internal/queries"
8+
"github.com/shurco/litecart/pkg/logging"
89
"github.com/shurco/litecart/pkg/webutil"
910
)
1011

1112
// Pages is ...
1213
// [get] /api/_/pages
1314
func Pages(c *fiber.Ctx) error {
1415
db := queries.DB()
16+
log := logging.New()
1517

1618
pages, err := db.ListPages(c.Context(), true)
1719
if err != nil {
18-
return webutil.StatusBadRequest(c, err.Error())
20+
log.ErrorStack(err)
21+
return webutil.StatusInternalServerError(c)
1922
}
2023

2124
return webutil.Response(c, fiber.StatusOK, "Pages", pages)
@@ -25,15 +28,18 @@ func Pages(c *fiber.Ctx) error {
2528
// [post] /api/_/page/
2629
func AddPage(c *fiber.Ctx) error {
2730
db := queries.DB()
31+
log := logging.New()
2832
request := new(models.Page)
2933

3034
if err := c.BodyParser(request); err != nil {
35+
log.ErrorStack(err)
3136
return webutil.StatusBadRequest(c, err.Error())
3237
}
3338

3439
page, err := db.AddPage(c.Context(), request)
3540
if err != nil {
36-
return webutil.StatusBadRequest(c, err.Error())
41+
log.ErrorStack(err)
42+
return webutil.StatusInternalServerError(c)
3743
}
3844

3945
return webutil.Response(c, fiber.StatusOK, "Page added", page)
@@ -44,15 +50,18 @@ func AddPage(c *fiber.Ctx) error {
4450
func UpdatePage(c *fiber.Ctx) error {
4551
pageID := c.Params("page_id")
4652
db := queries.DB()
53+
log := logging.New()
4754
request := new(models.Page)
4855
request.ID = pageID
4956

5057
if err := c.BodyParser(request); err != nil {
58+
log.ErrorStack(err)
5159
return webutil.StatusBadRequest(c, err.Error())
5260
}
5361

5462
if err := db.UpdatePage(c.Context(), request); err != nil {
55-
return webutil.StatusBadRequest(c, err.Error())
63+
log.ErrorStack(err)
64+
return webutil.StatusInternalServerError(c)
5665
}
5766

5867
return webutil.Response(c, fiber.StatusOK, "Page updated", nil)
@@ -63,9 +72,11 @@ func UpdatePage(c *fiber.Ctx) error {
6372
func DeletePage(c *fiber.Ctx) error {
6473
pageID := c.Params("page_id")
6574
db := queries.DB()
75+
log := logging.New()
6676

6777
if err := db.DeletePage(c.Context(), pageID); err != nil {
68-
return webutil.StatusBadRequest(c, err.Error())
78+
log.ErrorStack(err)
79+
return webutil.StatusInternalServerError(c)
6980
}
7081

7182
return webutil.Response(c, fiber.StatusOK, "Page deleted", nil)
@@ -75,19 +86,23 @@ func DeletePage(c *fiber.Ctx) error {
7586
// [get] /api/_/page/:page_id/content
7687
func UpdatePageContent(c *fiber.Ctx) error {
7788
db := queries.DB()
89+
log := logging.New()
7890
pageID := c.Params("page_id")
91+
7992
request := &models.Page{
8093
Core: models.Core{
8194
ID: pageID,
8295
},
8396
}
8497

8598
if err := c.BodyParser(request); err != nil {
99+
log.ErrorStack(err)
86100
return webutil.StatusBadRequest(c, err.Error())
87101
}
88102

89103
if err := db.UpdatePageContent(c.Context(), request); err != nil {
90-
return webutil.StatusBadRequest(c, err.Error())
104+
log.ErrorStack(err)
105+
return webutil.StatusInternalServerError(c)
91106
}
92107

93108
return webutil.Response(c, fiber.StatusOK, "Page content updated", nil)
@@ -98,9 +113,11 @@ func UpdatePageContent(c *fiber.Ctx) error {
98113
func UpdatePageActive(c *fiber.Ctx) error {
99114
pageID := c.Params("page_id")
100115
db := queries.DB()
116+
log := logging.New()
101117

102118
if err := db.UpdatePageActive(c.Context(), pageID); err != nil {
103-
return webutil.StatusBadRequest(c, err.Error())
119+
log.ErrorStack(err)
120+
return webutil.StatusInternalServerError(c)
104121
}
105122

106123
return webutil.Response(c, fiber.StatusOK, "Page active updated", nil)

0 commit comments

Comments
 (0)