Skip to content

Commit

Permalink
✨ feat: stripe product and updating ui/ux
Browse files Browse the repository at this point in the history
  • Loading branch information
shurco committed Aug 6, 2023
1 parent 7bd5a58 commit d81fe9c
Show file tree
Hide file tree
Showing 29 changed files with 692 additions and 242 deletions.
8 changes: 5 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd",
"args": ["serve"]
},
"args": ["serve", "--dev"]
//"args": ["serve"]
//"args": ["serve", "--help"]
}
]
}
}
6 changes: 2 additions & 4 deletions README
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
1. STRIPE_SECRET_KEY for install (PROD) https://dashboard.stripe.com/apikeys or (DEV) https://dashboard.stripe.com/test/apikeys

For developer:
1. Install tools ./scripts/tools/tools
2. Install https://github.com/tailwindlabs/tailwindcss/releases/
cd web/admin && ../tailwindcss -o ./public/assets/css/style.css --watch --minify
cd web/site && ../tailwindcss -o ./public/assets/css/style.css --watch --minify
1. Install tools utilities ./scripts/tools
2. Install compiling CSS ./scripts/web
55 changes: 31 additions & 24 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,46 @@ import (
)

var (
gitCommit = "00000000"
version = "0.0.1"
gitCommit = "00000000"
buildDate = "14.07.2023"
)

var devMode bool

var rootCmd = &cobra.Command{
Use: "litecart",
Short: "LiteCart CLI",
Long: "Open Source realtime cart in 1 file",
Version: fmt.Sprintf("LiteCart v%s (%s) from %s", version, gitCommit, buildDate),
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true},
}

