Skip to content

Commit

Permalink
🦄 refactor: small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shurco committed Jul 27, 2023
1 parent 8d12090 commit 7989b15
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 52 deletions.
8 changes: 0 additions & 8 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

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

"github.com/shurco/litecart/internal/queries"
Expand Down Expand Up @@ -78,13 +77,6 @@ func NewApp() error {
routes.ApiPrivateRoutes(apiRoute)
routes.ApiPublicRoutes(apiRoute)

app.Use(redirect.New(redirect.Config{
Rules: map[string]string{
"/_": "/_/products",
},
StatusCode: 301,
}))

routes.NotFoundRoute(app)

ln, err := net.Listen("tcp", fmt.Sprintf(":%d", 8080))
Expand Down
42 changes: 21 additions & 21 deletions internal/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,44 @@ type Base struct {
ProductQueries
}

func InitDB(dbPath string, migrations embed.FS) (*sql.DB, error) {
func InitDB(dbPath string, migrations embed.FS) (db *sql.DB, err error) {
if !fsutil.IsFile(dbPath) {
// create db
if _, err := fsutil.OpenFile(dbPath, fsutil.FsCWFlags, 0666); err != nil {
return nil, err
if _, err = fsutil.OpenFile(dbPath, fsutil.FsCWFlags, 0666); err != nil {
return
}

// first migrate db
if err := Migrate(dbPath, migrations); err != nil {
return nil, err
if err = Migrate(dbPath, migrations); err != nil {
return
}
}

// connect to database
dsn := fmt.Sprintf("%s?_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)&_pragma=journal_size_limit(200000000)&_pragma=synchronous(NORMAL)&_pragma=foreign_keys(ON)", dbPath)
sqlite, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, err
}
return sqlite, nil
db, err = sql.Open("sqlite", dsn)
return
}

func Migrate(dbPath string, migrations embed.FS) error {
func Migrate(dbPath string, migrations embed.FS) (err error) {
goose.SetBaseFS(migrations)
db, err := goose.OpenDBWithDriver("sqlite", dbPath)
var db *sql.DB
db, err = goose.OpenDBWithDriver("sqlite", dbPath)
if err != nil {
return err
return
}
defer db.Close()

if err := goose.Up(db, "migrations"); err != nil {
return err
}
return nil
err = goose.Up(db, "migrations")
return
}

func InitQueries(embed embed.FS) error {
func InitQueries(embed embed.FS) (err error) {
// init database
sqlite, err := InitDB("./lc_base/data.db", embed)
var sqlite *sql.DB
sqlite, err = InitDB("./lc_base/data.db", embed)
if err != nil {
return err
return
}

db = &Base{
Expand All @@ -69,9 +66,12 @@ func InitQueries(embed embed.FS) error {
SettingQueries: SettingQueries{DB: sqlite},
ProductQueries: ProductQueries{DB: sqlite},
}
return nil
return
}

func DB() *Base {
if db == nil {
db = &Base{}
}
return db
}
18 changes: 10 additions & 8 deletions scripts/golang
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ source ${ROOT_PATH}/scripts/_helper

print_header "Install/Update Golang"

get_latest_go_version() {
wget -qO- "https://golang.org/dl/" | grep -v -E 'go[0-9\.]+(beta|rc)' | grep -E -o 'go[0-9\.]+' | grep -E -o '[0-9]\.[0-9]+(\.[0-9]+)?' | sort -V | uniq | tail -1
}

case "$OS" in
Darwin*)
brew install go
;;
Linux*)
support_arch

GO_RELEASE=$(wget -qO- "https://golang.org/dl/" | grep -v -E 'go[0-9\.]+(beta|rc)' | grep -E -o 'go[0-9\.]+' | grep -E -o '[0-9]\.[0-9]+(\.[0-9]+)?' | sort -V | uniq | tail -1)
GO_RELEASE=$(get_latest_go_version)
GO_PATH="$HOME/go"

if [ ! -d ${GO_PATH} ]; then
maybe_sudo mkdir $GO_PATH
echo -e "\nexport PATH=\$PATH:${GO_PATH}/bin\n" >>~/.bashrc
echo -e "\nexport PATH=\$PATH:\$HOME/go/bin\n" >>~/.bashrc
source ~/.bashrc
fi

maybe_sudo mkdir -p $GO_PATH
echo -e "\nexport PATH=\$PATH:${GO_PATH}/bin\nexport PATH=\$PATH:\$HOME/go/bin\n" >>~/.bashrc
source ~/.bashrc

curl --silent https://dl.google.com/go/go${GO_RELEASE}.linux-amd64.tar.gz | sudo tar -vxz --strip-components 1 -C ${GO_PATH} >/dev/null 2>&1
;;
*)
Expand Down
12 changes: 6 additions & 6 deletions scripts/sqlite
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
ROOT_PATH="$(git rev-parse --show-toplevel)"
source ${ROOT_PATH}/scripts/_helper

LC_BASE_DIR="${ROOT_PATH}/cmd/lc_base"
DB_FILE="${LC_BASE_DIR}/data.db"

if ! [ -d "${ROOT_PATH}/cmd/lc_base" ]; then
mkdir "${ROOT_PATH}/cmd/lc_base"
mkdir -p "${LC_BASE_DIR}"

if ! [ -f "${ROOT_PATH}/cmd/lc_base/data.db" ]; then
sqlite3 "${ROOT_PATH}/cmd/lc_base/data.db" "PRAGMA auto_vacuum;"
fi
if ! [ -f "${DB_FILE}" ]; then
sqlite3 "${DB_FILE}" "PRAGMA auto_vacuum;"
fi

sqlite3 "${ROOT_PATH}/cmd/lc_base/data.db" "VACUUM;"
sqlite3 "${DB_FILE}" "VACUUM;"
4 changes: 1 addition & 3 deletions scripts/tools
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ Darwin)
brew install goose golangci-lint yq sqlite3
;;
Linux)
if ! [ -d ${ROOT_PATH}/.vscode/tmp ]; then
mkdir ${ROOT_PATH}/.vscode/tmp
fi
mkdir -p ${ROOT_PATH}/.vscode/tmp

print_header "Install/Update sqlite3"
sudo apt-get install sqlite3 -y >/dev/null 2>&1
Expand Down
11 changes: 6 additions & 5 deletions web/views/admin/products.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ <h1 class="text-2xl font-bold text-gray-900 sm:text-3xl">Products</h1>
</div>

<drawer :is-open="isDrawerOpen" max-width="700px" @close="closeDrawer" :title="productName">

<div class="flow-root">
<dl class="-my-3 divide-y divide-gray-100 text-sm">
<div class="grid grid-cols-1 gap-1 py-3 sm:grid-cols-3 sm:gap-4">
Expand Down Expand Up @@ -122,7 +123,7 @@ <h1 class="text-2xl font-bold text-gray-900 sm:text-3xl">Products</h1>

methods: {
async getProduct(id) {
const response = await fetch('/api/products/' + id, {
const response = await fetch(`/api/products/${id}`, {
credentials: "include",
method: 'GET',
headers: {
Expand All @@ -132,22 +133,22 @@ <h1 class="text-2xl font-bold text-gray-900 sm:text-3xl">Products</h1>
});
this.resp = await response.json();
if (this.resp.success) {
this.productInfo = this.resp.result
this.productPrice = this.costFormat(this.resp.result.price.amount) + " " + this.resp.result.price.currency
this.productInfo = this.resp.result;
this.productPrice = `${this.costFormat(this.resp.result.price.amount)} ${this.resp.result.price.currency}`;
}
},

openDrawer(id, name) {
this.productName = name;
this.isDrawerOpen = true;
this.getProduct(id)
this.getProduct(id);
},

closeDrawer() {
this.productName = null;
this.isDrawerOpen = false;
this.productInfo = {};
this.productPrice = null
this.productPrice = null;
},

productAdd() {
Expand Down
2 changes: 1 addition & 1 deletion web/views/admin/signin.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h1 class="text-2xl font-bold sm:text-3xl">👨‍🎨 Admin sign in</h1>
type: 'error',
});
} else {
window.location.href = "/_";
window.location.href = "/_/products";
}
},
},
Expand Down

0 comments on commit 7989b15

Please sign in to comment.