-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
41 lines (34 loc) · 901 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/rs/cors"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s({\"data\":\"Test Resopnse\"})", r.URL.Query().Get("callback"))
}
// Form comment
type Form struct {
Name, Email string
}
func submitHandler(w http.ResponseWriter, r *http.Request) {
dec := json.NewDecoder(r.Body)
var f Form
err := dec.Decode(&f)
if err == nil {
fmt.Printf("%s: %s\n", f.Name, f.Email)
} else {
fmt.Println("Something went wrong")
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
mux.HandleFunc("/submit/", submitHandler)
// cors.Default() setup the middleware with default options being
// all origins accepted with simple methods (GET, POST). See
// documentation below for more options.
handler := cors.Default().Handler(mux)
http.ListenAndServe("localhost:8081", handler)
}