Skip to content

Commit

Permalink
🦄 refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shurco committed Jul 25, 2023
1 parent 195e26a commit b8193f2
Show file tree
Hide file tree
Showing 37 changed files with 384 additions and 510 deletions.
5 changes: 2 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"log"
"os"

"github.com/shurco/litecart/internal/app"
"github.com/shurco/litecart/internal/core"
app "github.com/shurco/litecart/internal"
)

var (
Expand All @@ -15,7 +14,7 @@ var (
)

func main() {
flags := core.Flags{
flags := app.Flags{
Serve: true,
}

Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
Expand All @@ -78,8 +76,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stripe/stripe-go/v74 v74.26.0 h1:enbhLtjKGWvJKcGM0f2CazqFSXzpHXcQ42nG2PNsWK0=
github.com/stripe/stripe-go/v74 v74.26.0/go.mod h1:f9L6LvaXa35ja7eyvP6GQswoaIPaBRvGAimAO+udbBw=
github.com/stripe/stripe-go/v74 v74.27.0 h1:e2uVDaWQeOp8i1c11BGIOOv6yN1+7b7QfYyiF9QzAa0=
github.com/stripe/stripe-go/v74 v74.27.0/go.mod h1:f9L6LvaXa35ja7eyvP6GQswoaIPaBRvGAimAO+udbBw=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down Expand Up @@ -111,7 +107,6 @@ golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
60 changes: 0 additions & 60 deletions golangci.yaml

This file was deleted.

125 changes: 125 additions & 0 deletions internal/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package app

import (
"embed"
"fmt"
"net"
"net/http"
"os"
"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/pkg/fsutil"
)

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

var (
DevMode bool
MainDomain string
)

type Flags struct {
Serve bool
}

// NewApp is ...
func NewApp(flags Flags) error {
DevMode = true
log := zerolog.New(os.Stderr).With().Timestamp().Logger()

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

if err := queries.InitQueries(embedMigrations); err != nil {
log.Err(err).Send()
return err
}
db := queries.DB()

// web web server
var views *html.Engine
if DevMode {
views = html.New("../web/views", ".html")
views.Reload(true)
} else {
views = html.NewFileSystem(http.Dir("../web/views"), ".html")
}
views.Delims("{#", "#}")
views.AddFunc(
"arr", func(els ...any) []any {
return els
},
)

app := fiber.New(fiber.Config{
DisableStartupMessage: true,
Views: views,
})

app.Use(fiberzerolog.New(fiberzerolog.Config{
Logger: &log,
}))

app.Static("/", "../web/public")
app.Static("/uploads", "./uploads")

app.Use(func(c *fiber.Ctx) error {
// init install
mainPath := strings.Split(c.Path(), "/")[1]
if !db.IsInstalled() {
if c.Path() != "/_/install" && mainPath != "api" {
return c.Redirect("/_/install")
}
} else if c.Path() == "/_/install" {
return c.Redirect("/_")
}

// init main domain
if MainDomain == "" {
hostname := strings.Split(c.Hostname(), ".")
if len(hostname) > 2 {
hostname = hostname[1:]
}
MainDomain = strings.Join(hostname, ".")
}

// check subdomain
if len(c.Subdomains()) > 0 {
if !db.CheckSubdomain(c.Subdomains()[0]) && !DevMode {
return c.Redirect(fmt.Sprintf("%s://%s", c.Protocol(), MainDomain), fiber.StatusMovedPermanently)
}
}

return c.Next()
})

routes.SiteRoutes(app)
routes.AdminRoutes(app)
routes.ApiRoutes(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()
}
}

return nil
}
73 changes: 0 additions & 73 deletions internal/app/app.go

This file was deleted.

84 changes: 0 additions & 84 deletions internal/app/controllers/auth.go

This file was deleted.

Loading

0 comments on commit b8193f2

Please sign in to comment.