-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
57 lines (48 loc) · 1.02 KB
/
web.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
// Copyright (c) 2021 Dave Marsh. See LICENSE.
package forms
import (
"fmt"
"io"
"net/http"
"github.com/golang/glog"
"github.com/gorilla/mux"
)
func GetRequestString(r *http.Request, key string) (sel string) {
vars := mux.Vars(r)
fmt.Sscan(vars[key], &sel)
return
}
func GetRequestUint(r *http.Request, key string) (sel uint) {
vars := mux.Vars(r)
fmt.Sscan(vars[key], &sel)
return
}
func GetRequestInt(r *http.Request, key string) (sel int) {
vars := mux.Vars(r)
fmt.Sscan(vars[key], &sel)
return
}
func WriteError(w http.ResponseWriter, err error) {
glog.Infoln(err)
http.Error(w, err.Error(), 400)
}
func WriteResponse(w http.ResponseWriter, b []byte) {
_, err := w.Write(b)
if err != nil {
glog.Errorln(err)
http.Error(w, err.Error(), 400)
}
}
func Request(u string) (data []byte, err error) {
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
return
}