func main() {
var serveCmd = &cobra.Command{
Use: "serve",
rootCmd.SetHelpCommand(&cobra.Command{
Use: "no-help",
Hidden: true,
})

rootCmd.AddCommand(cmdServe())

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

func cmdServe() *cobra.Command {
cmd := &cobra.Command{
Use: "serve [flags]",
Short: "Starts the web server (default to 127.0.0.1:8080)",
Run: func(cmd *cobra.Command, args []string) {
if err := app.NewApp(); err != nil {
Run: func(serveCmd *cobra.Command, args []string) {
if err := app.NewApp(devMode); err != nil {
os.Exit(1)
}
},
}
cmd.PersistentFlags().BoolVar(&devMode, "dev", false, "develop mode")

var rootCmd = &cobra.Command{
Use: "litecart",
Short: "LiteCart CLI",
Long: "Open Source realtime cart in 1 file",
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
Version: fmt.Sprintf("LiteCart v%s (%s) from %s", version, gitCommit, buildDate),
}
rootCmd.SetHelpCommand(&cobra.Command{
Use: "no-help",
Hidden: true,
})

rootCmd.AddCommand(serveCmd)
rootCmd.Execute()
return cmd
}
62 changes: 47 additions & 15 deletions internal/app.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
package app

import (
"embed"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strings"

"github.com/armon/go-proxyproto"
"github.com/rs/zerolog"

"github.com/gofiber/contrib/fiberzerolog"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"

"github.com/shurco/litecart/internal/queries"
"github.com/shurco/litecart/internal/routes"
"github.com/shurco/litecart/migrations"
"github.com/shurco/litecart/pkg/fsutil"
"github.com/shurco/litecart/pkg/logging"
)

//go:embed migrations/*.sql
var embedMigrations embed.FS

var (
DevMode bool
MainDomain string
log zerolog.Logger
)

// NewApp is ...
func NewApp() error {
DevMode = true
log := logging.Log()
func NewApp(appDev bool) error {
DevMode = appDev
log = logging.Log()

// check uploads folder
if err := fsutil.MkDirs(0775, "./uploads"); err != nil {
log.Err(err).Send()
return err
}

if err := queries.InitQueries(embedMigrations); err != nil {
// check web folder
if err := fsutil.MkDirs(0775, "./web"); err != nil {
log.Err(err).Send()
return err
}

if err := queries.InitQueries(migrations.Embed()); err != nil {
log.Err(err).Send()
return err
}
Expand Down Expand Up @@ -76,13 +84,25 @@ func NewApp() error {
routes.ApiPublicRoutes(app)
routes.NotFoundRoute(app)

ln, err := net.Listen("tcp", fmt.Sprintf(":%d", 8080))
if err != nil {
log.Err(err).Send()
}
proxyListener := &proxyproto.Listener{Listener: ln}
if err := app.Listener(proxyListener); err != nil {
log.Err(err).Send()
if DevMode {
StartServer(app)
} else {
idleConnsClosed := make(chan struct{})

go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint

if err := app.Shutdown(); err != nil {
log.Err(err).Send()
}

close(idleConnsClosed)
}()

StartServer(app)
<-idleConnsClosed
}

return nil
Expand Down Expand Up @@ -120,3 +140,15 @@ func SubdomainCheck(c *fiber.Ctx) error {
}
return c.Next()
}

// StartServer is ...
func StartServer(a *fiber.App) {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", 8080))
if err != nil {
log.Err(err).Send()
}
proxyListener := &proxyproto.Listener{Listener: ln}
if err := a.Listener(proxyListener); err != nil {
log.Err(err).Send()
}
}
2 changes: 2 additions & 0 deletions internal/handlers/private/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/shurco/litecart/pkg/webutil"
)

// SignIn is ...
func SignIn(c *fiber.Ctx) error {
db := queries.DB()
request := new(models.SignIn)
Expand Down Expand Up @@ -64,6 +65,7 @@ func SignIn(c *fiber.Ctx) error {
return webutil.StatusOK(c, "Token", token)
}

// SignOut is ...
func SignOut(c *fiber.Ctx) error {
db := queries.DB()
settingJWT, err := db.SettingJWT()
Expand Down
113 changes: 109 additions & 4 deletions internal/handlers/private/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@ package handlers
import (
"github.com/gofiber/fiber/v2"

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

// Products is ...
// [get] /api/products
func Products(c *fiber.Ctx) error {
db := queries.DB()

products, err := db.ListProducts()
if err != nil {
return webutil.StatusBadRequest(c, err.Error())
}

return webutil.Response(c, fiber.StatusOK, "Products", products)
}

// GetProduct is ...
// [get] /api/products/:id
func Product(c *fiber.Ctx) error {
Expand All @@ -18,20 +33,110 @@ func Product(c *fiber.Ctx) error {
return webutil.StatusBadRequest(c, err.Error())
}

return webutil.Response(c, fiber.StatusOK, "Product information", product)
return webutil.Response(c, fiber.StatusOK, "Product info", product)
}

// AddProduct is ...
func AddProduct(c *fiber.Ctx) error {
return webutil.Response(c, fiber.StatusOK, "AddProduct", nil)
db := queries.DB()
request := new(models.Product)

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

if err := validator.Struct(request); err != nil {
return webutil.StatusBadRequest(c, err)
}

if err := db.AddProduct(request); err != nil {
return webutil.StatusBadRequest(c, err.Error())
}

return webutil.Response(c, fiber.StatusOK, "Product added", nil)
}

// UpdateProduct is ...
func UpdateProduct(c *fiber.Ctx) error {
return webutil.Response(c, fiber.StatusOK, "UpdateProduct", nil)
db := queries.DB()
request := new(models.Product)

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

if err := validator.Struct(request); err != nil {
return webutil.StatusBadRequest(c, err)
}

if err := db.UpdateProduct(request); err != nil {
return webutil.StatusBadRequest(c, err.Error())
}

return webutil.Response(c, fiber.StatusOK, "Product updated", nil)
}

// UpdateProductActive is ...
// [patch] /api/active/:id
func UpdateProductActive(c *fiber.Ctx) error {
id := c.Params("id")
db := queries.DB()

if err := db.UpdateActive(id); err != nil {
return webutil.StatusBadRequest(c, err.Error())
}

return webutil.Response(c, fiber.StatusOK, "Product active updated", nil)
}

// DeleteProduct is ...
func DeleteProduct(c *fiber.Ctx) error {
return webutil.Response(c, fiber.StatusOK, "DeleteProduct", nil)
id := c.Params("id")
db := queries.DB()

if err := db.DeleteProduct(id); err != nil {
if err == queries.StripeProductNotFound {
return webutil.StatusNotFound(c)
}
return webutil.StatusBadRequest(c, err.Error())
}

return webutil.Response(c, fiber.StatusOK, "Product deleted", nil)
}

// AddStripeProduct is ...
func AddStripeProduct(c *fiber.Ctx) error {
id := c.Params("id")
db := queries.DB()

stripeID, err := db.AddStripeProduct(id)
if err != nil {
return webutil.StatusBadRequest(c, err.Error())
}

if err := db.UpdateStripeProduct(id, stripeID); err != nil {
return webutil.StatusBadRequest(c, err.Error())
}

return webutil.Response(c, fiber.StatusOK, "Stripe id", stripeID)
}

// DeleteStripeProduct is ...
func DeleteStripeProduct(c *fiber.Ctx) error {
id := c.Params("id")
db := queries.DB()

if err := db.DeleteStripeProduct(id); err != nil {
return webutil.StatusBadRequest(c, err.Error())
}

return webutil.Response(c, fiber.StatusOK, "Stripe product deleted", nil)
}

// CheckStripeProduct is ...
func CheckStripeProduct(c *fiber.Ctx) error {
id := c.Params("id")
db := queries.DB()

return webutil.Response(c, fiber.StatusOK, "Stripe product check", db.IsStripeProduct(id))
}
10 changes: 9 additions & 1 deletion internal/handlers/private/product.http
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Content-Type: application/json
"password":"123456789ASDFGzxcvb"
}


###
# @name products
GET {{host}}/api/products
Expand All @@ -26,3 +25,12 @@ Authorization: Bearer {{signin.response.body.result}}
GET {{host}}/api/products/fv6c9s9cqzf36sc
Authorization: Bearer {{signin.response.body.result}}

###
# @name active
PATCH {{host}}/api/products/active/fv6c9s9cqzf36sc
Authorization: Bearer {{signin.response.body.result}}

###
# @name check
GET {{host}}/api/products/stripe/fv6c9s9cqzf36sc/check
Authorization: Bearer {{signin.response.body.result}}
2 changes: 2 additions & 0 deletions internal/models/auth.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package models

// SignIn is ...
type SignIn struct {
Email string `json:"email" validate:"required,email,lte=255"`
Password string `json:"password" validate:"required,lte=255"`
}

// Renew is ...
type Renew struct {
RefreshToken string `json:"refresh_token"`
}
Loading

0 comments on commit d81fe9c

Please sign in to comment.