Skip to content

Commit 056676c

Browse files
committed
refactor: 小重构,update README.md
1 parent fea519f commit 056676c

File tree

2 files changed

+76
-81
lines changed

2 files changed

+76
-81
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
# 开发指南
3333

34-
0. go version > 1.16
34+
0. go version >= 1.13
3535
1. 准备数据库
3636

3737
```shell
@@ -41,4 +41,5 @@ docker run -d -p3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=1234qwer mysql:8
4141
2. 创建数据库,导入 [SQL](https://gitee.com/tomyang1898/APIJSON-Demo/blob/master/MySQL/sys.sql)
4242
3. 根据数据库参数修改 main.go 的 db.Init 参数
4343
4. 运行 `go run main.go`
44-
5. HTTP 数据测试可以看根目录的 [test.http](https://gitee.com/tiangao/apijson-go/blob/master/test.http)
44+
5. HTTP 数据测试可以看根目录的 [test.http](https://gitee.com/tiangao/apijson-go/blob/master/test.http)
45+
6. 或者使用 APIAuto 测试更方便 [APIAuto](https://github.com/TommyLemon/APIAuto)

handler/handler.go

+73-79
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,87 @@
11
package handler
22

33
import (
4-
"bytes"
5-
"encoding/json"
6-
"github.com/j2go/apijson/logger"
7-
"io/ioutil"
8-
"net/http"
9-
"strings"
4+
"bytes"
5+
"encoding/json"
6+
"github.com/j2go/apijson/logger"
7+
"io/ioutil"
8+
"net/http"
9+
"strings"
1010
)
1111

1212
func commonHandle(w http.ResponseWriter, r *http.Request, bodyHandler func(map[string]interface{}) map[string]interface{}) {
13-
if r.Method == http.MethodOptions {
14-
//logger.Infof("%v", r.Header)
15-
cors(w, r)
16-
w.WriteHeader(http.StatusOK)
17-
return
18-
}
19-
if data, err := ioutil.ReadAll(r.Body); err != nil {
20-
logger.Error("请求参数有问题: " + err.Error())
21-
w.WriteHeader(http.StatusBadRequest)
22-
return
23-
} else {
24-
logger.Infof("request: %s", string(data))
25-
var bodyMap map[string]interface{}
26-
if err = json.Unmarshal(data, &bodyMap); err != nil {
27-
logger.Error("请求体 JSON 格式有问题: " + err.Error())
28-
w.WriteHeader(http.StatusBadRequest)
29-
return
30-
}
31-
cors(w, r)
32-
dataMap := bodyHandler(bodyMap)
33-
var response []byte
34-
if response, err = json.Marshal(dataMap); err != nil {
35-
w.WriteHeader(http.StatusInternalServerError)
36-
} else {
37-
//logger.Debugf("返回数据 %s", string(respBody))
38-
if _, err = w.Write(response); err != nil {
39-
w.WriteHeader(http.StatusInternalServerError)
40-
} else {
41-
w.WriteHeader(http.StatusOK)
42-
}
43-
}
44-
}
13+
if r.Method == http.MethodOptions {
14+
//logger.Infof("%v", r.Header)
15+
cors(w, r)
16+
w.WriteHeader(http.StatusOK)
17+
return
18+
}
19+
if data, err := ioutil.ReadAll(r.Body); err != nil {
20+
logger.Error("请求参数有问题: " + err.Error())
21+
w.WriteHeader(http.StatusBadRequest)
22+
return
23+
} else {
24+
logger.Infof("request: %s", string(data))
25+
var bodyMap map[string]interface{}
26+
if err = json.Unmarshal(data, &bodyMap); err != nil {
27+
logger.Error("请求体 JSON 格式有问题: " + err.Error())
28+
w.WriteHeader(http.StatusBadRequest)
29+
return
30+
}
31+
cors(w, r)
32+
dataMap := bodyHandler(bodyMap)
33+
var response []byte
34+
if response, err = json.Marshal(dataMap); err != nil {
35+
w.WriteHeader(http.StatusInternalServerError)
36+
} else {
37+
//logger.Debugf("返回数据 %s", string(respBody))
38+
if _, err = w.Write(response); err != nil {
39+
w.WriteHeader(http.StatusInternalServerError)
40+
} else {
41+
w.WriteHeader(http.StatusOK)
42+
}
43+
}
44+
}
4545
}
4646

4747
func cors(w http.ResponseWriter, r *http.Request) {
48-
host := r.Header.Get("Origin")
49-
headers := r.Header
50-
hs := []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "Authorization", "X-CSRF-Token"}
51-
if len(headers) > 0 {
52-
for k, _ := range headers {
53-
if len(k) > 0 {
54-
find := false
55-
for _, h := range hs {
56-
if h == k {
57-
find = true
58-
break
59-
}
60-
}
61-
62-
if find {
63-
continue
64-
}
65-
66-
hs = append(hs, k)
67-
}
68-
}
69-
}
48+
allowHeaders := []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "Authorization", "X-CSRF-Token"}
49+
for k, _ := range r.Header {
50+
if len(k) > 0 {
51+
if notContains(allowHeaders, k) {
52+
allowHeaders = append(allowHeaders, k)
53+
}
54+
}
55+
}
56+
host := r.Header.Get("Origin")
57+
if len(host) > 0 {
58+
w.Header().Set("Access-Control-Allow-Origin", host)
59+
} else {
60+
w.Header().Set("Access-Control-Allow-Origin", "http://apijson.cn")
61+
}
62+
w.Header().Set("Access-Control-Allow-Credentials", "true")
63+
w.Header().Set("Access-Control-Allow-Headers", strings.Join(allowHeaders, ", ")) // 无效 "*")
64+
w.Header().Set("Access-Control-Request-Methods", "GET, POST, HEAD, PUT, DELETE, PATCH, OPTIONS")
65+
}
7066

71-
if len(host) > 0 {
72-
w.Header().Set("Access-Control-Allow-Origin", host)
73-
} else {
74-
w.Header().Set("Access-Control-Allow-Origin", "http://apijson.cn")
75-
}
76-
w.Header().Set("Access-Control-Allow-Credentials", "true")
77-
w.Header().Set("Access-Control-Allow-Headers", strings.Join(hs, ", ")) // 无效 "*")
78-
//w.Header().Set("Access-Control-Request-Method", "POST")
79-
w.Header().Set("Access-Control-Request-Methods", "GET, POST, HEAD, PUT, DELETE, PATCH, OPTIONS")
67+
func notContains(arr []string, k string) bool {
68+
for _, h := range arr {
69+
if h == k {
70+
return false
71+
}
72+
}
73+
return true
8074
}
8175

8276
func genPlaceholder(n int) string {
83-
if n == 1 {
84-
return "?"
85-
} else {
86-
buf := bytes.Buffer{}
87-
buf.WriteString("?")
88-
for i := 1; i < n; i++ {
89-
buf.WriteString(",?")
90-
}
91-
return buf.String()
92-
}
77+
if n == 1 {
78+
return "?"
79+
} else {
80+
buf := bytes.Buffer{}
81+
buf.WriteString("?")
82+
for i := 1; i < n; i++ {
83+
buf.WriteString(",?")
84+
}
85+
return buf.String()
86+
}
9387
}

0 commit comments

Comments
 (0)