Commit 8cfac050 authored by Stefan Dähling's avatar Stefan Dähling
Browse files

refactoring

parent 7b938332
Pipeline #416171 passed with stages
in 7 minutes and 30 seconds
...@@ -60,7 +60,6 @@ import ( ...@@ -60,7 +60,6 @@ import (
// handleAlive is the handler for requests to path /api/alive // handleAlive is the handler for requests to path /api/alive
func (ams *AMS) handleAlive(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleAlive(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var httpErr error var httpErr error
httpErr = httpreply.Alive(w, nil) httpErr = httpreply.Alive(w, nil)
ams.logErrors(r.URL.Path, nil, httpErr) ams.logErrors(r.URL.Path, nil, httpErr)
...@@ -69,7 +68,6 @@ func (ams *AMS) handleAlive(w http.ResponseWriter, r *http.Request) { ...@@ -69,7 +68,6 @@ func (ams *AMS) handleAlive(w http.ResponseWriter, r *http.Request) {
// handleCloneMAP is the handler for requests to path /api/clonemap // handleCloneMAP is the handler for requests to path /api/clonemap
func (ams *AMS) handleCloneMAP(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleCloneMAP(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
// return info about running clonemap instance // return info about running clonemap instance
...@@ -85,7 +83,6 @@ func (ams *AMS) handleCloneMAP(w http.ResponseWriter, r *http.Request) { ...@@ -85,7 +83,6 @@ func (ams *AMS) handleCloneMAP(w http.ResponseWriter, r *http.Request) {
// handleGetMAS is the handler for get requests to path /api/clonemap/mas // handleGetMAS is the handler for get requests to path /api/clonemap/mas
func (ams *AMS) handleGetMAS(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleGetMAS(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
var mass []schemas.MASInfoShort var mass []schemas.MASInfoShort
...@@ -100,7 +97,6 @@ func (ams *AMS) handleGetMAS(w http.ResponseWriter, r *http.Request) { ...@@ -100,7 +97,6 @@ func (ams *AMS) handleGetMAS(w http.ResponseWriter, r *http.Request) {
// handlePostMAS is the handler for post requests to path /api/clonemap/mas // handlePostMAS is the handler for post requests to path /api/clonemap/mas
func (ams *AMS) handlePostMAS(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handlePostMAS(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
// create new MAS // create new MAS
...@@ -127,7 +123,6 @@ func (ams *AMS) handlePostMAS(w http.ResponseWriter, r *http.Request) { ...@@ -127,7 +123,6 @@ func (ams *AMS) handlePostMAS(w http.ResponseWriter, r *http.Request) {
// handleGetMASID is the handler for get requests to path /api/clonemap/mas/{masid} // handleGetMASID is the handler for get requests to path /api/clonemap/mas/{masid}
func (ams *AMS) handleGetMASID(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleGetMASID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -149,7 +144,6 @@ func (ams *AMS) handleGetMASID(w http.ResponseWriter, r *http.Request) { ...@@ -149,7 +144,6 @@ func (ams *AMS) handleGetMASID(w http.ResponseWriter, r *http.Request) {
// handleDeleteMASID is the handler for delete requests to path /api/clonemap/mas/{masid} // handleDeleteMASID is the handler for delete requests to path /api/clonemap/mas/{masid}
func (ams *AMS) handleDeleteMASID(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleDeleteMASID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -170,7 +164,6 @@ func (ams *AMS) handleDeleteMASID(w http.ResponseWriter, r *http.Request) { ...@@ -170,7 +164,6 @@ func (ams *AMS) handleDeleteMASID(w http.ResponseWriter, r *http.Request) {
// handleGetMASName is the handler for get requests to path /api/clonemap/mas/name/{name} // handleGetMASName is the handler for get requests to path /api/clonemap/mas/name/{name}
func (ams *AMS) handleGetMASName(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleGetMASName(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -188,7 +181,6 @@ func (ams *AMS) handleGetMASName(w http.ResponseWriter, r *http.Request) { ...@@ -188,7 +181,6 @@ func (ams *AMS) handleGetMASName(w http.ResponseWriter, r *http.Request) {
// handleGetAgents is the handler for get requests to path /api/clonemap/mas/{masid}/agents // handleGetAgents is the handler for get requests to path /api/clonemap/mas/{masid}/agents
func (ams *AMS) handleGetAgents(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleGetAgents(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -210,7 +202,6 @@ func (ams *AMS) handleGetAgents(w http.ResponseWriter, r *http.Request) { ...@@ -210,7 +202,6 @@ func (ams *AMS) handleGetAgents(w http.ResponseWriter, r *http.Request) {
// handlePostAgent is the handler for post requests to path /api/clonemap/mas/{masid}/agents // handlePostAgent is the handler for post requests to path /api/clonemap/mas/{masid}/agents
func (ams *AMS) handlePostAgent(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handlePostAgent(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -244,7 +235,6 @@ func (ams *AMS) handlePostAgent(w http.ResponseWriter, r *http.Request) { ...@@ -244,7 +235,6 @@ func (ams *AMS) handlePostAgent(w http.ResponseWriter, r *http.Request) {
// handleGetAgentID is the handler for get requests to path // handleGetAgentID is the handler for get requests to path
// /api/clonemap/mas/{masid}/agents/{agentid} // /api/clonemap/mas/{masid}/agents/{agentid}
func (ams *AMS) handleGetAgentID(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleGetAgentID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r) masID, agentID, cmapErr := getAgentID(r)
...@@ -265,7 +255,6 @@ func (ams *AMS) handleGetAgentID(w http.ResponseWriter, r *http.Request) { ...@@ -265,7 +255,6 @@ func (ams *AMS) handleGetAgentID(w http.ResponseWriter, r *http.Request) {
// handleDeleteAgentID is the handler for delete requests to path // handleDeleteAgentID is the handler for delete requests to path
// /api/clonemap/mas/{masid}/agents/{agentid} // /api/clonemap/mas/{masid}/agents/{agentid}
func (ams *AMS) handleDeleteAgentID(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleDeleteAgentID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r) masID, agentID, cmapErr := getAgentID(r)
...@@ -286,7 +275,6 @@ func (ams *AMS) handleDeleteAgentID(w http.ResponseWriter, r *http.Request) { ...@@ -286,7 +275,6 @@ func (ams *AMS) handleDeleteAgentID(w http.ResponseWriter, r *http.Request) {
// handleGetAgentAddress is the handler for get requests to path // handleGetAgentAddress is the handler for get requests to path
// /api/clonemap/mas/{masid}/agents/{agentid}/address // /api/clonemap/mas/{masid}/agents/{agentid}/address
func (ams *AMS) handleGetAgentAddress(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleGetAgentAddress(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r) masID, agentID, cmapErr := getAgentID(r)
...@@ -304,7 +292,6 @@ func (ams *AMS) handleGetAgentAddress(w http.ResponseWriter, r *http.Request) { ...@@ -304,7 +292,6 @@ func (ams *AMS) handleGetAgentAddress(w http.ResponseWriter, r *http.Request) {
// handlePutAgentAddress is the handler for put requests to path // handlePutAgentAddress is the handler for put requests to path
// /api/clonemap/mas/{masid}/agents/{agentid}/address // /api/clonemap/mas/{masid}/agents/{agentid}/address
func (ams *AMS) handlePutAgentAddress(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handlePutAgentAddress(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r) masID, agentID, cmapErr := getAgentID(r)
...@@ -337,7 +324,6 @@ func (ams *AMS) handlePutAgentAddress(w http.ResponseWriter, r *http.Request) { ...@@ -337,7 +324,6 @@ func (ams *AMS) handlePutAgentAddress(w http.ResponseWriter, r *http.Request) {
// handlePutAgentCustom is the put handler for requests to path // handlePutAgentCustom is the put handler for requests to path
// /api/clonemap/mas/{masid}/agents/{agentid}/custom // /api/clonemap/mas/{masid}/agents/{agentid}/custom
func (ams *AMS) handlePutAgentCustom(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handlePutAgentCustom(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r) masID, agentID, cmapErr := getAgentID(r)
...@@ -365,7 +351,6 @@ func (ams *AMS) handlePutAgentCustom(w http.ResponseWriter, r *http.Request) { ...@@ -365,7 +351,6 @@ func (ams *AMS) handlePutAgentCustom(w http.ResponseWriter, r *http.Request) {
// handleGetAgentName is the handler for get requests to path // handleGetAgentName is the handler for get requests to path
// /api/clonemap/mas/{masid}/agents/name/{name} // /api/clonemap/mas/{masid}/agents/name/{name}
func (ams *AMS) handleGetAgentName(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleGetAgentName(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -388,7 +373,6 @@ func (ams *AMS) handleGetAgentName(w http.ResponseWriter, r *http.Request) { ...@@ -388,7 +373,6 @@ func (ams *AMS) handleGetAgentName(w http.ResponseWriter, r *http.Request) {
// handleGetAgencies is the handler for get requests to path /api/cloumap/mas/{masid}/agencies // handleGetAgencies is the handler for get requests to path /api/cloumap/mas/{masid}/agencies
func (ams *AMS) handleGetAgencies(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleGetAgencies(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -411,7 +395,6 @@ func (ams *AMS) handleGetAgencies(w http.ResponseWriter, r *http.Request) { ...@@ -411,7 +395,6 @@ func (ams *AMS) handleGetAgencies(w http.ResponseWriter, r *http.Request) {
// handleGetAgencyID is the handler for get requests to path // handleGetAgencyID is the handler for get requests to path
// /api/clonemap/mas/{masid}/imgroup/{imid}/agencies/{agencyid} // /api/clonemap/mas/{masid}/imgroup/{imid}/agencies/{agencyid}
func (ams *AMS) handleGetAgencyID(w http.ResponseWriter, r *http.Request) { func (ams *AMS) handleGetAgencyID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr) defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -442,7 +425,6 @@ func (ams *AMS) handleGetAgencyID(w http.ResponseWriter, r *http.Request) { ...@@ -442,7 +425,6 @@ func (ams *AMS) handleGetAgencyID(w http.ResponseWriter, r *http.Request) {
// methodNotAllowed is the default handler for valid paths but invalid methods // methodNotAllowed is the default handler for valid paths but invalid methods
func (ams *AMS) methodNotAllowed(w http.ResponseWriter, r *http.Request) { func (ams *AMS) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.MethodNotAllowed(w) httpErr := httpreply.MethodNotAllowed(w)
cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path) cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path)
ams.logErrors(r.URL.Path, cmapErr, httpErr) ams.logErrors(r.URL.Path, cmapErr, httpErr)
...@@ -451,7 +433,6 @@ func (ams *AMS) methodNotAllowed(w http.ResponseWriter, r *http.Request) { ...@@ -451,7 +433,6 @@ func (ams *AMS) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
// resourceNotFound is the default handler for invalid paths // resourceNotFound is the default handler for invalid paths
func (ams *AMS) resourceNotFound(w http.ResponseWriter, r *http.Request) { func (ams *AMS) resourceNotFound(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.NotFoundError(w) httpErr := httpreply.NotFoundError(w)
cmapErr := errors.New("Resource not found") cmapErr := errors.New("Resource not found")
ams.logErrors(r.URL.Path, cmapErr, httpErr) ams.logErrors(r.URL.Path, cmapErr, httpErr)
...@@ -483,6 +464,14 @@ func getAgentID(r *http.Request) (masID int, agentID int, err error) { ...@@ -483,6 +464,14 @@ func getAgentID(r *http.Request) (masID int, agentID int, err error) {
return return
} }
// loggingMiddleware logs request before calling final handler
func (ams *AMS) loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
next.ServeHTTP(w, r)
})
}
// server creates the ams server // server creates the ams server
func (ams *AMS) server(port int) (serv *http.Server) { func (ams *AMS) server(port int) (serv *http.Server) {
r := mux.NewRouter() r := mux.NewRouter()
...@@ -533,7 +522,7 @@ func (ams *AMS) server(port int) (serv *http.Server) { ...@@ -533,7 +522,7 @@ func (ams *AMS) server(port int) (serv *http.Server) {
s.Path("/clonemap/mas/{masid}/imgroup/{imid}/agency/{agencyid}"). s.Path("/clonemap/mas/{masid}/imgroup/{imid}/agency/{agencyid}").
Methods("PUT", "DELETE", "POST").HandlerFunc(ams.methodNotAllowed) Methods("PUT", "DELETE", "POST").HandlerFunc(ams.methodNotAllowed)
s.PathPrefix("").HandlerFunc(ams.resourceNotFound) s.PathPrefix("").HandlerFunc(ams.resourceNotFound)
s.Use(ams.loggingMiddleware)
serv = &http.Server{ serv = &http.Server{
Addr: ":" + strconv.Itoa(port), Addr: ":" + strconv.Itoa(port),
Handler: r, Handler: r,
......
...@@ -58,7 +58,6 @@ import ( ...@@ -58,7 +58,6 @@ import (
// handleAlive is the handler for requests to path /api/alive // handleAlive is the handler for requests to path /api/alive
func (df *DF) handleAlive(w http.ResponseWriter, r *http.Request) { func (df *DF) handleAlive(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var httpErr error var httpErr error
httpErr = httpreply.Alive(w, nil) httpErr = httpreply.Alive(w, nil)
df.logErrors(r.URL.Path, nil, httpErr) df.logErrors(r.URL.Path, nil, httpErr)
...@@ -67,7 +66,6 @@ func (df *DF) handleAlive(w http.ResponseWriter, r *http.Request) { ...@@ -67,7 +66,6 @@ func (df *DF) handleAlive(w http.ResponseWriter, r *http.Request) {
// handleGetMASService is the handler for get requests to path /api/df/{masid}/svc // handleGetMASService is the handler for get requests to path /api/df/{masid}/svc
func (df *DF) handleGetMASService(w http.ResponseWriter, r *http.Request) { func (df *DF) handleGetMASService(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr) defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -88,7 +86,6 @@ func (df *DF) handleGetMASService(w http.ResponseWriter, r *http.Request) { ...@@ -88,7 +86,6 @@ func (df *DF) handleGetMASService(w http.ResponseWriter, r *http.Request) {
// handlePostMASService is the handler for post requests to path /api/df/{masid}/svc // handlePostMASService is the handler for post requests to path /api/df/{masid}/svc
func (df *DF) handlePostMASService(w http.ResponseWriter, r *http.Request) { func (df *DF) handlePostMASService(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr) defer df.logErrors(r.URL.Path, cmapErr, httpErr)
var body []byte var body []byte
...@@ -118,7 +115,6 @@ func (df *DF) handlePostMASService(w http.ResponseWriter, r *http.Request) { ...@@ -118,7 +115,6 @@ func (df *DF) handlePostMASService(w http.ResponseWriter, r *http.Request) {
// handleGetMASGraph is the handler for get requests to path /api/df/{masid}/graph // handleGetMASGraph is the handler for get requests to path /api/df/{masid}/graph
func (df *DF) handleGetMASGraph(w http.ResponseWriter, r *http.Request) { func (df *DF) handleGetMASGraph(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr) defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -139,7 +135,6 @@ func (df *DF) handleGetMASGraph(w http.ResponseWriter, r *http.Request) { ...@@ -139,7 +135,6 @@ func (df *DF) handleGetMASGraph(w http.ResponseWriter, r *http.Request) {
// handlePostMASGraph is the handler for post and put requests to path /api/df/{masid}/graph // handlePostMASGraph is the handler for post and put requests to path /api/df/{masid}/graph
func (df *DF) handlePostMASGraph(w http.ResponseWriter, r *http.Request) { func (df *DF) handlePostMASGraph(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr) defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -171,7 +166,6 @@ func (df *DF) handlePostMASGraph(w http.ResponseWriter, r *http.Request) { ...@@ -171,7 +166,6 @@ func (df *DF) handlePostMASGraph(w http.ResponseWriter, r *http.Request) {
// handleGetSvcDesc is the handler for get requests to path /api/df/{masid}/svc/desc/{desc} // handleGetSvcDesc is the handler for get requests to path /api/df/{masid}/svc/desc/{desc}
func (df *DF) handleGetSvcDesc(w http.ResponseWriter, r *http.Request) { func (df *DF) handleGetSvcDesc(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr) defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -194,7 +188,6 @@ func (df *DF) handleGetSvcDesc(w http.ResponseWriter, r *http.Request) { ...@@ -194,7 +188,6 @@ func (df *DF) handleGetSvcDesc(w http.ResponseWriter, r *http.Request) {
// handleGetSvcNode is the handler for get requests to path // handleGetSvcNode is the handler for get requests to path
// /api/df/{masid}/svc/desc/{desc}/node/{nodeid}/dist/{dist} // /api/df/{masid}/svc/desc/{desc}/node/{nodeid}/dist/{dist}
func (df *DF) handleGetSvcNodeDist(w http.ResponseWriter, r *http.Request) { func (df *DF) handleGetSvcNodeDist(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr) defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -226,7 +219,6 @@ func (df *DF) handleGetSvcNodeDist(w http.ResponseWriter, r *http.Request) { ...@@ -226,7 +219,6 @@ func (df *DF) handleGetSvcNodeDist(w http.ResponseWriter, r *http.Request) {
// handleGetSvcID is the handler for get requests to path /api/df/{masid}/svc/id/{svcid} // handleGetSvcID is the handler for get requests to path /api/df/{masid}/svc/id/{svcid}
func (df *DF) handleGetSvcID(w http.ResponseWriter, r *http.Request) { func (df *DF) handleGetSvcID(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr) defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -248,7 +240,6 @@ func (df *DF) handleGetSvcID(w http.ResponseWriter, r *http.Request) { ...@@ -248,7 +240,6 @@ func (df *DF) handleGetSvcID(w http.ResponseWriter, r *http.Request) {
// handleDeleteSvcID is the handler for delete requests to path /api/df/{masid}/svc/id/{svcid} // handleDeleteSvcID is the handler for delete requests to path /api/df/{masid}/svc/id/{svcid}
func (df *DF) handleDeleteSvcID(w http.ResponseWriter, r *http.Request) { func (df *DF) handleDeleteSvcID(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr) defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -269,7 +260,6 @@ func (df *DF) handleDeleteSvcID(w http.ResponseWriter, r *http.Request) { ...@@ -269,7 +260,6 @@ func (df *DF) handleDeleteSvcID(w http.ResponseWriter, r *http.Request) {
// methodNotAllowed is the default handler for valid paths but invalid methods // methodNotAllowed is the default handler for valid paths but invalid methods
func (df *DF) methodNotAllowed(w http.ResponseWriter, r *http.Request) { func (df *DF) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.MethodNotAllowed(w) httpErr := httpreply.MethodNotAllowed(w)
cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path) cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path)
df.logErrors(r.URL.Path, cmapErr, httpErr) df.logErrors(r.URL.Path, cmapErr, httpErr)
...@@ -278,7 +268,6 @@ func (df *DF) methodNotAllowed(w http.ResponseWriter, r *http.Request) { ...@@ -278,7 +268,6 @@ func (df *DF) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
// resourceNotFound is the default handler for invalid paths // resourceNotFound is the default handler for invalid paths
func (df *DF) resourceNotFound(w http.ResponseWriter, r *http.Request) { func (df *DF) resourceNotFound(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.NotFoundError(w) httpErr := httpreply.NotFoundError(w)
cmapErr := errors.New("Resource not found") cmapErr := errors.New("Resource not found")
df.logErrors(r.URL.Path, cmapErr, httpErr) df.logErrors(r.URL.Path, cmapErr, httpErr)
...@@ -296,6 +285,14 @@ func (df *DF) logErrors(path string, cmapErr error, httpErr error) { ...@@ -296,6 +285,14 @@ func (df *DF) logErrors(path string, cmapErr error, httpErr error) {
return return
} }
// loggingMiddleware logs request before calling final handler
func (df *DF) loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
next.ServeHTTP(w, r)
})
}
// server creates the df server // server creates the df server
func (df *DF) server(port int) (serv *http.Server) { func (df *DF) server(port int) (serv *http.Server) {
r := mux.NewRouter() r := mux.NewRouter()
...@@ -320,6 +317,7 @@ func (df *DF) server(port int) (serv *http.Server) { ...@@ -320,6 +317,7 @@ func (df *DF) server(port int) (serv *http.Server) {
s.Path("/df/{masid}/svc/id/{svcid}").Methods("DELETE").HandlerFunc(df.handleDeleteSvcID) s.Path("/df/{masid}/svc/id/{svcid}").Methods("DELETE").HandlerFunc(df.handleDeleteSvcID)
s.Path("/df/{masid}/svc/id/{svcid}").Methods("POST", "PUT").HandlerFunc(df.methodNotAllowed) s.Path("/df/{masid}/svc/id/{svcid}").Methods("POST", "PUT").HandlerFunc(df.methodNotAllowed)
s.PathPrefix("").HandlerFunc(df.resourceNotFound) s.PathPrefix("").HandlerFunc(df.resourceNotFound)
s.Use(df.loggingMiddleware)
serv = &http.Server{ serv = &http.Server{
Addr: ":" + strconv.Itoa(port), Addr: ":" + strconv.Itoa(port),
Handler: r, Handler: r,
......
...@@ -57,7 +57,6 @@ import ( ...@@ -57,7 +57,6 @@ import (
// handleGetMASs is the handler for get requests to path /api/ams/mas // handleGetMASs is the handler for get requests to path /api/ams/mas
func (fe *Frontend) handleGetMASs(w http.ResponseWriter, r *http.Request) { func (fe *Frontend) handleGetMASs(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr) defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
// return short info of all MAS // return short info of all MAS
...@@ -73,7 +72,6 @@ func (fe *Frontend) handleGetMASs(w http.ResponseWriter, r *http.Request) { ...@@ -73,7 +72,6 @@ func (fe *Frontend) handleGetMASs(w http.ResponseWriter, r *http.Request) {
// handlePostMASs is the handler for post requests to path /api/ams/mas // handlePostMASs is the handler for post requests to path /api/ams/mas
func (fe *Frontend) handlePostMAS(w http.ResponseWriter, r *http.Request) { func (fe *Frontend) handlePostMAS(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr) defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
var body []byte var body []byte
...@@ -99,7 +97,6 @@ func (fe *Frontend) handlePostMAS(w http.ResponseWriter, r *http.Request) { ...@@ -99,7 +97,6 @@ func (fe *Frontend) handlePostMAS(w http.ResponseWriter, r *http.Request) {
// handleGetMASID is the handler for get requests to path /api/ams/mas/{masid} // handleGetMASID is the handler for get requests to path /api/ams/mas/{masid}
func (fe *Frontend) handleGetMASID(w http.ResponseWriter, r *http.Request) { func (fe *Frontend) handleGetMASID(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr) defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -126,7 +123,6 @@ func (fe *Frontend) handleDeleteMASID(w http.ResponseWriter, r *http.Request) { ...@@ -126,7 +123,6 @@ func (fe *Frontend) handleDeleteMASID(w http.ResponseWriter, r *http.Request) {
// handlePostAgent is the handler for post requests to path /api/clonemap/mas/{masid}/agents // handlePostAgent is the handler for post requests to path /api/clonemap/mas/{masid}/agents
func (fe *Frontend) handlePostAgent(w http.ResponseWriter, r *http.Request) { func (fe *Frontend) handlePostAgent(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr) defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r) vars := mux.Vars(r)
...@@ -159,7 +155,6 @@ func (fe *Frontend) handlePostAgent(w http.ResponseWriter, r *http.Request) { ...@@ -159,7 +155,6 @@ func (fe *Frontend) handlePostAgent(w http.ResponseWriter, r *http.Request) {
// handleGetAgentID is the handler for get requests to path /api/ams/mas/{masid}/agents/{agentid} // handleGetAgentID is the handler for get requests to path /api/ams/mas/{masid}/agents/{agentid}
func (fe *Frontend) handleGetAgentID(w http.ResponseWriter, r *http.Request) { func (fe *Frontend) handleGetAgentID(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr) defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r) masID, agentID, cmapErr := getAgentID(r)
...@@ -181,7 +176,6 @@ func (fe *Frontend) handleGetAgentID(w http.ResponseWriter, r *http.Request) { ...@@ -181,7 +176,6 @@ func (fe *Frontend) handleGetAgentID(w http.ResponseWriter, r *http.Request) {
// handleDeleteAgentID is the handler for delete requests to path // handleDeleteAgentID is the handler for delete requests to path
// /api/ams/mas/{masid}/agents/{agentid} // /api/ams/mas/{masid}/agents/{agentid}
func (fe *Frontend) handleDeleteAgentID(w http.ResponseWriter, r *http.Request) { func (fe *Frontend) handleDeleteAgentID(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr) defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r) masID, agentID, cmapErr := getAgentID(r)
......
...@@ -56,7 +56,6 @@ import ( ...@@ -56,7 +56,6 @@ import (
// handleGetModules is the handler for get requests to path /api/pf/modules // handleGetModules is the handler for get requests to path /api/pf/modules
func (fe *Frontend) handleGetModules(w http.ResponseWriter, r *http.Request) { func (fe *Frontend) handleGetModules(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr) defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
var mods schemas.ModuleStatus var mods schemas.ModuleStatus
...@@ -71,7 +70,6 @@ func (fe *Frontend) handleGetModules(w http.ResponseWriter, r *http.Request) { ...@@ -71,7 +70,6 @@ func (fe *Frontend) handleGetModules(w http.ResponseWriter, r *http.Request) {
// methodNotAllowed is the default handler for valid paths but invalid methods // methodNotAllowed is the default handler for valid paths but invalid methods
func (fe *Frontend) methodNotAllowed(w http.ResponseWriter, r *http.Request) { func (fe *Frontend) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.MethodNotAllowed(w) httpErr := httpreply.MethodNotAllowed(w)
cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path) cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path)
fe.logErrors(r.URL.Path, cmapErr, httpErr) fe.logErrors(r.URL.Path, cmapErr, httpErr)
...@@ -80,7 +78,6 @@ func (fe *Frontend) methodNotAllowed(w http.ResponseWriter, r *http.Request) { ...@@ -80,7 +78,6 @@ func (fe *Frontend) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
// resourceNotFound is the default handler for invalid paths // resourceNotFound is the default handler for invalid paths
func (fe *Frontend) resourceNotFound(w http.ResponseWriter, r *http.Request) { func (fe *Frontend) resourceNotFound(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.NotFoundError(w) httpErr := httpreply.NotFoundError(w)
cmapErr := errors.New("Resource not found") cmapErr := errors.New("Resource not found")
fe.logErrors(r.URL.Path, cmapErr, httpErr) fe.logErrors(r.URL.Path, cmapErr, httpErr)
...@@ -112,6 +109,14 @@ func getAgentID(r *http.Request) (masID int, agentID int, err error) { ...@@ -112,6 +109,14 @@ func getAgentID(r *http.Request) (masID int, agentID int, err error) {
return return
} }
// loggingMiddleware logs request before calling final handler
func (fe *Frontend) loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
next.ServeHTTP(w, r)
})
}
// server creates the fe server // server creates the fe server
func (fe *Frontend) server(port int) (serv *http.Server) { func (fe *Frontend) server(port int) (serv *http.Server) {
r := mux.NewRouter() r := mux.NewRouter()
...@@ -135,6 +140,7 @@ func (fe *Frontend) server(port int) (serv *http.Server) { ...@@ -135,6 +140,7 @@ func (fe *Frontend) server(port int) (serv *http.Server) {
s.Path("/pf/modules").Methods("GET").HandlerFunc(fe.handleGetModules) s.Path("/pf/modules").Methods("GET").HandlerFunc(fe.handleGetModules)
s.Path("/pf/modules").Methods("POST", "PUT", "POST").HandlerFunc(fe.methodNotAllowed) s.Path("/pf/modules").Methods("POST", "PUT", "POST").HandlerFunc(fe.methodNotAllowed)
s.PathPrefix("").HandlerFunc(fe.resourceNotFound) s.PathPrefix("").HandlerFunc(fe.resourceNotFound)
s.Use(fe.loggingMiddleware)
r.HandleFunc("/", http.FileServer(http.Dir("./web")).ServeHTTP)