-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
239 lines (193 loc) · 6.32 KB
/
server.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
"strings"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
_ "github.com/mattn/go-sqlite3"
"github.com/ory/client-go"
ory "github.com/ory/client-go"
)
type App struct {
db *sql.DB
ory *ory.APIClient
}
var app App
func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}
c := ory.NewConfiguration()
c.Servers = client.ServerConfigurations{
{URL: fmt.Sprint("http://localhost:4433")},
}
//c.Servers = client.ServerConfigurations{
// {URL: fmt.Sprintf("https://%s.projects.oryapis.com", os.Getenv("ORY_PROJECT_SLUG"))},
//}
app = App{
db: InitDB(),
ory: ory.NewAPIClient(c),
}
router := mux.NewRouter()
router.HandleFunc("/", getIndex).Methods("GET")
router.HandleFunc("/board", getBoard).Methods("GET")
router.HandleFunc("/compose", app.sessionMiddleware(app.getCompose())).Methods("GET")
router.HandleFunc("/compose", app.sessionMiddleware(app.postCompose())).Methods("POST")
router.HandleFunc("/generationStatus", getGenerationStatus).Methods("GET")
router.HandleFunc("/project/{id}", getProject).Methods("GET")
router.HandleFunc("/profile", app.sessionMiddleware(app.getProfile())).Methods("GET")
router.PathPrefix("/public/").HandlerFunc(serveStatic)
router.PathPrefix("/data/").HandlerFunc(serveStatic)
fmt.Println("starting on http://:8080")
log.Fatal(http.ListenAndServe(":8080", router))
}
func getIndex(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("public/templates/header.tmpl", "public/templates/footer.tmpl", "public/html/index.html"))
err := tmpl.ExecuteTemplate(w, "index.html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func getBoard(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("public/templates/header.tmpl", "public/templates/footer.tmpl", "public/html/board.html"))
err := tmpl.ExecuteTemplate(w, "board.html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (app *App) getCompose() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("public/templates/header.tmpl", "public/templates/footer.tmpl", "public/html/compose.html"))
err := tmpl.ExecuteTemplate(w, "compose.html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
var taskStatus = make(map[string]string)
func (app *App) postCompose() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := getSession(r.Context())
user_id := session.Identity.Id
problem := r.FormValue("problem")
target := r.FormValue("target")
features := r.FormValue("features")
success := r.FormValue("success")
unique_id, err := GenerateRandomString(32)
// TODO: check if unique_id is unique in database
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
go func() {
project, err := GenerateProjectPlan(unique_id, problem, target, features, success)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = SaveProject(app.db, user_id, unique_id, project)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
taskStatus[unique_id] = "completed"
}()
// Return a pending response with the task ID
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"status": "pending",
"task_id": unique_id,
})
}
}
func getGenerationStatus(w http.ResponseWriter, r *http.Request) {
taskID := r.URL.Query().Get("task_id")
status := taskStatus[taskID]
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"status": status,
})
}
func getProject(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
project, err := LoadProject(app.db, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl := template.Must(template.ParseFiles("public/templates/header.tmpl", "public/templates/footer.tmpl", "public/html/project.html"))
err = tmpl.ExecuteTemplate(w, "project.html", project)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
type ProfilePayload struct {
Session *ory.Session
Projects []Project
}
func (app *App) getProfile() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session := getSession(r.Context())
if session == nil {
http.Error(w, "session error", http.StatusInternalServerError)
return
}
user_id := session.Identity.Id
rows, err := app.db.Query("SELECT id, user_id, data FROM projects WHERE user_id = ?", user_id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
defer rows.Close()
var projects []Project
for rows.Next() {
var ID string
var UserID string
var PlanStr string
if err := rows.Scan(&ID, &UserID, &PlanStr); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
var Plan ProjectPlan
err := json.Unmarshal([]byte(PlanStr), &Plan)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
p := Project{
ID: ID,
UserID: UserID,
Plan: Plan,
}
projects = append(projects, p)
}
payload := ProfilePayload{
Session: session,
Projects: projects,
}
tmpl := template.Must(template.ParseFiles("public/templates/header.tmpl", "public/templates/footer.tmpl", "public/html/profile.html"))
err = tmpl.ExecuteTemplate(w, "profile.html", payload)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func serveStatic(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
filePath := filepath.Join(".", path)
if strings.HasPrefix(path, "/public/css") {
w.Header().Set("Content-Type", "text/css")
} else if strings.HasPrefix(path, "/public/js") {
w.Header().Set("Content-Type", "application/javascript")
} else if strings.HasPrefix(path, "/public/images") {
w.Header().Set("Content-Type", "image/jpeg")
} else if strings.HasPrefix(path, "/data/images") {
w.Header().Set("Content-Type", "image/png")
}
http.ServeFile(w, r, filePath)
}