aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-21 18:12:49 +0900
committernsfisis <nsfisis@gmail.com>2024-07-21 18:12:49 +0900
commit2512da7ca57dc4c52900417a50daf4f6f2f74054 (patch)
tree6a4921a9fcd2fe774171795425ba04889c9c8a49 /backend
parente3373eb48b8cd5eef61a59d8ff590db184612294 (diff)
downloadphperkaigi-2025-albatross-2512da7ca57dc4c52900417a50daf4f6f2f74054.tar.gz
phperkaigi-2025-albatross-2512da7ca57dc4c52900417a50daf4f6f2f74054.tar.zst
phperkaigi-2025-albatross-2512da7ca57dc4c52900417a50daf4f6f2f74054.zip
add sqldef and sqlc
Diffstat (limited to 'backend')
-rw-r--r--backend/main.go41
-rw-r--r--backend/query.sql0
-rw-r--r--backend/schema.sql23
-rw-r--r--backend/sqlc.yaml10
4 files changed, 40 insertions, 34 deletions
diff --git a/backend/main.go b/backend/main.go
index f0121ef..ae39b46 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -56,7 +56,6 @@ func loadEnv() (*Config, error) {
const (
gameTypeGolf = "golf"
- gameTypeRace = "race"
)
const (
@@ -68,24 +67,12 @@ const (
type Game struct {
GameID int `db:"game_id"`
- // "golf" or "race"
+ // "golf"
Type string `db:"type"`
CreatedAt string `db:"created_at"`
State string `db:"state"`
}
-func initDB() error {
- _, err := db.Exec(`
- CREATE TABLE IF NOT EXISTS games (
- game_id SERIAL PRIMARY KEY,
- type VARCHAR(255) NOT NULL,
- created_at TIMESTAMP NOT NULL DEFAULT NOW(),
- state VARCHAR(255) NOT NULL
- );
- `)
- return err
-}
-
var gameHubs = map[int]*GameHub{}
func startGame(game *Game) {
@@ -127,8 +114,7 @@ func handleGolfPost(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, fmt.Sprintf("/golf/%d/%s/", waitingGame.GameID, yourTeam), http.StatusSeeOther)
}
-func handleRacePost(w http.ResponseWriter, r *http.Request) {
- http.Redirect(w, r, "/race/1/a/", http.StatusSeeOther)
+func handleApiLogin(w http.ResponseWriter, r *http.Request) {
}
func main() {
@@ -151,22 +137,13 @@ func main() {
}
defer db.Close()
- err = initDB()
- if err != nil {
- log.Fatalf("Error initializing db %v", err)
- }
-
server := http.NewServeMux()
server.HandleFunc("GET /assets/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./public"+r.URL.Path)
})
- server.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
- http.ServeFile(w, r, "./public/index.html")
- })
-
- server.HandleFunc("GET /golf/{$}", func(w http.ResponseWriter, r *http.Request) {
+ server.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./public/index.html")
})
@@ -174,10 +151,6 @@ func main() {
handleGolfPost(w, r)
})
- server.HandleFunc("GET /golf/{gameId}/watch/{$}", func(w http.ResponseWriter, r *http.Request) {
- http.ServeFile(w, r, "./public/index.html")
- })
-
server.HandleFunc("GET /sock/golf/{gameId}/watch/{$}", func(w http.ResponseWriter, r *http.Request) {
gameId := r.PathValue("gameId")
gameIdInt, err := strconv.Atoi(gameId)
@@ -199,10 +172,6 @@ func main() {
serveWsWatcher(hub, w, r)
})
- server.HandleFunc("GET /golf/{gameId}/{team}/{$}", func(w http.ResponseWriter, r *http.Request) {
- http.ServeFile(w, r, "./public/index.html")
- })
-
server.HandleFunc("GET /sock/golf/{gameId}/{team}/{$}", func(w http.ResponseWriter, r *http.Request) {
gameId := r.PathValue("gameId")
gameIdInt, err := strconv.Atoi(gameId)
@@ -225,6 +194,10 @@ func main() {
serveWs(hub, w, r, team)
})
+ server.HandleFunc("POST /api/login/{$}", func(w http.ResponseWriter, r *http.Request) {
+ handleApiLogin(w, r)
+ })
+
defer func() {
for _, hub := range gameHubs {
hub.Close()
diff --git a/backend/query.sql b/backend/query.sql
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/backend/query.sql
diff --git a/backend/schema.sql b/backend/schema.sql
new file mode 100644
index 0000000..6a2db2d
--- /dev/null
+++ b/backend/schema.sql
@@ -0,0 +1,23 @@
+CREATE TABLE users (
+ user_id SERIAL PRIMARY KEY,
+ username VARCHAR(64) NOT NULL UNIQUE,
+ display_username VARCHAR(64) NOT NULL,
+ icon_url VARCHAR(255),
+ is_admin BOOLEAN NOT NULL,
+ created_at TIMESTAMP NOT NULL DEFAULT NOW()
+);
+
+CREATE TABLE user_auths (
+ user_auth_id SERIAL PRIMARY KEY,
+ user_id INT NOT NULL,
+ auth_type VARCHAR(16) NOT NULL,
+ password_hash VARCHAR(256),
+ CONSTRAINT fk_user_id FOREIGN KEY(user_id) REFERENCES users(user_id)
+);
+
+CREATE TABLE games (
+ game_id SERIAL PRIMARY KEY,
+ type VARCHAR(255) NOT NULL,
+ created_at TIMESTAMP NOT NULL DEFAULT NOW(),
+ state VARCHAR(255) NOT NULL
+);
diff --git a/backend/sqlc.yaml b/backend/sqlc.yaml
new file mode 100644
index 0000000..4027d9b
--- /dev/null
+++ b/backend/sqlc.yaml
@@ -0,0 +1,10 @@
+version: "2"
+sql:
+ - engine: "postgresql"
+ queries: "query.sql"
+ schema: "schema.sql"
+ gen:
+ go:
+ package: "sqlc"
+ out: "sqlc"
+ sql_package: "pgx/v5"