-
Notifications
You must be signed in to change notification settings - Fork 1
/
playground.go
395 lines (360 loc) · 11.5 KB
/
playground.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package stroo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
_ "github.com/badu/stroo/statik"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"go/format"
"go/parser"
"go/token"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"text/template"
)
const (
playgroundVersion = "0.0.3"
storageFolder = "/server"
assetsFolder = "/assets/"
indexHTML = "/index.html"
codeTextAreaHTML = "/codetextarea.html"
playgroundHTML = "/playground.html"
favico = "/favicon.ico"
exampleGo = "/example-source"
exampleTemplate = "/example-template"
jQuery = "/jquery-3.4.1.js"
semanticJs = "/semantic-2.4.2.js"
codeMirrorJs = "/codemirror-5.51.0.js"
riotJs = "/riotcompiler-4.8.7.js"
matchBracketsJs = "/matchbrackets.js"
goJs = "/go.js"
semanticCss = "/semantic-2.4.2.css"
codeMirrorTheme = "/darcula-5.51.0.css"
codeMirrorCss = "/codemirror-5.51.0.css"
font1 = "/themes/default/assets/fonts/icons.woff2"
font2 = "/themes/default/assets/fonts/icons.woff"
font3 = "/themes/default/assets/fonts/icons.ttf"
packageName = "playground"
)
func provideFile(w http.ResponseWriter, statikFS http.FileSystem, path string) {
r, err := statikFS.Open(path)
if err != nil {
log.Printf("error finding file : %q", path)
w.WriteHeader(http.StatusNotFound)
return
}
defer r.Close()
contents, err := ioutil.ReadAll(r)
if err != nil {
log.Printf("error reading file : %q", path)
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(contents)
}
func indexTemplate(statikFS http.FileSystem) *template.Template {
r, err := statikFS.Open(indexHTML)
if err != nil {
log.Fatalf("error finding file : %q", indexHTML)
}
defer r.Close()
contents, err := ioutil.ReadAll(r)
if err != nil {
log.Fatalf("error reading file : %q", indexHTML)
}
index, err := template.New("index.html").Parse(string(contents))
if err != nil {
log.Fatalf("error parsing template : %q", indexHTML)
}
return index
}
func indexTemplateLocal(workingDir string) *template.Template {
index, err := template.ParseFiles(workingDir + storageFolder + indexHTML)
if err != nil {
log.Fatalf("Error parsing template : %v\n working dir is = %q", err, workingDir)
}
return index
}
func filesHandler(workingDir string, statikFS http.FileSystem, testMode bool) http.HandlerFunc {
type pageinfo struct {
Version string
}
info := pageinfo{
Version: playgroundVersion,
}
var idxTemplate *template.Template
// prepare index.html
if !testMode {
idxTemplate = indexTemplate(statikFS)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case indexHTML, "/":
if testMode {
idxTemplate = indexTemplateLocal(workingDir) // reload template
}
if err := idxTemplate.ExecuteTemplate(w, "index", info); err != nil {
log.Printf("error producing index template : %v", err)
return
}
case codeTextAreaHTML, playgroundHTML:
if testMode {
http.ServeFile(w, r, workingDir+storageFolder+"/"+r.URL.Path)
} else {
provideFile(w, statikFS, r.URL.Path)
}
case semanticCss, codeMirrorCss, codeMirrorTheme:
w.Header().Set("Content-Type", "text/css")
provideFile(w, statikFS, r.URL.Path)
case jQuery, semanticJs, codeMirrorJs, riotJs, goJs, matchBracketsJs:
provideFile(w, statikFS, r.URL.Path)
case font1, font2, font3:
provideFile(w, statikFS, assetsFolder+strings.Replace(r.URL.Path, "/themes/default/assets/fonts/", "", -1))
case exampleGo:
if testMode {
http.ServeFile(w, r, workingDir+storageFolder+exampleGo+".go")
} else {
provideFile(w, statikFS, exampleGo+".go")
}
case exampleTemplate:
if testMode {
http.ServeFile(w, r, workingDir+storageFolder+exampleTemplate+".tpl")
} else {
provideFile(w, statikFS, exampleTemplate+".tpl")
}
case favico:
// just ignore it
default:
w.WriteHeader(http.StatusNotFound)
log.Printf("Requested UNKNOWN URL : %q", r.URL.Path)
}
})
}
type ErrorType int
const (
Json ErrorType = 1
TemplaParse ErrorType = 2
BadTempProject ErrorType = 3
PackaLoad ErrorType = 4
OnePackage ErrorType = 5
Packalyse ErrorType = 6
NoTypes ErrorType = 7
TemplExe ErrorType = 8
BadFormat ErrorType = 9
)
type MalformedRequest struct {
Status int `json:"status"`
ErrorMessage string `json:"errorMessage"`
PartialSource string `json:"partialSource"`
Type ErrorType `json:"type"`
}
func (m MalformedRequest) Error() string {
return m.ErrorMessage
}
var InvalidGoSource = MalformedRequest{Type: BadTempProject, Status: http.StatusBadRequest}
var InvalidTypes = MalformedRequest{Type: NoTypes, Status: http.StatusBadRequest}
var InvalidPackage = MalformedRequest{Type: PackaLoad, Status: http.StatusBadRequest}
var InvalidAnalysis = MalformedRequest{Type: Packalyse, Status: http.StatusBadRequest}
var InvalidFormat = MalformedRequest{Type: BadFormat, Status: http.StatusBadRequest}
var InvalidTemplate2 = MalformedRequest{Type: TemplaParse, Status: http.StatusBadRequest}
var InvalidTemplate = MalformedRequest{Type: TemplExe, Status: http.StatusBadRequest}
func respond(w http.ResponseWriter, data interface{}, optionalMessage ...string) {
if err, ok := data.(error); ok {
var (
typedError MalformedRequest
syntaxError *json.SyntaxError
unmarshalTypeError *json.UnmarshalTypeError
)
switch {
case errors.Is(err, io.EOF):
typedError = MalformedRequest{
ErrorMessage: "payload empty",
Status: http.StatusBadRequest,
Type: Json,
}
case errors.As(err, &syntaxError):
case errors.As(err, &unmarshalTypeError):
typedError = MalformedRequest{
ErrorMessage: err.Error(),
Status: http.StatusBadRequest,
Type: Json,
}
case errors.Is(err, InvalidGoSource):
typedError = InvalidGoSource
if len(optionalMessage) == 1 {
typedError.ErrorMessage = optionalMessage[0]
}
case errors.Is(err, InvalidTypes):
typedError = InvalidTypes
if len(optionalMessage) == 1 {
typedError.ErrorMessage = optionalMessage[0]
}
case errors.Is(err, InvalidAnalysis):
typedError = InvalidAnalysis
if len(optionalMessage) == 1 {
typedError.ErrorMessage = optionalMessage[0]
}
case errors.Is(err, InvalidFormat):
typedError = InvalidFormat
switch len(optionalMessage) {
case 1:
typedError.ErrorMessage = optionalMessage[0]
case 2:
typedError.ErrorMessage = optionalMessage[0]
typedError.PartialSource = optionalMessage[1]
}
case errors.Is(err, InvalidTemplate):
typedError = InvalidTemplate
if len(optionalMessage) == 1 {
typedError.ErrorMessage = optionalMessage[0]
}
case errors.Is(err, InvalidTemplate2):
typedError = InvalidTemplate2
if len(optionalMessage) == 1 {
typedError.ErrorMessage = optionalMessage[0]
}
case errors.Is(err, InvalidPackage):
typedError = InvalidPackage
if len(optionalMessage) == 1 {
typedError.ErrorMessage = optionalMessage[0]
}
default:
log.Printf("Unhandled error ? %T %#v", data, data)
typedError = MalformedRequest{
ErrorMessage: err.Error(),
Status: http.StatusBadRequest,
Type: Json,
}
}
data = typedError
w.WriteHeader(typedError.Status)
} else {
w.WriteHeader(http.StatusOK) // status is ok
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
log.Printf("Error encoding data : %v", err)
}
}
type previewRequest struct {
Template string
Source string
SourceChanged bool
}
type previewResponse struct {
Result string `json:"result"`
}
func strooHandler(command *Command) http.HandlerFunc {
var cachedResult *Code
return func(w http.ResponseWriter, r *http.Request) {
var request previewRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
respond(w, err)
return
}
// template is more likely to change : we're processing it first
tmpTemplate, err := template.New(packageName).Funcs(DefaultFuncMap()).Parse(request.Template)
if err != nil {
respond(w, InvalidTemplate2, err.Error())
return
}
// we're using cached result, so we don't stress the disk for nothing
if request.SourceChanged || cachedResult == nil {
// first we check the correctness of the source, so we don't write down for nothing
fileSet := token.NewFileSet()
_, err := parser.ParseFile(fileSet, packageName, request.Source, parser.DeclarationErrors|parser.AllErrors)
if err != nil {
respond(w, InvalidGoSource, err.Error())
return
}
// prepare a temp project
tempProj, err := CreateTempProj([]TemporaryPackage{{Name: packageName, Files: map[string]interface{}{"file.go": request.Source}}})
if err != nil {
respond(w, InvalidGoSource, err.Error())
return
}
// setup cleanup, so temporary files and folders gets deleted
defer tempProj.Cleanup()
tempProj.Config.Mode = packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax | packages.NeedImports
// load package using the old way
thePackages, err := packages.Load(tempProj.Config, fmt.Sprintf("file=%s", tempProj.File(packageName, "file.go")))
if err != nil {
respond(w, InvalidPackage, err.Error())
return
}
if len(thePackages) != 1 {
respond(w, MalformedRequest{ErrorMessage: "expecting exactly one package", Type: OnePackage})
return
}
// create a temporary command to analyse the loaded package
codeBuilder := DefaultAnalyzer()
tempCommand := NewCommand(codeBuilder)
tempCommand.TestMode = command.TestMode
tempCommand.DebugPrint = command.DebugPrint
if err := tempCommand.Analyse(codeBuilder, thePackages[0]); err != nil {
respond(w, InvalidAnalysis, err.Error())
return
}
// convention : by default, the upper most type struct is provided to the code builder
firstTypeName := ""
if len(tempCommand.Result.Types) >= 1 {
firstTypeName = tempCommand.Result.Types[0].Kind
tempCommand.SelectedType = firstTypeName
tempCommand.CodeConfig.SelectedType = firstTypeName
}
// create code
cachedResult, err = New(tempCommand.Result, tempCommand.CodeConfig, tmpTemplate)
if err != nil {
respond(w, MalformedRequest{ErrorMessage: err.Error()})
return
}
}
cachedResult.ResetKeeper() // reset kept data (so we can refill it)
// finally, we're processing the template over the result
var buf bytes.Buffer
if err := tmpTemplate.Execute(&buf, cachedResult); err != nil {
respond(w, InvalidTemplate, err.Error())
return
}
optImports, err := imports.Process(packageName, buf.Bytes(), nil)
if err != nil {
respond(w, InvalidFormat, err.Error(), buf.String())
return
}
formatted, err := format.Source(optImports)
if err != nil {
respond(w, InvalidFormat, err.Error(), string(optImports))
return
}
response := previewResponse{Result: string(formatted)}
respond(w, response)
}
}
func StartPlayground(command *Command) {
log.Printf("Starting on http://0.0.0.0:8080\n")
wd, err := os.Getwd()
if err != nil {
log.Fatalf("could NOT obtain current workdir : %v", err)
}
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
router := mux.NewRouter()
router.NotFoundHandler = filesHandler(wd, statikFS, command.TestMode)
router.HandleFunc("/stroo-it", strooHandler(command)).Methods("POST")
if err := http.ListenAndServe("0.0.0.0:8080", router); err != nil {
log.Fatalf("error while serving : %v", err)
}
log.Printf("Server started.")
}