Repository navigation
Expand file tree
/
Copy pathhttp.go
More file actions
158 lines (146 loc) · 3.24 KB
/
Copy pathhttp.go
File metadata and controls
158 lines (146 loc) · 3.24 KB
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
package dev
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"regexp"
)
type Ctx struct {
W http.ResponseWriter
R *http.Request
Auth Claims
}
func (c Ctx) Send(w interface{}) {
switch v := w.(type) {
// Marsheld json
case Map:
jsn, err := json.Marshal(w)
if err != nil {
fmt.Printf("Error: %s", err.Error())
return
}
io.WriteString(c.W, string(jsn))
case []byte:
c.W.Write(w.([]byte))
case string:
io.WriteString(c.W, string(w.(string)))
default:
_ = v
print(v.(string))
}
}
func (c Ctx) PathEnd() string {
return path.Base(c.R.URL.Path)
}
func (c Ctx) Param(q string) string {
return c.R.URL.Query().Get(q)
}
func (c Ctx) Sends(w interface{}) {
io.WriteString(c.W, string(w.(string)))
}
func (c Ctx) SendErr(w string) {
io.WriteString(c.W, `{"err":"`+w+`"}`)
}
type Fld struct {
Err bool
Null bool
Val string
Name string
}
func (c Ctx) Field(name string, chk string) Fld {
v := c.R.FormValue(name)
var fld = Fld{Name: name, Err: false, Val: v}
// if len(v) == 0 {
// fld.Null = true
// }
if chk == `name` && len(v) < 3 {
fld.Err = true
return fld
}
if chk == `email` {
match, err := regexp.MatchString(`^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$`, v)
if match && err != nil {
fld.Err = true
return fld
}
}
if chk == `password` && len(v) < 3 {
fld.Err = true
return fld
}
return fld
}
func Get(path string, cbk func(c Ctx)) {
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
if r.Method != http.MethodGet {
io.WriteString(w, `err`)
return
}
cbk(Ctx{W: w, R: r})
})
}
// func Gets(path string, cbk func(c Ctx)) {
// http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
// if r.Method != http.MethodGet {
// io.WriteString(w, `err`)
// return
// }
// cbk(Ctx{W: w, R: r})
// })
// }
func Post(path string, cbk func(c Ctx)) {
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
io.WriteString(w, `err`)
return
}
cbk(Ctx{W: w, R: r})
})
}
func Gets(path string, cbk func(c Ctx)) {
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
if r.Method != http.MethodGet {
io.WriteString(w, `err`)
return
}
tkn, err := Signed(r)
if err {
io.WriteString(w, `Sign err`)
return
}
cbk(Ctx{W: w, R: r, Auth: tkn})
})
}
func Posts(path string, cbk func(c Ctx)) {
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
if r.Method != http.MethodPost {
io.WriteString(w, `err`)
return
}
tkn, err := Signed(r)
if err {
io.WriteString(w, `Sign err`)
return
}
cbk(Ctx{W: w, R: r, Auth: tkn})
})
}
func Cdn(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0700)
}
http.Handle(`/`+path+`/`, http.StripPrefix(`/`+path+`/`, http.FileServer(http.Dir(path))))
}
func Serve(path int) {
fmt.Printf("Serving on port %d\n", path)
http.ListenAndServe(fmt.Sprintf(":%d", path), nil)
}