-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathhandler.go
More file actions
32 lines (28 loc) · 727 Bytes
/
Copy pathhandler.go
File metadata and controls
32 lines (28 loc) · 727 Bytes
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
package yaml2go
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
)
func HandleConvert(w http.ResponseWriter, r *http.Request) {
// Enable CORS
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
http.Error(w, fmt.Sprintf("Bad Request. Error: %s", err.Error()), http.StatusBadRequest)
return
}
// Create yaml2go object and invoke Convert()
y2g := New()
result, err := y2g.Convert("Yaml2Go", []byte(data))
if err != nil {
log.Println(err)
http.Error(w, fmt.Sprintf("Bad Request. Error: %s", err.Error()), http.StatusBadRequest)
return
}
io.WriteString(w, result)
}