Added params as a parameter for each handler.
This commit is contained in:
+1
-1
@@ -1,2 +1,2 @@
|
|||||||
client: go run client.go
|
client: go run client.go
|
||||||
server: go run ../main.go -config ./functional-config.json -logtostderr
|
server: go run ../main.go -config ./functional-config.json -alsologtostderr
|
||||||
+11
-4
@@ -10,13 +10,20 @@ type applicationContext struct {
|
|||||||
DB db
|
DB db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// url params
|
||||||
|
type params map[string]string
|
||||||
|
|
||||||
|
func (p params) Get(key string) string {
|
||||||
|
return p[key]
|
||||||
|
}
|
||||||
|
|
||||||
// A handlerHTTPC responds to an HTTP request with custom application context.
|
// A handlerHTTPC responds to an HTTP request with custom application context.
|
||||||
type handlerHTTPC interface {
|
type handlerHTTPC interface {
|
||||||
ServeHTTPC(ctx *applicationContext, w http.ResponseWriter, r *http.Request)
|
ServeHTTPC(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
type handlerHTTPCFunc func(ctx *applicationContext, w http.ResponseWriter, r *http.Request)
|
type handlerHTTPCFunc func(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request)
|
||||||
|
|
||||||
func (c handlerHTTPCFunc) ServeHTTPC(ctx *applicationContext, w http.ResponseWriter, r *http.Request) {
|
func (c handlerHTTPCFunc) ServeHTTPC(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request) {
|
||||||
c(ctx, w, r)
|
c(ctx, p, w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-33
@@ -13,7 +13,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
log "github.com/golang/glog"
|
log "github.com/golang/glog"
|
||||||
"github.com/gorilla/mux"
|
|
||||||
|
|
||||||
"github.com/dimiro1/ipe/utils"
|
"github.com/dimiro1/ipe/utils"
|
||||||
)
|
)
|
||||||
@@ -48,9 +47,8 @@ func prepareQueryString(params url.Values) string {
|
|||||||
// * The query parameters sorted by key, with keys converted to lowercase, then joined as in the query string.
|
// * The query parameters sorted by key, with keys converted to lowercase, then joined as in the query string.
|
||||||
// Note that the string must not be url escaped (e.g. given the keys auth_key: foo, Name: Something else, you get auth_key=foo&name=Something else)
|
// Note that the string must not be url escaped (e.g. given the keys auth_key: foo, Name: Something else, you get auth_key=foo&name=Something else)
|
||||||
func restAuthenticationHandler(ctx *applicationContext, h handlerHTTPC) handlerHTTPC {
|
func restAuthenticationHandler(ctx *applicationContext, h handlerHTTPC) handlerHTTPC {
|
||||||
return handlerHTTPCFunc(func(ctx *applicationContext, w http.ResponseWriter, r *http.Request) {
|
return handlerHTTPCFunc(func(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
appID := p.Get("app_id")
|
||||||
appID := vars["app_id"]
|
|
||||||
|
|
||||||
app, err := ctx.DB.GetAppByAppID(appID)
|
app, err := ctx.DB.GetAppByAppID(appID)
|
||||||
|
|
||||||
@@ -60,17 +58,17 @@ func restAuthenticationHandler(ctx *applicationContext, h handlerHTTPC) handlerH
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
params := r.URL.Query()
|
query := r.URL.Query()
|
||||||
|
|
||||||
signature := params.Get("auth_signature")
|
signature := query.Get("auth_signature")
|
||||||
params.Del("auth_signature")
|
query.Del("auth_signature")
|
||||||
|
|
||||||
queryString := prepareQueryString(params)
|
queryString := prepareQueryString(query)
|
||||||
|
|
||||||
toSign := strings.ToUpper(r.Method) + "\n" + r.URL.Path + "\n" + queryString
|
toSign := strings.ToUpper(r.Method) + "\n" + r.URL.Path + "\n" + queryString
|
||||||
|
|
||||||
if utils.HashMAC([]byte(toSign), []byte(app.Secret)) == signature {
|
if utils.HashMAC([]byte(toSign), []byte(app.Secret)) == signature {
|
||||||
h.ServeHTTPC(ctx, w, r)
|
h.ServeHTTPC(ctx, p, w, r)
|
||||||
} else {
|
} else {
|
||||||
log.Error("Not authorized")
|
log.Error("Not authorized")
|
||||||
http.Error(w, "Not authorized", http.StatusUnauthorized)
|
http.Error(w, "Not authorized", http.StatusUnauthorized)
|
||||||
@@ -80,9 +78,8 @@ func restAuthenticationHandler(ctx *applicationContext, h handlerHTTPC) handlerH
|
|||||||
|
|
||||||
// Check if the application is disabled
|
// Check if the application is disabled
|
||||||
func restCheckAppDisabledHandler(ctx *applicationContext, h handlerHTTPC) handlerHTTPC {
|
func restCheckAppDisabledHandler(ctx *applicationContext, h handlerHTTPC) handlerHTTPC {
|
||||||
return handlerHTTPCFunc(func(ctx *applicationContext, w http.ResponseWriter, r *http.Request) {
|
return handlerHTTPCFunc(func(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
appID := p.Get("app_id")
|
||||||
appID := vars["app_id"]
|
|
||||||
|
|
||||||
currentApp, err := ctx.DB.GetAppByAppID(appID)
|
currentApp, err := ctx.DB.GetAppByAppID(appID)
|
||||||
|
|
||||||
@@ -96,7 +93,7 @@ func restCheckAppDisabledHandler(ctx *applicationContext, h handlerHTTPC) handle
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
h.ServeHTTPC(ctx, w, r)
|
h.ServeHTTPC(ctx, p, w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,9 +117,8 @@ func commonHandlers(ctx *applicationContext, h handlerHTTPCFunc) handlerHTTPC {
|
|||||||
// Response is an empty JSON hash.
|
// Response is an empty JSON hash.
|
||||||
//
|
//
|
||||||
// POST /apps/{app_id}/events
|
// POST /apps/{app_id}/events
|
||||||
func postEvents(ctx *applicationContext, w http.ResponseWriter, r *http.Request) {
|
func postEvents(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
appID := p.Get("app_id")
|
||||||
appID := vars["app_id"]
|
|
||||||
|
|
||||||
app, err := ctx.DB.GetAppByAppID(appID)
|
app, err := ctx.DB.GetAppByAppID(appID)
|
||||||
|
|
||||||
@@ -186,13 +182,12 @@ func postEvents(ctx *applicationContext, w http.ResponseWriter, r *http.Request)
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// GET /apps/{app_id}/channels
|
// GET /apps/{app_id}/channels
|
||||||
func getChannels(ctx *applicationContext, w http.ResponseWriter, r *http.Request) {
|
func getChannels(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request) {
|
||||||
params := r.URL.Query()
|
query := r.URL.Query()
|
||||||
vars := mux.Vars(r)
|
|
||||||
|
|
||||||
appID := vars["app_id"]
|
appID := p.Get("app_id")
|
||||||
filter := params.Get("filter_by_prefix")
|
filter := query.Get("filter_by_prefix")
|
||||||
info := params.Get("info")
|
info := query.Get("info")
|
||||||
|
|
||||||
attributes := strings.Split(info, ",")
|
attributes := strings.Split(info, ",")
|
||||||
|
|
||||||
@@ -267,20 +262,19 @@ func getChannels(ctx *applicationContext, w http.ResponseWriter, r *http.Request
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// GET /apps/{app_id}/channels/{channel_name}
|
// GET /apps/{app_id}/channels/{channel_name}
|
||||||
func getChannel(ctx *applicationContext, w http.ResponseWriter, r *http.Request) {
|
func getChannel(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
||||||
|
|
||||||
params := r.URL.Query()
|
query := r.URL.Query()
|
||||||
vars := mux.Vars(r)
|
|
||||||
|
|
||||||
appID := vars["app_id"]
|
appID := p.Get("app_id")
|
||||||
app, err := ctx.DB.GetAppByAppID(appID)
|
app, err := ctx.DB.GetAppByAppID(appID)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, fmt.Sprintf("Could not found an app with app_id: %s", appID), http.StatusBadRequest)
|
http.Error(w, fmt.Sprintf("Could not found an app with app_id: %s", appID), http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
channelName := vars["channel_name"]
|
channelName := p.Get("channel_name")
|
||||||
|
|
||||||
// Channel name could not be empty
|
// Channel name could not be empty
|
||||||
if strings.TrimSpace(channelName) == "" {
|
if strings.TrimSpace(channelName) == "" {
|
||||||
@@ -288,7 +282,7 @@ func getChannel(ctx *applicationContext, w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
info := params.Get("info")
|
info := query.Get("info")
|
||||||
attributes := strings.Split(info, ",")
|
attributes := strings.Split(info, ",")
|
||||||
|
|
||||||
// Attributes requested
|
// Attributes requested
|
||||||
@@ -357,11 +351,9 @@ func getChannel(ctx *applicationContext, w http.ResponseWriter, r *http.Request)
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// GET /apps/{app_id}/channels/{channel_name}/users
|
// GET /apps/{app_id}/channels/{channel_name}/users
|
||||||
func getChannelUsers(ctx *applicationContext, w http.ResponseWriter, r *http.Request) {
|
func getChannelUsers(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
appID := p.Get("app_id")
|
||||||
|
channelName := p.Get("channel_name")
|
||||||
appID := vars["app_id"]
|
|
||||||
channelName := vars["channel_name"]
|
|
||||||
|
|
||||||
isPresence := utils.IsPresenceChannel(channelName)
|
isPresence := utils.IsPresenceChannel(channelName)
|
||||||
|
|
||||||
|
|||||||
+9
-5
@@ -24,14 +24,18 @@ func newRouter(ctx *applicationContext) *router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *router) GET(path string, handler handlerHTTPC) {
|
func (a *router) GET(path string, handler handlerHTTPC) {
|
||||||
a.mux.Methods("GET").Path(path).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
a.Handle("GET", path, handler)
|
||||||
handler.ServeHTTPC(a.ctx, w, r)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *router) POST(path string, handler handlerHTTPC) {
|
func (a *router) POST(path string, handler handlerHTTPC) {
|
||||||
a.mux.Methods("POST").Path(path).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
a.Handle("POST", path, handler)
|
||||||
handler.ServeHTTPC(a.ctx, w, r)
|
}
|
||||||
|
|
||||||
|
func (a *router) Handle(method, path string, handler handlerHTTPC) {
|
||||||
|
a.mux.Methods(method).Path(path).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
p := params(mux.Vars(r))
|
||||||
|
|
||||||
|
handler.ServeHTTPC(a.ctx, p, w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -13,7 +13,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
log "github.com/golang/glog"
|
log "github.com/golang/glog"
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
"github.com/dimiro1/ipe/utils"
|
"github.com/dimiro1/ipe/utils"
|
||||||
@@ -207,7 +206,7 @@ func onMessage(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, ses
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Websocket GET /app/{key}
|
// Websocket GET /app/{key}
|
||||||
func wsHandler(ctx *applicationContext, w http.ResponseWriter, r *http.Request) {
|
func wsHandler(ctx *applicationContext, p params, w http.ResponseWriter, r *http.Request) {
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
defer func() {
|
defer func() {
|
||||||
if conn != nil {
|
if conn != nil {
|
||||||
@@ -220,8 +219,7 @@ func wsHandler(ctx *applicationContext, w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
vars := mux.Vars(r)
|
appKey := p.Get("key")
|
||||||
appKey := vars["key"]
|
|
||||||
|
|
||||||
app, err := ctx.DB.GetAppByKey(appKey)
|
app, err := ctx.DB.GetAppByKey(appKey)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user