File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ package gitlab_mock
2+
3+ import (
4+ "encoding/json"
5+ "fmt"
6+ "net/http"
7+ "path"
8+ "strings"
9+ )
10+
11+ type FileHandler struct {
12+ Root http.FileSystem
13+ }
14+
15+ func (f * FileHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
16+ upath := r .URL .Path
17+ if ! strings .HasPrefix (upath , "/" ) {
18+ upath = "/" + upath
19+ r .URL .Path = upath
20+ }
21+ serveFile (w , r , f .Root , path .Clean (upath ))
22+ }
23+
24+ func serveFile (w http.ResponseWriter , r * http.Request , fs http.FileSystem , name string ) {
25+ f , err := fs .Open (name + ".json" )
26+ if err != nil {
27+ w .Header ().Set ("Content-Type" , "application/json" )
28+ w .WriteHeader (http .StatusNotFound )
29+ fmt .Fprintln (w , fmt .Sprintf (`{"message": "%s"}` , err ))
30+ return
31+ }
32+ defer f .Close ()
33+
34+ var data interface {}
35+ if err := json .NewDecoder (f ).Decode (& data ); err != nil {
36+ w .Header ().Set ("Content-Type" , "application/json" )
37+ w .WriteHeader (http .StatusInternalServerError )
38+ fmt .Fprintln (w , fmt .Sprintf (`{"message": "%s"}` , err ))
39+ return
40+ }
41+
42+ w .Header ().Set ("Content-Type" , "application/json" )
43+ json .NewEncoder (w ).Encode (data )
44+ }
You can’t perform that action at this time.
0 commit comments