99 "net/http"
1010 "os"
1111 "strconv"
12+
13+ "github.com/tkc/go-json-server/src/logger"
1214)
1315
1416const (
@@ -78,7 +80,6 @@ const (
7880 HeaderXCSRFToken = "X-CSRF-Token"
7981)
8082
81-
8283type Endpoint struct {
8384 Type string `json:"type"`
8485 Method string `json:"method"`
@@ -97,7 +98,6 @@ type API struct {
9798var api API
9899
99100func main () {
100-
101101 raw , err := ioutil .ReadFile ("./api.json" )
102102 if err != nil {
103103 fmt .Println (err .Error ())
@@ -114,39 +114,45 @@ func main() {
114114 if len (ep .Folder ) > 0 {
115115 http .Handle (ep .Path + "/" , http .StripPrefix (ep .Path + "/" , http .FileServer (http .Dir (ep .Folder ))))
116116 } else {
117- http .HandleFunc (ep .Path , Response )
117+ http .HandleFunc (ep .Path , response )
118118 }
119119 }
120120
121121 err = http .ListenAndServe (":" + strconv .Itoa (api .Port ), nil )
122+
122123 if err != nil {
123124 log .Fatal (" " , err )
124125 }
125126}
126127
127- func Response (w http.ResponseWriter , r * http.Request ) {
128+
129+ func response (w http.ResponseWriter , r * http.Request ) {
130+
131+ appLogger := logger .CreateLogger ()
132+
133+ r .ParseForm ()
134+ appLogger .AccessLog (r )
128135
129136 w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
130137 w .Header ().Set ("Access-Control-Allow-Credentials" , "true" )
131138 w .Header ().Set ("Access-Control-Allow-Headers" , "Origin, X-Requested-With, Content-Type, Accept, Authorization" )
132139 w .Header ().Set ("Access-Control-Allow-Methods" , "GET, POST, PUT, DELETE, OPTIONS" )
133140
134- r .ParseForm ()
135141 for _ , ep := range api .Endpoints {
136142 if r .URL .Path == ep .Path && r .Method == ep .Method {
137143 fmt .Println ("method:" , r .Method )
138144 fmt .Println ("path:" , r .URL .Path )
139145 w .Header ().Set (HeaderContentType , MIMETextPlainCharsetUTF8 )
140146 w .WriteHeader (ep .Status )
141- s := Path2Response (ep .JsonPath )
147+ s := path2Response (ep .JsonPath )
142148 b := []byte (s )
143149 w .Write (b )
144150 }
145151 continue
146152 }
147153}
148154
149- func Path2Response (path string ) string {
155+ func path2Response (path string ) string {
150156 file , err := os .Open (path )
151157 if err != nil {
152158 log .Print (err )
0 commit comments