Skip to content

Commit

Permalink
move sql to app for future docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima committed Jan 6, 2024
1 parent ccecd4f commit efa0a25
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
2 changes: 2 additions & 0 deletions backend/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ func NewRouter(db *sqlitex.Pool, ytr *youtube.YouTubeRequester) *Router {
ytr: ytr,
}

// api
router.PathPrefix("/api/videos/random").Methods("GET").HandlerFunc(router.GetRandom).HeadersRegexp("visitor", "[0-9]{10,20}")
router.PathPrefix("/api/videos/{video_id}/{reaction:(?:cool|trash)}").Methods("POST").HandlerFunc(router.WriteReaction).HeadersRegexp("visitor", "[0-9]{10,20}")
router.PathPrefix("/api/videos/{video_id}").Methods("GET").HandlerFunc(router.GetVideo)

// front
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("frontend/static"))))
router.PathPrefix("/").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
Expand Down
37 changes: 37 additions & 0 deletions backend/api/tables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package api

const CreateTablesIfNotExists = `
CREATE TABLE IF NOT EXISTS videos
(
id STRING PRIMARY KEY,
uploaded INTEGER,
title STRING,
views INTEGER,
vertical INTEGER,
category INTEGER
);
CREATE TABLE IF NOT EXISTS visitors (
id STRING PRIMARY KEY,
last_seen INTEGER
);
CREATE TABLE IF NOT EXISTS videos_visitors
(
visitor_id STRING,
video_id STRING,
PRIMARY KEY (visitor_id, video_id),
FOREIGN KEY (video_id) REFERENCES videos (id) ON DELETE CASCADE,
FOREIGN KEY (visitor_id) REFERENCES visitors (id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS reactions
(
cool INTEGER,
visitor_id STRING,
video_id STRING,
PRIMARY KEY (visitor_id, video_id),
FOREIGN KEY (video_id) REFERENCES videos (id) ON DELETE CASCADE,
FOREIGN KEY (visitor_id) REFERENCES visitors (id) ON DELETE CASCADE
);
`
33 changes: 0 additions & 33 deletions db.sql

This file was deleted.

7 changes: 2 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ func main() {
if err != nil {
log.Fatal("cannot open db", err)
}
dbScheme, err := os.ReadFile("db.sql")
if err != nil {
log.Fatal("cannot open db.sql: ", err.Error())
}

conn := db.Get(context.Background())
if err := sqlitex.ExecuteScript(conn, string(dbScheme), nil); err != nil {
if err := sqlitex.ExecuteScript(conn, api.CreateTablesIfNotExists, nil); err != nil {
log.Fatal("cannot create db: ", err)
}
db.Put(conn)
Expand Down

0 comments on commit efa0a25

Please sign in to comment.