-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
190 lines (157 loc) · 4.77 KB
/
template.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
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strings"
"text/template"
)
type Template struct {
ID int
Name string
File string
}
func (t *Template) Process(device int, job Job) ([]byte, error) {
type TemplateData struct {
Input, Output string
Seed, DeviceID int
}
data := TemplateData{Seed: int(rand.Int31())}
data.DeviceID = device
data.Output = fmt.Sprintf("sim.%d.dcd", data.Seed)
for _, file := range job.Model.Files {
parts := strings.Split(strings.ToLower(file), ".")
if parts[len(parts)-1] == "tpr" {
data.Input = strings.ToLower(fmt.Sprintf("gromacstprfile ../../../model/%s/%s", job.Model.Name, file))
break
}
if parts[len(parts)-1] == "pdb" {
data.Input = strings.ToLower(fmt.Sprintf("../../../model/%s/%s", job.Model.Name, file))
break
}
}
temp, err := template.New(strings.Split(job.Template.File, "/")[1]).ParseFiles(job.Template.File)
if err != nil {
return nil, err
}
var templateData bytes.Buffer
if err := temp.Execute(&templateData, data); err != nil {
return nil, err
}
return templateData.Bytes(), nil
}
func (t *Template) IsProtoMol() bool {
parts := strings.Split(strings.ToLower(t.File), ".")
return parts[len(parts)-1] == "conf"
}
var Templates []Template
func FindTemplate(id int, templates []Template) *Template {
for i := 0; i < len(templates); i++ {
if templates[i].ID == id {
return &templates[i]
}
}
return nil
}
func LoadTemplates(db *sql.DB) ([]Template, error) {
rows, err := DB.Query("SELECT * FROM template")
if err != nil {
return nil, err
}
var templates []Template
for rows.Next() {
var id int
var name, file string
if err := rows.Scan(&id, &name, &file); err != nil {
return nil, err
}
templates = append(templates, Template{ID: id, Name: name, File: file})
}
return templates, nil
}
func templateHandler(w http.ResponseWriter, r *http.Request) {
type JSONResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}
t, err := template.New("template.html").ParseFiles("template.html")
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
if err := t.Execute(w, Templates); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
}
func templateAddHandler(w http.ResponseWriter, r *http.Request) {
type JSONResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Template *Template `json:"template"`
}
if err := r.ParseMultipartForm(1 * 1024 * 1024); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: "Unable to parse form", Template: nil})
return
}
name := r.FormValue("name")
if name == "" {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: "Missing Name", Template: nil})
return
}
template := Template{Name: name, File: fmt.Sprintf("data/%s.template", name)}
for _, fileHeaders := range r.MultipartForm.File {
for _, fileHeader := range fileHeaders {
file, _ := fileHeader.Open()
buf, _ := ioutil.ReadAll(file)
parts := strings.Split(strings.ToLower(fileHeader.Filename), ".")
extension := parts[len(parts)-1]
ioutil.WriteFile(fmt.Sprintf("data/%s.template.%s", name, extension), buf, os.ModePerm)
template.File = fmt.Sprintf("data/%s.template.%s", name, extension)
}
}
res, err := DB.Exec("insert into template(name, file) values (?,?)", template.Name, template.File)
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
id, err := res.LastInsertId()
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
template.ID = int(id)
Templates = append(Templates, template)
json.NewEncoder(w).Encode(JSONResponse{Success: true, Message: "", Template: &template})
}
func templateRemoveHandler(w http.ResponseWriter, r *http.Request) {
type JSONResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}
w.Header().Set("Content-Type", "application/json")
id := r.FormValue("id")
if id == "" {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: "Missing Data"})
return
}
var file string
if err := DB.QueryRow("SELECT file FROM template WHERE id = ?", id).Scan(&file); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
if err := os.Remove(file); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
if _, err := DB.Exec("DELETE FROM template WHERE id = ?", id); err != nil {
json.NewEncoder(w).Encode(JSONResponse{Success: false, Message: err.Error()})
return
}
json.NewEncoder(w).Encode(JSONResponse{Success: true})
}