/
server.go
98 lines (82 loc) · 2.46 KB
/
server.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"gitlab.dian.fm/livecloud/config-server/pkg/database"
"github.com/go-martini/martini"
"github.com/martini-contrib/encoder"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
)
type NewError struct {
errcode int
errmsg string
}
func GetServer(enc encoder.Encoder, db database.DB, parms martini.Params) (int, []byte) {
fmt.Printf("begin get server\n")
id, err := strconv.ParseInt(parms["id"], 10, 64)
al := db.Get(id)
if err != nil || al == nil {
msg := fmt.Sprintf("the album with id %s does not exist", parms["id"])
return http.StatusNotFound, encoder.Must(enc.Encode(
NewError{errcode: 404, errmsg: msg}))
}
return http.StatusOK, encoder.Must(enc.Encode(al))
}
func FindServer(enc encoder.Encoder, db database.DB, r *http.Request) (int, []byte) {
fmt.Printf("begin find server\n")
params := r.URL.Query()
roomId, err := strconv.ParseInt(params.Get("room_id"), 10, 64)
al := db.Find(roomId)
if err != nil || al == nil {
msg := fmt.Sprintf("server witch room_id %s does not exist", params.Get("id"))
return http.StatusNotFound, encoder.Must(enc.Encode(
NewError{errcode: 404, errmsg: msg}))
}
return http.StatusOK, encoder.Must(enc.Encode(al))
}
var rxExt = regexp.MustCompile(`(\.(?:xml|json))\/?$`)
func MapEncoder(c martini.Context, w http.ResponseWriter, r *http.Request) {
// Get the format extension
matches := rxExt.FindStringSubmatch(r.URL.Path)
ext := ".json"
if len(matches) > 1 {
// Rewrite the URL without the format extension
lentghWithoutExt := len(r.URL.Path) - len(matches[1])
if strings.HasSuffix(r.URL.Path, "/") {
lentghWithoutExt--
}
r.URL.Path = r.URL.Path[:lentghWithoutExt]
ext = matches[1]
}
// Inject the requested encoder
switch ext {
case ".xml":
c.MapTo(encoder.XmlEncoder{}, (*encoder.Encoder)(nil))
w.Header().Set("Content-Type", "application/xml")
default:
c.MapTo(encoder.JsonEncoder{}, (*encoder.Encoder)(nil))
w.Header().Set("Content-Type", "application/json")
}
}
func init() {
}
func main() {
fmt.Printf("begin \n")
m := martini.New()
m.Use(martini.Recovery())
m.Use(martini.Logger())
m.Use(MapEncoder)
router := martini.NewRouter()
router.Get(`/servers`, FindServer)
router.Get(`/servers/:id`, GetServer)
// router.Post(`/servers`, AddAlbum)
// router.Put(`/servers/:id`, UpdateAlbum)
// router.Delete(`/servers/:id`, DeleteAlbum)
db := database.NewServerDB()
m.MapTo(&db, (*database.DB)(nil))
m.Action(router.Handle)
m.Run()
fmt.Printf("end \n")
}