-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (119 loc) · 3.02 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"text/template"
"github.com/google/jsonapi"
"github.com/gorilla/mux"
)
/**
* Flags for command line configuration.
**/
var (
port = flag.Uint64("port", 8080, "Port to listen on.")
host = flag.String("addr", "", "The server's host.")
)
// templates holds pre-parsed template files.
var templates = template.Must(template.ParseFiles("view.tmpl"))
// Dummy is an empty struct for JSON:API unmarshalling.
type Dummy struct{}
// JSONInput holds the JSON string to be formatted.
type JSONInput struct {
Data string
}
/**
* Main exposes 3 API endpoints on a host and port.
**/
func main() {
flag.Parse()
rtr := mux.NewRouter()
rtr.HandleFunc("/", viewHandler)
api := rtr.PathPrefix("/api/v1").Subrouter()
api.HandleFunc("/std", stdHandler).Methods(http.MethodPost)
api.HandleFunc("/spec", specHandler).Methods(http.MethodPost)
fmt.Printf("Listening on %s:%d...\n", *host, *port)
log.Fatal(http.ListenAndServe(*host+":"+strconv.FormatUint(*port, 10), rateLimit(rtr)))
}
/**
* viewHandler presents a view to the client.
**/
func viewHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "view")
}
/**
* renderTemplate renders template that was parsed at program boot.
**/
func renderTemplate(w http.ResponseWriter, tmpl string) {
err := templates.ExecuteTemplate(w, tmpl+".tmpl", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
/**
* stdHandler verifies given JSON and formats it for client.
**/
func stdHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var t JSONInput
err := json.NewDecoder(r.Body).Decode(&t)
if err != nil {
errorResponse(w, err)
return
}
pretty_json, err := getPrettyJSONString([]byte(t.Data))
if err != nil {
errorResponse(w, err)
return
}
w.WriteHeader(http.StatusCreated)
w.Write([]byte(pretty_json))
}
/**
* specHandler verifies given JSON by the JSON:API spec
* and formats it for client.
**/
func specHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", jsonapi.MediaType)
b, err := ioutil.ReadAll(r.Body)
if err != nil {
errorResponse(w, err)
return
}
var d Dummy
err = jsonapi.UnmarshalPayload(bytes.NewReader(b), &d)
if err != nil {
errorResponse(w, err)
return
}
pretty_json, err := getPrettyJSONString(b)
if err != nil {
errorResponse(w, err)
return
}
w.WriteHeader(http.StatusCreated)
w.Write([]byte(pretty_json))
}
/**
* errorResponse creates an error message for the client to process.
**/
func errorResponse(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write([]byte(`{"error": "` + err.Error() + `"}`))
}
/**
* getPrettyJSONString indents given JSON byte array into a returned string.
**/
func getPrettyJSONString(src []byte) (string, error) {
var json_pretty bytes.Buffer
err := json.Indent(&json_pretty, src, "", "\t")
if err != nil {
return "", err
}
return string(json_pretty.Bytes()), nil
}