-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
208 lines (182 loc) · 4.92 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Package web is a super minimalistic web/http server library
package web
import (
"bytes"
"encoding/json"
"html/template"
"io"
"io/fs"
"log"
"net/http"
"os"
"strings"
)
// Headers is a map of string to string where the key is the key for the header
// And the value is the value for the header
type Headers map[string]string
// Response is a generic response object for our handlers
type Response struct {
// StatusCode
Status int
// Content Type to writer
ContentType string
// Content to be written to the response writer
Content io.Reader
// Headers to be written to the response writer
Headers Headers
}
// Write writes a response to an http.ResponseWriter
func (response *Response) Write(rw http.ResponseWriter) {
if response != nil {
if response.ContentType != "" {
rw.Header().Set("Content-Type", response.ContentType)
}
for k, v := range response.Headers {
rw.Header().Set(k, v)
}
rw.WriteHeader(response.Status)
_, err := io.Copy(rw, response.Content)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
}
} else {
rw.WriteHeader(http.StatusOK)
}
}
// Action represents a simplified http action
// that implements http.Handler
type Action func(r *http.Request) *Response
// Hyperlink represents a hyperlink
type Hyperlink struct {
Rel string `json:"rel"`
Href string `json:"href"`
}
// Action's http.Handler implementation
func (a Action) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
response := a(r)
response.Write(rw)
}
// Error returns an error response
func Error(status int, err error, headers Headers) *Response {
return &Response{
Status: status,
Content: bytes.NewBufferString(err.Error()),
Headers: headers,
}
}
type errorResponse struct {
Error string `json:"error"`
}
// ErrorJSON returns an error in json format
func ErrorJSON(status int, err error, headers Headers) *Response {
errResp := errorResponse{
Error: err.Error(),
}
b, err := json.Marshal(errResp)
if err != nil {
return Error(http.StatusInternalServerError, err, headers)
}
return &Response{
Status: status,
ContentType: "application/json",
Content: bytes.NewBuffer(b),
Headers: headers,
}
}
// Data returns a data response
func Data(status int, content []byte, headers Headers) *Response {
return &Response{
Status: status,
Content: bytes.NewBuffer(content),
Headers: headers,
}
}
// DataJSON returns a data response in json format
func DataJSON(status int, v interface{}, headers Headers) *Response {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return ErrorJSON(http.StatusInternalServerError, err, headers)
}
return &Response{
Status: status,
ContentType: "application/json",
Content: bytes.NewBuffer(b),
Headers: headers,
}
}
// Empty returns an empty http response
func Empty(status int) *Response {
return Data(status, []byte(""), nil)
}
// Sends an HTTP 302 redirect
func Redirect(location string) *Response {
return &Response{
Status: http.StatusFound,
Content: bytes.NewBuffer(nil),
Headers: map[string]string{
"Location": location,
},
}
}
// HTML renders an html template to a web response
func HTML(status int, t *template.Template, template string, data interface{}, headers Headers) *Response {
//render template to buffer
var buf bytes.Buffer
if err := t.ExecuteTemplate(&buf, template, data); err != nil {
log.Println(err)
return Empty(http.StatusInternalServerError)
}
return &Response{
Status: status,
ContentType: "text/html",
Content: &buf,
Headers: headers,
}
}
// TemplateParseFSRecursive recursively parses all templates in the FS with the given extension.
// File paths are used as template names to support duplicate file names.
// Use nonRootTemplateNames to exclude root directory from template names
// (e.g. index.html instead of templates/index.html)
func TemplateParseFSRecursive(
templates fs.FS,
ext string,
nonRootTemplateNames bool,
funcMap template.FuncMap) (*template.Template, error) {
root := template.New("")
err := fs.WalkDir(templates, "templates", func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() && strings.HasSuffix(path, ext) {
if err != nil {
return err
}
b, err := fs.ReadFile(templates, path)
if err != nil {
return err
}
name := ""
if nonRootTemplateNames {
//name the template based on the file path (excluding the root)
parts := strings.Split(path, string(os.PathSeparator))
name = strings.Join(parts[1:], string(os.PathSeparator))
}
t := root.New(name).Funcs(funcMap)
_, err = t.Parse(string(b))
if err != nil {
return err
}
}
return nil
})
return root, err
}
// PathLast returns the last segment in the path
// and the number of path segments
func PathLast(r *http.Request) (string, int) {
//remove trailing /
path := r.URL.Path
if path[len(path)-1] == '/' {
path = path[0 : len(path)-1]
}
parts := strings.Split(path, "/")
l := len(parts) - 1
return parts[l], l
}