Skip to content

Commit

Permalink
✨ feat: logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
shurco committed Dec 11, 2023
1 parent 88258b4 commit 268b40d
Show file tree
Hide file tree
Showing 30 changed files with 307 additions and 136 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/shurco/litecart

go 1.21.3
go 1.21.4

require (
github.com/disintegration/imaging v1.6.2
Expand All @@ -9,6 +9,7 @@ require (
github.com/gofiber/contrib/jwt v1.0.8
github.com/gofiber/fiber/v2 v2.51.0
github.com/gofiber/template/html/v2 v2.0.5
github.com/gofiber/utils v1.1.0
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/google/uuid v1.4.0
github.com/pressly/goose/v3 v3.16.0
Expand All @@ -28,14 +29,14 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-test/deep v1.1.0 // indirect
github.com/gofiber/template v1.8.2 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.18 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.4 // indirect
Expand Down
12 changes: 5 additions & 7 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"
"time"

"github.com/rs/zerolog"
"golang.org/x/crypto/acme/autocert"

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

var (
DevMode bool
// MainDomain string
log zerolog.Logger
log *logging.Log
)

// NewApp is ...
func NewApp(httpAddr, httpsAddr string, noSite, appDev bool) error {
DevMode = appDev
log = logging.Log()
log = logging.New()

schema := "http"
mainAddr := httpAddr
Expand Down Expand Up @@ -80,7 +78,7 @@ func NewApp(httpAddr, httpsAddr string, noSite, appDev bool) error {
Level: compress.LevelBestSpeed,
}))
app.Use(fiberzerolog.New(fiberzerolog.Config{
Logger: &log,
Logger: log.Logger,
}))

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

response, err := db.GetSettingByKey(ctx, "installed", "password")
response, err := db.GetSettingByKey(ctx, "installed")
if err != nil {
return webutil.StatusBadRequest(c, err.Error())
}
install, _ := strconv.ParseBool(response[0].Value.(string))
install, _ := strconv.ParseBool(response["installed"].Value.(string))

if !install {
if !strings.HasPrefix(c.Path(), "/_/install") && !strings.HasPrefix(c.Path(), "/_/assets") && !strings.HasPrefix(c.Path(), "/api") {
Expand Down
27 changes: 20 additions & 7 deletions internal/handlers/private/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/shurco/litecart/internal/models"
"github.com/shurco/litecart/internal/queries"
"github.com/shurco/litecart/pkg/jwtutil"
"github.com/shurco/litecart/pkg/logging"
"github.com/shurco/litecart/pkg/security"
"github.com/shurco/litecart/pkg/webutil"
)
Expand All @@ -17,19 +18,23 @@ import (
// [post] /api/sign/in
func SignIn(c *fiber.Ctx) error {
db := queries.DB()
log := logging.New()
request := new(models.SignIn)

if err := c.BodyParser(request); err != nil {
log.ErrorStack(err)
return webutil.StatusBadRequest(c, err.Error())
}

if err := request.Validate(); err != nil {
log.ErrorStack(err)
return webutil.StatusBadRequest(c, err.Error())
}

passwordHash, err := db.GetPasswordByEmail(c.Context(), request.Email)
if err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

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

userID := uuid.New()
expires := time.Now().Add(time.Hour * time.Duration(settingJWT.ExpireHours)).Unix()
token, err := jwtutil.GenerateNewToken(settingJWT.Secret, userID.String(), expires, nil)
if err != nil {
return webutil.Response(c, fiber.StatusInternalServerError, "Internal server error", err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

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

c.Cookie(&fiber.Cookie{
Expand All @@ -69,18 +77,23 @@ func SignIn(c *fiber.Ctx) error {
// [post] /api/sign/out
func SignOut(c *fiber.Ctx) error {
db := queries.DB()
log := logging.New()

settingJWT, err := queries.GetSettingByGroup[models.JWT](c.Context(), db)
if err != nil {
return err
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

claims, err := jwtutil.ExtractTokenMetadata(c, settingJWT.Secret)
if err != nil {
return webutil.Response(c, fiber.StatusInternalServerError, "Internal server error", err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

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

c.Cookie(&fiber.Cookie{
Expand Down
9 changes: 7 additions & 2 deletions internal/handlers/private/cart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/shurco/litecart/internal/mailer"
"github.com/shurco/litecart/internal/queries"
"github.com/shurco/litecart/pkg/logging"
"github.com/shurco/litecart/pkg/webutil"
)

// Carts is ...
// [get] /api/_/carts
func Carts(c *fiber.Ctx) error {
db := queries.DB()
log := logging.New()

products, err := db.Carts(c.Context())
if err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

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

if err := mailer.SendCartLetter(cartID); err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

return webutil.Response(c, fiber.StatusOK, "Mail sended", nil)
Expand Down
7 changes: 6 additions & 1 deletion internal/handlers/private/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@ import (

"github.com/shurco/litecart/internal/models"
"github.com/shurco/litecart/internal/queries"
"github.com/shurco/litecart/pkg/logging"
"github.com/shurco/litecart/pkg/webutil"
)

// Install is ...
// [post] /api/install
func Install(c *fiber.Ctx) error {
db := queries.DB()
log := logging.New()
request := new(models.Install)

if err := c.BodyParser(request); err != nil {
log.ErrorStack(err)
return webutil.StatusBadRequest(c, err.Error())
}

if err := request.Validate(); err != nil {
log.ErrorStack(err)
return webutil.StatusBadRequest(c, err.Error())
}

if err := db.Install(c.Context(), request); err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

return webutil.Response(c, fiber.StatusOK, "Cart installed", nil)
Expand Down
29 changes: 23 additions & 6 deletions internal/handlers/private/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import (

"github.com/shurco/litecart/internal/models"
"github.com/shurco/litecart/internal/queries"
"github.com/shurco/litecart/pkg/logging"
"github.com/shurco/litecart/pkg/webutil"
)

// Pages is ...
// [get] /api/_/pages
func Pages(c *fiber.Ctx) error {
db := queries.DB()
log := logging.New()

pages, err := db.ListPages(c.Context(), true)
if err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

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

if err := c.BodyParser(request); err != nil {
log.ErrorStack(err)
return webutil.StatusBadRequest(c, err.Error())
}

page, err := db.AddPage(c.Context(), request)
if err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

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

if err := c.BodyParser(request); err != nil {
log.ErrorStack(err)
return webutil.StatusBadRequest(c, err.Error())
}

if err := db.UpdatePage(c.Context(), request); err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

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

if err := db.DeletePage(c.Context(), pageID); err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

return webutil.Response(c, fiber.StatusOK, "Page deleted", nil)
Expand All @@ -75,19 +86,23 @@ func DeletePage(c *fiber.Ctx) error {
// [get] /api/_/page/:page_id/content
func UpdatePageContent(c *fiber.Ctx) error {
db := queries.DB()
log := logging.New()
pageID := c.Params("page_id")

request := &models.Page{
Core: models.Core{
ID: pageID,
},
}

if err := c.BodyParser(request); err != nil {
log.ErrorStack(err)
return webutil.StatusBadRequest(c, err.Error())
}

if err := db.UpdatePageContent(c.Context(), request); err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

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

if err := db.UpdatePageActive(c.Context(), pageID); err != nil {
return webutil.StatusBadRequest(c, err.Error())
log.ErrorStack(err)
return webutil.StatusInternalServerError(c)
}

return webutil.Response(c, fiber.StatusOK, "Page active updated", nil)
Expand Down
Loading

0 comments on commit 268b40d

Please sign in to comment.