This repository was archived by the owner on Dec 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebhandler.go
80 lines (73 loc) · 2.64 KB
/
webhandler.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
package toscaviewer
// This is a basic example
// Thanks http://thenewstack.io/make-a-restful-json-api-go/ for the tutorial
import (
"encoding/json"
"fmt"
"log"
"mime/multipart"
"net/http"
)
// UploadHandler courtesy of http://noypi-linux.blogspot.fr/2014/07/golang-web-server-basic-operatons-using.html
func (toscaGraph *ToscaGraph) UploadHandler(res http.ResponseWriter, req *http.Request) {
// For the redirect at the end
//url, _ := mux.CurrentRoute(req).Subrouter().Get("/index.html").URL()
log.Println("UploadHandler called")
var (
status int
err error
)
defer func() {
if nil != err {
http.Error(res, err.Error(), status)
}
}()
// parse request
const _24K = (1 << 20) * 24
if err = req.ParseMultipartForm(_24K); nil != err {
status = http.StatusInternalServerError
return
}
for _, fheaders := range req.MultipartForm.File {
for _, hdr := range fheaders {
// open uploaded
var infile multipart.File
if infile, err = hdr.Open(); nil != err {
status = http.StatusInternalServerError
return
}
err = toscaGraph.ToscaDefinition.Parse(infile)
//res.Write([]byte(toscaGraph.ToscaDefinition.Bytes()))
if err != nil {
log.Println(err)
}
toscaGraph.Initialize()
//http.Redirect(res, req, url.String(), http.StatusFound)
//res.Write([]byte("uploaded file:" + hdr.Filename + ";length:" + strconv.Itoa(int(written))))
}
}
http.Redirect(res, req, "/", http.StatusFound)
}
// GetState returns a json file of the current topology
func (toscaGraph *ToscaGraph) GetState(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
d, err := json.Marshal(toscaGraph.ToscaDefinition.TopologyTemplate.NodeTemplates)
if err == nil {
fmt.Fprintf(w, string(d))
}
}
// ViewToscaYaml is a http handler that output the yaml file
func (toscaGraph *ToscaGraph) ViewToscaYaml(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text; charset=UTF-8")
fmt.Fprintf(w, string(toscaGraph.Graph["ToscaYaml"]))
}
// ViewToscaDefinition is a http handler that output the SVG representation of the current tosca structure
func (toscaGraph *ToscaGraph) ViewToscaDefinition(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml; charset=UTF-8")
fmt.Fprintf(w, string(toscaGraph.Graph["ToscaDefinition"]))
}
// ViewToscaWorkfow is a http handler that output the SVG representation of the current tosca execution workflow
func (toscaGraph *ToscaGraph) ViewToscaWorkflow(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml; charset=UTF-8")
fmt.Fprintf(w, string(toscaGraph.Graph["ToscaWorkflow"]))
}