Skip to content

Commit bb4f4cb

Browse files
committed
Impl
1 parent 8d6e719 commit bb4f4cb

File tree

6 files changed

+64
-13
lines changed

6 files changed

+64
-13
lines changed

api.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"port": 3000,
3+
"endpoints": [
4+
{
5+
"method": "GET",
6+
"status": 200,
7+
"path": "/users",
8+
"jsonPath": "./users.json"
9+
},
10+
{
11+
"method": "GET",
12+
"status": 200,
13+
"path": "/user/1",
14+
"jsonPath": "./user.json"
15+
},
16+
{
17+
"path": "/file",
18+
"folder": "./static"
19+
}
20+
]
21+
}

example/api.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@
1818
"folder": "./static"
1919
}
2020
]
21-
}
22-
21+
}

go-json-server.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"encoding/csv"
56
"encoding/json"
67
"fmt"
78
"io/ioutil"
@@ -78,7 +79,6 @@ const (
7879
HeaderXCSRFToken = "X-CSRF-Token"
7980
)
8081

81-
8282
type Endpoint struct {
8383
Type string `json:"type"`
8484
Method string `json:"method"`
@@ -97,7 +97,6 @@ type API struct {
9797
var api API
9898

9999
func main() {
100-
101100
raw, err := ioutil.ReadFile("./api.json")
102101
if err != nil {
103102
fmt.Println(err.Error())
@@ -114,39 +113,42 @@ func main() {
114113
if len(ep.Folder) > 0 {
115114
http.Handle(ep.Path+"/", http.StripPrefix(ep.Path+"/", http.FileServer(http.Dir(ep.Folder))))
116115
} else {
117-
http.HandleFunc(ep.Path, Response)
116+
http.HandleFunc(ep.Path, response)
118117
}
119118
}
120119

121120
err = http.ListenAndServe(":"+strconv.Itoa(api.Port), nil)
121+
122122
if err != nil {
123123
log.Fatal(" ", err)
124124
}
125125
}
126126

127-
func Response(w http.ResponseWriter, r *http.Request) {
128-
129-
w.Header().Set( "Access-Control-Allow-Origin", "*" )
130-
w.Header().Set( "Access-Control-Allow-Credentials", "true" )
131-
w.Header().Set( "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization" )
132-
w.Header().Set( "Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS" )
127+
func response(w http.ResponseWriter, r *http.Request) {
133128

134129
r.ParseForm()
130+
accessLog(r)
131+
132+
w.Header().Set("Access-Control-Allow-Origin", "*")
133+
w.Header().Set("Access-Control-Allow-Credentials", "true")
134+
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
135+
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
136+
135137
for _, ep := range api.Endpoints {
136138
if r.URL.Path == ep.Path && r.Method == ep.Method {
137139
fmt.Println("method:", r.Method)
138140
fmt.Println("path:", r.URL.Path)
139141
w.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
140142
w.WriteHeader(ep.Status)
141-
s := Path2Response(ep.JsonPath)
143+
s := path2Response(ep.JsonPath)
142144
b := []byte(s)
143145
w.Write(b)
144146
}
145147
continue
146148
}
147149
}
148150

149-
func Path2Response(path string) string {
151+
func path2Response(path string) string {
150152
file, err := os.Open(path)
151153
if err != nil {
152154
log.Print(err)
@@ -157,3 +159,15 @@ func Path2Response(path string) string {
157159
buf.ReadFrom(file)
158160
return buf.String()
159161
}
162+
163+
func accessLog(r *http.Request) {
164+
file, err := os.OpenFile("log.csv", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
165+
if err != nil {
166+
log.Fatal(err)
167+
}
168+
defer file.Close()
169+
s := []string{r.Method, r.Host, r.Proto, r.RequestURI, r.RemoteAddr}
170+
writer := csv.NewWriter(file)
171+
writer.Write(s)
172+
writer.Flush()
173+
}

log.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
GET,localhost:3000,HTTP/1.1,/users,[::1]:56235,/users
2+
GET,localhost:3000,HTTP/1.1,/users,[::1]:56259,/users
3+
GET,localhost:3000,HTTP/1.1,/users,[::1]:56302
4+
GET,localhost:3000,HTTP/1.1,/users,[::1]:56302
5+
GET,localhost:3000,HTTP/1.1,/users,[::1]:56302
6+
GET,localhost:3000,HTTP/1.1,/user/1,[::1]:56302

user.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": 1,
3+
"name": "name",
4+
"address": "address"
5+
}

users.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "name"
5+
}
6+
]

0 commit comments

Comments
 (0)