-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
152 lines (123 loc) · 3.71 KB
/
handlers.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
package main
import (
"github.com/go-yaml/yaml"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
_ "github.com/mattn/go-sqlite3"
"net/http"
"html/template"
"fmt"
"log"
"bytes"
"io"
)
func update(w http.ResponseWriter, r *http.Request){
switch r.Method {
case "GET":
templateFiles := []string {
"./templates/update.html",
"./templates/base.html",
}
ts, err := template.ParseFiles(templateFiles...)
if err != nil {
logError("Failed to parse template(s)"+fmt.Sprintf("%v",err))
httpISE(w)
return
}
err = ts.Execute(w,nil)
if err != nil {
logError("Failed to execute template(s)"+fmt.Sprintf("%v",err))
httpISE(w)
return
}
break
case "POST":
s := ModelSpec{}
err := r.ParseForm()
if err != nil {
logError("Failed to parse form")
return
}
file, handler, err := r.FormFile("data")
if err != nil {
logError(fmt.Sprintf("%v",err))
http.Error(w,"Error processing form",400)
return
}
defer file.Close()
buf := bytes.NewBuffer(nil)
n,err := io.Copy(buf,file)
if err != nil {
logError("Failed to read file")
http.Error(w,"Error processing data", 422)
return
} else {
fname := handler.Header["Content-Disposition"][0]
log.Printf("Read %v bytes from %s", n, fname)
}
yaml.Unmarshal(buf.Bytes(),&s)
if err != nil {
logError("Failed to parse yaml")
http.Error(w,"Error processing data", 422)
return
}
// Populate DB
db, err := gorm.Open("sqlite3", "db.sqlite")
if err != nil {
logError("Failed to open DB")
httpISE(w)
}
db.Exec("DROP TABLE variables;")
db.Exec("DROP TABLE themes;")
db.Exec("DROP TABLE sets;")
db.AutoMigrate(&Variable{},&Set{},&Theme{})
tx := db.Begin()
// Add variables, along with their variable sets
for set ,variables := range s.Colsets {
setObj := Set{Name: set, Variables: make([]*Variable,len(variables))}
for idx,v := range variables {
varObj := Variable{Name: v}
setObj.Variables[idx] = &varObj
// Do not duplicate variables
tx.FirstOrCreate(&varObj,Variable{Name:v})
}
tx.Create(&setObj)
}
// Create themes; collections of variable sets
for theme, sets := range s.Themes {
themeObj := Theme{Name: theme, Sets: make([]*Set,len(sets))}
for idx,set := range sets {
setObj := Set{}
tx.FirstOrCreate(&setObj,Set{Name: set})
themeObj.Sets[idx] = &setObj
}
tx.Create(&themeObj)
}
tx.Commit()
db.Close()
fmt.Fprintf(w,"Success!")
break
}
}
func show(w http.ResponseWriter, r *http.Request){
templateFiles := []string {
"./templates/list.html",
"./templates/base.html",
}
ts, err := template.ParseFiles(templateFiles...)
if err != nil {
logError("Failed to parse template(s)"+fmt.Sprintf("%v",err))
httpISE(w)
return
}
db, err := gorm.Open("sqlite3", "db.sqlite")
if err != nil {
logError("Failed to open DB")
httpISE(w)
}
defer db.Close()
variables := make([]*Variable,0)
db.Find(&variables)
fmt.Printf("%v",variables)
ts.Execute(w,variables)
}