-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
66 lines (51 loc) · 1.3 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"io"
"io/ioutil"
"github.com/codegangsta/martini"
"net/http"
"net/url"
"strings"
)
type Service interface {
InitRoutes(routes []Route)
Start(settings Settings)
}
type ServiceImpl struct {
routes []Route
}
func (service *ServiceImpl) InitRoutes(routes []Route) {
service.routes = routes
}
func (service *ServiceImpl) Start(settings Settings) {
m := martini.New()
m.Use(ParseRoute(service))
http.ListenAndServe(":3000", m)
}
func ParseRoute(service *ServiceImpl) martini.Handler {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-type", "application/json")
for _, b := range service.routes {
path := strings.TrimPrefix(r.URL.Path, "/")
if b.path == path {
if r.Method == "GET" {
fmt.Println(r.URL.RawQuery)
m, _ := url.ParseQuery(r.URL.RawQuery)
// need to wrap the file in a function if jsonp
content, _ := ioutil.ReadFile(b.path + "/GET")
// check to see if this is a jsonp call
if value, ok := m["callback"]; ok {
s := fmt.Sprintf("%s(%s)", value[0], string(content))
io.WriteString(w, s)
} else {
io.WriteString(w, string(content))
}
}
}
}
fmt.Println(r.URL.Path)
r.URL.Path = ""
}
}