-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
451 lines (382 loc) · 11.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package main
import (
"database/sql"
"embed"
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
//go:embed templates/*
var templatesFS embed.FS
type Task struct {
ID string
Name string
XP int
Completed bool
SessionID string
}
type Unlockable struct {
ID string
Level int
Description string
}
type templateData struct {
SessionID string
Tasks []Task
Unlockables []Unlockable
TotalXP int
CurrentLevel int
Progress float64
Unlocked map[int]bool
}
// Configuration struct
type Config struct {
DatabaseURL string
ServerPort string
MaxDBConns int
}
// Database tables structure
const (
createTablesSQL = `
CREATE TABLE IF NOT EXISTS sessions (
id VARCHAR(255) PRIMARY KEY,
total_xp INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS app_state (
id INTEGER PRIMARY KEY,
total_xp INTEGER NOT NULL DEFAULT 0,
CHECK (id = 1)
);
CREATE TABLE IF NOT EXISTS tasks (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
xp INTEGER NOT NULL CHECK (xp > 0),
completed BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
session_id VARCHAR(255) REFERENCES sessions(id)
);
CREATE TABLE IF NOT EXISTS unlockables (
id SERIAL PRIMARY KEY,
level INTEGER NOT NULL CHECK (level >= 0),
description TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
session_id VARCHAR(255) REFERENCES sessions(id),
UNIQUE (level, description)
);
CREATE TABLE IF NOT EXISTS unlocked_levels (
level INTEGER PRIMARY KEY,
unlocked BOOLEAN NOT NULL DEFAULT true,
unlocked_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
session_id VARCHAR(255) REFERENCES sessions(id)
);
CREATE INDEX IF NOT EXISTS idx_tasks_completed ON tasks(completed);
CREATE INDEX IF NOT EXISTS idx_unlockables_level ON unlockables(level);`
)
type AppState struct {
sync.RWMutex
db *sql.DB
SessionID string
TotalXP int
Unlocked map[int]bool
stmts map[string]*sql.Stmt
}
var templates *template.Template
func init() {
godotenv.Load()
templates = template.Must(template.New("").Funcs(template.FuncMap{
"add": func(a, b int) int { return a + b },
"sub": func(a, b int) int { return a - b },
"mod": func(a, b int) int { return a % b },
}).ParseFS(templatesFS, "templates/*.html"))
}
// Initialize database connection
func initDB(config Config) (*sql.DB, error) {
db, err := sql.Open("postgres", config.DatabaseURL)
if err != nil {
return nil, fmt.Errorf("error opening database: %w", err)
}
// Set connection pool parameters
db.SetMaxOpenConns(config.MaxDBConns)
db.SetMaxIdleConns(config.MaxDBConns / 2)
db.SetConnMaxLifetime(time.Hour)
// Initialize tables
if _, err := db.Exec(createTablesSQL); err != nil {
return nil, fmt.Errorf("error creating tables: %w", err)
}
return db, nil
}
// Replace loadState with initAppState
func initAppState(db *sql.DB, sessionID string) (*AppState, error) {
state := &AppState{
db: db,
SessionID: sessionID,
Unlocked: make(map[int]bool),
stmts: make(map[string]*sql.Stmt),
}
// First ensure the session exists
_, err := db.Exec("INSERT INTO sessions (id) VALUES ($1) ON CONFLICT (id) DO NOTHING", sessionID)
if err != nil {
return nil, fmt.Errorf("error ensuring session exists: %w", err)
}
// Prepare statements
statements := map[string]string{
"getTasks": "SELECT id, name, xp, completed FROM tasks WHERE session_id = $1",
"getUnlockables": "SELECT id, level, description FROM unlockables WHERE session_id = $1 ORDER BY level",
"addTask": "INSERT INTO tasks (name, xp, completed, session_id) VALUES ($1, $2, false, $3)",
"completeTask": "UPDATE tasks SET completed = true WHERE id = $1 AND session_id = $2 AND completed = false RETURNING xp",
"updateXP": "UPDATE sessions SET total_xp = $1 WHERE id = $2",
"addUnlockable": "INSERT INTO unlockables (level, description, session_id) VALUES ($1, $2, $3)",
}
for name, query := range statements {
stmt, err := db.Prepare(query)
if err != nil {
return nil, fmt.Errorf("error preparing statement %s: %w", name, err)
}
state.stmts[name] = stmt
}
// Initialize state
err = db.QueryRow("SELECT total_xp FROM sessions WHERE id = $1", sessionID).Scan(&state.TotalXP)
if err != nil {
return nil, fmt.Errorf("error loading session state: %w", err)
}
// Load unlocked levels
if err := state.loadUnlockedLevels(); err != nil {
return nil, err
}
return state, nil
}
func (a *AppState) loadUnlockedLevels() error {
rows, err := a.db.Query("SELECT level FROM unlocked_levels WHERE session_id = $1 AND unlocked = true", a.SessionID)
if err != nil {
return fmt.Errorf("error loading unlocked levels: %w", err)
}
defer rows.Close()
for rows.Next() {
var level int
if err := rows.Scan(&level); err != nil {
return fmt.Errorf("error scanning unlocked level: %w", err)
}
a.Unlocked[level] = true
}
return nil
}
// Use prepared statements and caching for frequently accessed methods
func (a *AppState) getTasks() ([]Task, error) {
rows, err := a.stmts["getTasks"].Query(a.SessionID)
if err != nil {
return nil, fmt.Errorf("error getting tasks: %w", err)
}
defer rows.Close()
var tasks []Task
for rows.Next() {
var t Task
if err := rows.Scan(&t.ID, &t.Name, &t.XP, &t.Completed); err != nil {
return nil, fmt.Errorf("error scanning task: %w", err)
}
tasks = append(tasks, t)
}
return tasks, nil
}
func (a *AppState) getUnlockables() ([]Unlockable, error) {
rows, err := a.stmts["getUnlockables"].Query(a.SessionID)
if err != nil {
return nil, err
}
defer rows.Close()
var unlockables []Unlockable
for rows.Next() {
var u Unlockable
if err := rows.Scan(&u.ID, &u.Level, &u.Description); err != nil {
return nil, err
}
unlockables = append(unlockables, u)
}
return unlockables, nil
}
// Add this function to generate random session IDs
func generateSessionID() string {
const letters = "abcdefghijklmnopqrstuvwxyz"
b := strings.Builder{}
for i := 0; i < 4; i++ {
b.WriteByte(letters[rand.Intn(len(letters))])
}
return b.String()
}
func main() {
db, err := initDB(Config{
DatabaseURL: os.Getenv("DATABASE_URL"),
ServerPort: ":8080",
MaxDBConns: 10,
})
if err != nil {
log.Fatal(err)
}
defer db.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
sessionID := strings.TrimPrefix(r.URL.Path, "/")
if sessionID == "" {
newSessionID := generateSessionID()
http.Redirect(w, r, "/"+newSessionID, http.StatusFound)
return
}
if err := initSession(db, sessionID); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
state, err := initAppState(db, sessionID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := state.loadSessionXP(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
state.renderAppState(w, false)
})
http.HandleFunc("/add-task/", func(w http.ResponseWriter, r *http.Request) {
sessionID := strings.TrimPrefix(r.URL.Path, "/add-task/")
state, err := initAppState(db, sessionID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
state.handleAddTask(w, r)
})
http.HandleFunc("/add-xp/", func(w http.ResponseWriter, r *http.Request) {
sessionID := strings.TrimPrefix(r.URL.Path, "/add-xp/")
state, err := initAppState(db, sessionID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
state.handleAddXP(w, r)
})
http.HandleFunc("/add-unlockable/", func(w http.ResponseWriter, r *http.Request) {
sessionID := strings.TrimPrefix(r.URL.Path, "/add-unlockable/")
state, err := initAppState(db, sessionID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
state.handleAddUnlockable(w, r)
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Server starting on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func (a *AppState) GetProgressPercentage() int {
currentLevelXP := a.TotalXP % 1000
return int(float64(currentLevelXP) / 1000 * 100)
}
func initSession(db *sql.DB, sessionID string) error {
_, err := db.Exec("INSERT INTO sessions (id) VALUES ($1) ON CONFLICT (id) DO NOTHING", sessionID)
return err
}
func (s *AppState) loadSessionXP() error {
return s.db.QueryRow("SELECT total_xp FROM sessions WHERE id = $1", s.SessionID).Scan(&s.TotalXP)
}
func (s *AppState) handleAddTask(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
name := r.FormValue("name")
xp := r.FormValue("xp")
_, err := s.stmts["addTask"].Exec(name, xp, s.SessionID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s.renderAppState(w, true)
}
func (s *AppState) handleAddXP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
taskID := r.FormValue("task")
tx, err := s.db.Begin()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer tx.Rollback()
var xp int
err = tx.QueryRow("UPDATE tasks SET completed = true WHERE id = $1 AND session_id = $2 AND completed = false RETURNING xp",
taskID, s.SessionID).Scan(&xp)
if err == sql.ErrNoRows {
http.Error(w, "Task already completed or not found", http.StatusBadRequest)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s.TotalXP += xp
_, err = tx.Exec("UPDATE sessions SET total_xp = $1 WHERE id = $2", s.TotalXP, s.SessionID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = tx.Commit(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s.renderAppState(w, true)
}
func (s *AppState) handleAddUnlockable(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
level := r.FormValue("level")
description := r.FormValue("description")
_, err := s.stmts["addUnlockable"].Exec(level, description, s.SessionID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s.renderAppState(w, true)
}
func (s *AppState) renderAppState(w http.ResponseWriter, onlyApp bool) {
tasks, err := s.getTasks()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
unlockables, err := s.getUnlockables()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := templateData{
SessionID: s.SessionID,
Tasks: tasks,
Unlockables: unlockables,
TotalXP: s.TotalXP,
CurrentLevel: s.TotalXP / 1000,
Progress: float64(s.TotalXP%1000) / 10.0,
Unlocked: s.Unlocked,
}
file := "index.html"
if onlyApp {
file = "app.html"
}
if err := templates.ExecuteTemplate(w, file, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}