Skip to content

Commit

Permalink
Switch session storage to db + new dev setup
Browse files Browse the repository at this point in the history
  • Loading branch information
thechubbypanda committed Sep 2, 2024
1 parent a708a9e commit ab5e955
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build]
args_bin = []
bin = ";export $(grep -v '^#' .env | xargs); ./tmp/syncify" # also exports everything in .env
cmd = "go build -o ./tmp/syncify ."
delay = 100
exclude_dir = ["node_modules"]
include_ext = ["go", "css", "env"]
pre_cmd = ["npm run build-dev"]
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea/
.env
node_modules/
data/
static/stylesheet.css
tmp/
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.idea/
.env
node_modules/
data/
static/stylesheet.css
tmp/
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ COPY . .

RUN npm i

RUN npx tailwindcss -i stylesheet.css -o static/stylesheet.css
RUN npm run build-prod

FROM docker.io/library/alpine:latest

Expand All @@ -26,4 +26,6 @@ COPY --from=build-go /build/out/syncify /bin/

COPY --from=build-tailwind /build/static/stylesheet.css static/

VOLUME [ "/data" ]

CMD ["syncify"]
5 changes: 4 additions & 1 deletion compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
services:
syncify:
# image: ghcr.io/thechubbypanda/syncify:main
image: ghcr.io/thechubbypanda/syncify:main
build: .
user: 1000:1000 # Change to your user
env_file:
- .env
volumes:
- ./data:/data
ports:
- 8000:8000
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package config
import "github.com/sirupsen/logrus"

type Config struct {
DataDir string `env:"DATA_DIR" envDefault:"/data"`
LogLevel logrus.Level `env:"LOG_LEVEL" envDefault:"info"`
ClientID string `env:"CLIENT_ID"`
ClientSecret string `env:"CLIENT_SECRET"`
Url string `env:"URL"`
Url string `env:"URL" envDefault:"http://localhost:8000"`
Plausible Plausible
}

Expand Down
3 changes: 3 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Folder where the sqlite database will be stored
DATA_DIR=/data

LOG_LEVEL=info

# Grab these from your own app at https://developer.spotify.com/dashboard
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ require (
golang.org/x/oauth2 v0.21.0
)

require (
github.com/ncruces/julianday v1.0.0 // indirect
github.com/tetratelabs/wazero v1.8.0 // indirect
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/alexedwards/scs/sqlite3store v0.0.0-20240316134038-7e11d57e8885
github.com/joho/godotenv v1.5.1 // indirect
golang.org/x/sys v0.18.0 // indirect
github.com/ncruces/go-sqlite3 v0.18.1
golang.org/x/sys v0.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/alexedwards/scs/sqlite3store v0.0.0-20240316134038-7e11d57e8885 h1:+DCxWg/ojncqS+TGAuRUoV7OfG/S4doh0pcpAwEcow0=
github.com/alexedwards/scs/sqlite3store v0.0.0-20240316134038-7e11d57e8885/go.mod h1:Iyk7S76cxGaiEX/mSYmTZzYehp4KfyylcLaV3OnToss=
github.com/alexedwards/scs/v2 v2.8.0 h1:h31yUYoycPuL0zt14c0gd+oqxfRwIj6SOjHdKRZxhEw=
github.com/alexedwards/scs/v2 v2.8.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -130,6 +132,11 @@ github.com/maragudk/gomponents v0.20.3 h1:7V3UV2CQ8n0RGChhRUPBoPcqBEvOyvJ6G2Ossv
github.com/maragudk/gomponents v0.20.3/go.mod h1:nHkNnZL6ODgMBeJhrZjkMHVvNdoYsfmpKB2/hjdQ0Hg=
github.com/maragudk/gomponents-htmx v0.5.0 h1:sWtiRa72YmymgxccjTNZW3h0akKsZvnhYke9RQiS9dk=
github.com/maragudk/gomponents-htmx v0.5.0/go.mod h1:XgI7WE6ECWlyeVQ9Ix3R6aoKS4HtCSYtuQ4iH27GVDE=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/ncruces/go-sqlite3 v0.18.1 h1:iN8IMZV5EMxpH88NUac9vId23eTKNFUhP7jgY0EBbNc=
github.com/ncruces/go-sqlite3 v0.18.1/go.mod h1:eEOyZnW1dGTJ+zDpMuzfYamEUBtdFz5zeYhqLBtHxvM=
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand All @@ -146,6 +153,8 @@ 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/tetratelabs/wazero v1.8.0 h1:iEKu0d4c2Pd+QSRieYbnQC9yiFlMS9D+Jr0LsRmcF4g=
github.com/tetratelabs/wazero v1.8.0/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -282,6 +291,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
40 changes: 35 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package main

import (
"database/sql"
"encoding/gob"
"net/http"
"path/filepath"
"strings"
"time"

"github.com/alexedwards/scs/sqlite3store"
"github.com/alexedwards/scs/v2"
"github.com/alexedwards/scs/v2/memstore"
"github.com/chi-middleware/logrus-logger"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/ilyakaznacheev/cleanenv"
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/sirupsen/logrus"
"github.com/thechubbypanda/syncify/config"
"github.com/thechubbypanda/syncify/model"
"github.com/thechubbypanda/syncify/views"
"github.com/zmb3/spotify/v2"
"github.com/zmb3/spotify/v2/auth"
"golang.org/x/oauth2"
"net/http"
"strings"
"time"
)

var cfg config.Config
Expand All @@ -37,7 +42,32 @@ func main() {

SetOauthConfig(cfg)

sm.Store = memstore.New()
dbFilename := filepath.Join(cfg.DataDir, "syncify.db")

db, err := sql.Open("sqlite3", "file:"+dbFilename)
if err != nil {
logrus.Fatalln(err)
}

logrus.Traceln("opened database file: " + dbFilename)

_, err = db.Exec("CREATE TABLE IF NOT EXISTS sessions (token TEXT PRIMARY KEY, data BLOB NOT NULL, expiry REAL NOT NULL);")
if err != nil {
logrus.Fatalln(err)
}

logrus.Traceln("sessions db created")

_, err = db.Exec("CREATE INDEX IF NOT EXISTS sessions_expiry_idx ON sessions(expiry);")
if err != nil {
logrus.Fatalln(err)
}

logrus.Traceln("created sessions index")

logrus.Infoln("initialized database")

sm.Store = sqlite3store.New(db)

sm.Cookie.Secure = strings.HasPrefix(cfg.Url, "https")
sm.Cookie.HttpOnly = true
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"tailwindcss": "^3.4.4"
},
"scripts": {
"build": "npx tailwindcss -i stylesheet.css -o static/stylesheet.css",
"watch": "npx tailwindcss -w -i stylesheet.css -o static/stylesheet.css"
"watch": "npx tailwindcss -i stylesheet.css -o static/stylesheet.css -w",
"build-dev": "npx tailwindcss -i stylesheet.css -o static/stylesheet.css",
"build-prod": "npx tailwindcss -i stylesheet.css -o static/stylesheet.css -m"
}
}
File renamed without changes

0 comments on commit ab5e955

Please sign in to comment.