forked from wickett/word-cloud-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (67 loc) · 1.83 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
package main
import (
"embed"
"encoding/json"
"html/template"
"io"
"io/fs"
"io/ioutil"
"log"
"net/http"
"github.com/wickett/word-cloud-generator/wordyapi"
)
// serves up our static content like html
//go:embed static/*
var staticFiles embed.FS
func getStaticFiles() http.FileSystem {
fsys, err := fs.Sub(staticFiles, "static")
if err != nil {
log.Fatal(err)
}
return http.FS(fsys)
}
// TextSubmission is a json title and string to submit
type TextSubmission struct {
Title string `json:"title"`
Text string `json:"text"`
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("templates/upload.tmpl")
t.Execute(w, nil)
}
// Test json with curl using this:
// curl -H "Content-Type: application/json" -d '{"text":"this is a really really really important thing this is"}' http://localhost:8888/newapi
func receiveJSONHandler(w http.ResponseWriter, r *http.Request) {
var textIn TextSubmission
// don't allow huge uploads
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
//fmt.Printf(string(body))
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, &textIn); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422) // unprocessable entity
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
t := wordyapi.TextToParse{Title: textIn.Title, Text: textIn.Text}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(wordyapi.ParseText(t))
}
func main() {
staticFs := http.FileServer(getStaticFiles())
mux := http.NewServeMux()
// routes
mux.HandleFunc("/api", receiveJSONHandler)
mux.Handle("/", staticFs)
// Bind to a port and pass our router in
err := http.ListenAndServe(":8888", mux)
if err != nil {
log.Fatal(err)
}
}