This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.go
119 lines (105 loc) · 2.72 KB
/
home.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
package main
import (
"context"
"html/template"
"log"
"net/http"
)
type homeHandlers struct{}
var pageTmpl = `<!DOCTYPE html>
<html>
<head>
<title>{{.title}}</title>
<style>
html{
box-sizing: 'border-box';
}
.container{
width: 100%;
min-height: 100vh;
}
.container > div{width: 100%;float:left;}
.col-md-4{ width: 33.33%;}
.col-md-offset{margin-left: 33.33%;}
</style>
</head>
<body>
<div class="container">{{template "content" .}}</div>
</body>
</html>
`
var homePage = `{{define "content"}}<div class="row">
<div class="col-md-4 col-md-offset-4">
<h2>{{.message}}</h2>
<div class="panel">
<ul>
<li><a href="/about">About</a></li>
<li><a href="/hello" class="btn">Hello</a></li>
<li><input type="text" name="username"> <a href="/hello" class="btn" id="gobtn">Go</a></li>
</ul>
<p id="msg"></p>
</div>
</div>
</div>
<script>
var ip = document.querySelector("input[name='username']")
var btn = document.getElementById("gobtn")
var msg = document.getElementById("msg")
var baseurl = String(btn.href)
ip.addEventListener('input',function(e){
btn.href = baseurl+"/"+ip.value
})
document.querySelectorAll("a.btn").forEach(a=>{
a.addEventListener("click",function(e){
e.preventDefault()
console.log(e)
GetResult(e.target.href)
.then(res=>{return res.text()})
.then(txt=>{msg.innerHTML = e.target.href+" : "+txt})
return false
})
})
function GetResult(path){
return fetch(path)
}
</script>
{{end}}`
var aboutPage = `{{define "content"}}<div class="row">
<div class="col-md-4 col-md-offset-4">
<h2>{{.message}}</h2>
<a href="/">Home</a>
</div>
</div>
{{end}}`
func (h homeHandlers) homePage(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, int64(0), map[string]string{"title": "Mux Test Home", "message": "HOME"})
newr := r.WithContext(ctx)
h.render(homePage, w, newr)
}
func (h homeHandlers) aboutPage(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, int64(0), map[string]string{"title": "Mux Test Home", "message": "ABOUT"})
newr := r.WithContext(ctx)
h.render(aboutPage, w, newr)
}
func (h homeHandlers) render(tmpl string, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
data := ctx.Value(int64(0))
t, err := template.New("page").Parse(pageTmpl + tmpl)
if err != nil {
log.Println(err)
h.errPage500(w, r)
return
}
w.Header().Add("Content-type", "text/html")
t.ExecuteTemplate(w, "page", data)
}
func (h homeHandlers) errPage404(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Whoops! Sorry cant find that."))
}
func (h homeHandlers) errPage500(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Sorry had a bit of a problem."))
}