generated from jphacks/JP_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (138 loc) · 4.7 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
package main
import (
"database/sql"
"html/template"
"log"
"net/http"
"time"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
type DashboardData struct {
CurrentDate string
StudyData []map[string]interface{}
}
func init() {
var err error
db, err = sql.Open("sqlite3", "./user_registration.db")
if err != nil {
log.Fatal(err)
}
createUserTable := `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
username TEXT NOT NULL UNIQUE
);`
createStudyTable := `
CREATE TABLE IF NOT EXISTS study_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT,
detection_time REAL,
study_time REAL,
focus_score REAL
);`
_, err = db.Exec(createUserTable)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(createStudyTable)
if err != nil {
log.Fatal(err)
}
}
func main() {
http.HandleFunc("/", registrationPage)
http.HandleFunc("/register", registerUser)
http.HandleFunc("/login", loginPage)
http.HandleFunc("/authenticate", authenticateUser)
http.HandleFunc("/dashboard", dashboardPage)
http.HandleFunc("/logout", logout)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
log.Println("Server started on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
// ユーザー登録ページのハンドラー
func registrationPage(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/register.html"))
tmpl.Execute(w, nil)
}
// ユーザーをデータベースに登録するハンドラー
func registerUser(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
email := r.FormValue("email")
username := r.FormValue("username")
var exists bool
err := db.QueryRow("SELECT EXISTS(SELECT 1 FROM users WHERE email=?)", email).Scan(&exists)
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
if exists {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
_, err = db.Exec("INSERT INTO users (email, username) VALUES (?, ?)", email, username)
if err != nil {
http.Error(w, "Failed to register user", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/login", http.StatusSeeOther)
} else {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
// ログインページのハンドラー
func loginPage(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/login.html"))
tmpl.Execute(w, nil)
}
// ログイン認証のハンドラー
func authenticateUser(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
email := r.FormValue("email")
username := r.FormValue("username")
var exists bool
err := db.QueryRow("SELECT EXISTS(SELECT 1 FROM users WHERE email=? AND username=?)", email, username).Scan(&exists)
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
if exists {
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
} else {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
} else {
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
}
// ダッシュボードページのハンドラー
func dashboardPage(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/dashboard.html"))
currentDate := time.Now().Format("06/01/02") // 今日の日付 (YY/MM/DD 形式)
rows, err := db.Query("SELECT date, detection_time, study_time, focus_score FROM study_data ORDER BY date DESC")
if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
defer rows.Close()
var data []map[string]interface{}
for rows.Next() {
var date string
var detectionTime, studyTime, focusScore float64
rows.Scan(&date, &detectionTime, &studyTime, &focusScore)
record := map[string]interface{}{
"Date": date,
"DetectionTime": detectionTime,
"StudyTime": studyTime,
"FocusScore": focusScore,
}
data = append(data, record)
}
tmpl.Execute(w, DashboardData{CurrentDate: currentDate, StudyData: data})
}
// ログアウトのハンドラー
func logout(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/login", http.StatusSeeOther)
}