-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_cf_handler.go
166 lines (140 loc) · 4.35 KB
/
app_cf_handler.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
// ThingsConstruction, a code generator for WoT-based models
// Copyright (C) 2017,2018 @aschmidt75
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// This program is dual-licensed. For commercial licensing options, please
// contact the author(s).
//
//
package main
import (
"github.com/gorilla/mux"
"net/http"
)
type appGenParamsData struct {
AppPageData
NumGenerators int
AppGenTargets *AppGenTargets
VoteGenerators map[string]string
}
func appGenParamsNewPageData(id string) (*appGenParamsData, error) {
t, err := ReadGeneratorsConfig()
if err != nil {
Error.Printf("Unable to present generators. FIX CONFIG!\n")
return nil, err
}
var data = &appGenParamsData{
AppPageData: AppPageData{
PageData: PageData{
Title: "Choose Embedded Development Framework",
},
ThingId: id,
},
NumGenerators: 1,
AppGenTargets: t,
VoteGenerators: ServerConfig.VoteGenerators,
}
data.SetFeaturesFromConfig()
data.InApp = true
if !data.IsIdValid() {
Error.Printf("Invalid ID=%s\n", id)
return nil, &AppError{"Unable to locate WoT data for given ID"}
}
if err := data.Deserialize(); err != nil {
Error.Println(err)
return nil, &AppError{"Unable to locate WoT data for given ID"}
}
data.SetTocInfo()
return data, nil
}
// AppChooseFrameworkHandleGet lets user choose a generator module for a given Web Thing ID
func AppChooseFrameworkHandleGet(w http.ResponseWriter, req *http.Request) {
if ServerConfig.Features.App == false {
http.Redirect(w, req, "/", 302)
return
}
vars := mux.Vars(req)
id := vars["id"]
data, err := appGenParamsNewPageData(id)
data.UpdateFeaturesFromContext(req.Context())
if err != nil {
// send back to s page
http.Redirect(w, req, "/app", 302)
return
}
appGenParamsServePage(w, *data)
}
// AppChooseFrameworkHandlePost saves generator module choice
func AppChooseFrameworkHandlePost(w http.ResponseWriter, req *http.Request) {
if ServerConfig.Features.App == false {
http.Redirect(w, req, "/", 302)
return
}
err := req.ParseForm()
if err != nil {
Debug.Printf("Error parsing choose framework form: %s\n", err)
appCreateThingServePage(w, appEntryData{
AppPageData: AppPageData{
Message: "There was an error processing your data.",
}})
}
ctf := req.PostForm
// user selected a generator.
// check if id is valid
vars := mux.Vars(req)
id := vars["id"]
cfid := ctf.Get("cfid")
cfs := ctf.Get("cfs")
Debug.Printf("got id=%s, cfs=%s, cfid=%s\n", id, cfs, cfid)
if id != cfid {
AppErrorServePage(w, "An error occurred while processing form data. Please try again.", id)
return
}
data := &AppPageData{
ThingId: id,
}
data.SetFeaturesFromConfig()
data.UpdateFeaturesFromContext(req.Context())
if !data.IsIdValid() {
AppErrorServePage(w, "An error occurred while location session data. Please try again.", id)
return
}
if err := data.Deserialize(); err != nil {
Error.Println(err)
AppErrorServePage(w, "An error occurred while reading session data. Please try again.", id)
return
}
// write generator to meta data file
data.md = &GeneratorMetaData{
SelectedGeneratorId: cfs,
}
if err := data.Serialize(); err != nil {
Error.Println(err)
AppErrorServePage(w, "An error occurred while storing session data. Please try again.", id)
return
}
UseCaseCounter.Increment("module-selected", cfs)
// redirect to manage properties
http.Redirect(w, req, "/app/"+id+"/properties", 302)
}
func appGenParamsServePage(w http.ResponseWriter, data appGenParamsData) {
templates, err := NewBasicHtmlTemplateSet("app_cf.html.tpl", "app_cf_script.html.tpl")
if err != nil {
Error.Fatalf("Fatal error creating template set: %s\n", err)
}
if err = templates.ExecuteTemplate(w, "root", data); err != nil {
Error.Printf("Error executing template: %s\n", err)
}
}