-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (67 loc) · 1.82 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
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/url"
"github.com/davecgh/go-spew/spew"
)
type Dialog []struct {
Episodename string `json:"episodename"`
Act string `json:"act"`
Scenenumber string `json:"scenenumber"`
Texttype string `json:"texttype"`
Who string `json:"who"`
Text string `json:"text"`
Speech string `json:"speech"`
Released string `json:"released"`
Episode string `json:"episode"`
Imdbrating string `json:"imdbrating"`
Imdbid string `json:"imdbid"`
Season string `json:"season"`
}
var tpl = template.Must(template.ParseFiles("index.html"))
func indexHandler(w http.ResponseWriter, r *http.Request) {
tpl.Execute(w, nil)
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse(r.URL.String())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return
}
params := u.Query()
searchKey := params.Get("q")
dialog := &Dialog{}
endpoint := fmt.Sprintf("https://tngapi-awicwils6q-ew.a.run.app/?q=%s", url.QueryEscape(searchKey))
resp, err := http.Get(endpoint)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.NewDecoder(resp.Body).Decode(&dialog)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
spew.Dump(dialog)
err = tpl.Execute(w, dialog)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
func main() {
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("assets"))
mux.Handle("/assets/", http.StripPrefix("/assets/", fs))
mux.HandleFunc("/search", searchHandler)
mux.HandleFunc("/", indexHandler)
http.ListenAndServe(":3000", mux)
}