@@ -2,6 +2,7 @@ package main
22
33import (
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-
8282type Endpoint struct {
8383 Type string `json:"type"`
8484 Method string `json:"method"`
@@ -97,7 +97,6 @@ type API struct {
9797var api API
9898
9999func 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+ }
0 commit comments