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 (
// handleAlive is the handler for requests to path /api/alive
func (ams *AMS) handleAlive(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var httpErr error
httpErr = httpreply.Alive(w, nil)
ams.logErrors(r.URL.Path, nil, httpErr)
......@@ -69,7 +68,6 @@ func (ams *AMS) handleAlive(w http.ResponseWriter, r *http.Request) {
// handleCloneMAP is the handler for requests to path /api/clonemap
func (ams *AMS) handleCloneMAP(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
// return info about running clonemap instance
......@@ -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
func (ams *AMS) handleGetMAS(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
var mass []schemas.MASInfoShort
......@@ -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
func (ams *AMS) handlePostMAS(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
// create new MAS
......@@ -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}
func (ams *AMS) handleGetMASID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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}
func (ams *AMS) handleDeleteMASID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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}
func (ams *AMS) handleGetMASName(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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
func (ams *AMS) handleGetAgents(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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
func (ams *AMS) handlePostAgent(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -244,7 +235,6 @@ func (ams *AMS) handlePostAgent(w http.ResponseWriter, r *http.Request) {
// handleGetAgentID is the handler for get requests to path
// /api/clonemap/mas/{masid}/agents/{agentid}
func (ams *AMS) handleGetAgentID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -265,7 +255,6 @@ func (ams *AMS) handleGetAgentID(w http.ResponseWriter, r *http.Request) {
// handleDeleteAgentID is the handler for delete requests to path
// /api/clonemap/mas/{masid}/agents/{agentid}
func (ams *AMS) handleDeleteAgentID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -286,7 +275,6 @@ func (ams *AMS) handleDeleteAgentID(w http.ResponseWriter, r *http.Request) {
// handleGetAgentAddress is the handler for get requests to path
// /api/clonemap/mas/{masid}/agents/{agentid}/address
func (ams *AMS) handleGetAgentAddress(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -304,7 +292,6 @@ func (ams *AMS) handleGetAgentAddress(w http.ResponseWriter, r *http.Request) {
// handlePutAgentAddress is the handler for put requests to path
// /api/clonemap/mas/{masid}/agents/{agentid}/address
func (ams *AMS) handlePutAgentAddress(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -337,7 +324,6 @@ func (ams *AMS) handlePutAgentAddress(w http.ResponseWriter, r *http.Request) {
// handlePutAgentCustom is the put handler for requests to path
// /api/clonemap/mas/{masid}/agents/{agentid}/custom
func (ams *AMS) handlePutAgentCustom(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -365,7 +351,6 @@ func (ams *AMS) handlePutAgentCustom(w http.ResponseWriter, r *http.Request) {
// handleGetAgentName is the handler for get requests to path
// /api/clonemap/mas/{masid}/agents/name/{name}
func (ams *AMS) handleGetAgentName(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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
func (ams *AMS) handleGetAgencies(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -411,7 +395,6 @@ func (ams *AMS) handleGetAgencies(w http.ResponseWriter, r *http.Request) {
// handleGetAgencyID is the handler for get requests to path
// /api/clonemap/mas/{masid}/imgroup/{imid}/agencies/{agencyid}
func (ams *AMS) handleGetAgencyID(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer ams.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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
func (ams *AMS) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.MethodNotAllowed(w)
cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path)
ams.logErrors(r.URL.Path, cmapErr, httpErr)
......@@ -451,7 +433,6 @@ func (ams *AMS) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
// resourceNotFound is the default handler for invalid paths
func (ams *AMS) resourceNotFound(w http.ResponseWriter, r *http.Request) {
ams.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.NotFoundError(w)
cmapErr := errors.New("Resource not found")
ams.logErrors(r.URL.Path, cmapErr, httpErr)
......@@ -483,6 +464,14 @@ func getAgentID(r *http.Request) (masID int, agentID int, err error) {
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
func (ams *AMS) server(port int) (serv *http.Server) {
r := mux.NewRouter()
......@@ -533,7 +522,7 @@ func (ams *AMS) server(port int) (serv *http.Server) {
s.Path("/clonemap/mas/{masid}/imgroup/{imid}/agency/{agencyid}").
Methods("PUT", "DELETE", "POST").HandlerFunc(ams.methodNotAllowed)
s.PathPrefix("").HandlerFunc(ams.resourceNotFound)
s.Use(ams.loggingMiddleware)
serv = &http.Server{
Addr: ":" + strconv.Itoa(port),
Handler: r,
......
......@@ -58,7 +58,6 @@ import (
// handleAlive is the handler for requests to path /api/alive
func (df *DF) handleAlive(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var httpErr error
httpErr = httpreply.Alive(w, nil)
df.logErrors(r.URL.Path, nil, httpErr)
......@@ -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
func (df *DF) handleGetMASService(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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
func (df *DF) handlePostMASService(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr)
var body []byte
......@@ -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
func (df *DF) handleGetMASGraph(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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
func (df *DF) handlePostMASGraph(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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}
func (df *DF) handleGetSvcDesc(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -194,7 +188,6 @@ func (df *DF) handleGetSvcDesc(w http.ResponseWriter, r *http.Request) {
// handleGetSvcNode is the handler for get requests to path
// /api/df/{masid}/svc/desc/{desc}/node/{nodeid}/dist/{dist}
func (df *DF) handleGetSvcNodeDist(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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}
func (df *DF) handleGetSvcID(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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}
func (df *DF) handleDeleteSvcID(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer df.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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
func (df *DF) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.MethodNotAllowed(w)
cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path)
df.logErrors(r.URL.Path, cmapErr, httpErr)
......@@ -278,7 +268,6 @@ func (df *DF) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
// resourceNotFound is the default handler for invalid paths
func (df *DF) resourceNotFound(w http.ResponseWriter, r *http.Request) {
df.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.NotFoundError(w)
cmapErr := errors.New("Resource not found")
df.logErrors(r.URL.Path, cmapErr, httpErr)
......@@ -296,6 +285,14 @@ func (df *DF) logErrors(path string, cmapErr error, httpErr error) {
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
func (df *DF) server(port int) (serv *http.Server) {
r := mux.NewRouter()
......@@ -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("POST", "PUT").HandlerFunc(df.methodNotAllowed)
s.PathPrefix("").HandlerFunc(df.resourceNotFound)
s.Use(df.loggingMiddleware)
serv = &http.Server{
Addr: ":" + strconv.Itoa(port),
Handler: r,
......
......@@ -57,7 +57,6 @@ import (
// handleGetMASs is the handler for get requests to path /api/ams/mas
func (fe *Frontend) handleGetMASs(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
// return short info of all MAS
......@@ -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
func (fe *Frontend) handlePostMAS(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
var body []byte
......@@ -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}
func (fe *Frontend) handleGetMASID(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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
func (fe *Frontend) handlePostAgent(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -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}
func (fe *Frontend) handleGetAgentID(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -181,7 +176,6 @@ func (fe *Frontend) handleGetAgentID(w http.ResponseWriter, r *http.Request) {
// handleDeleteAgentID is the handler for delete requests to path
// /api/ams/mas/{masid}/agents/{agentid}
func (fe *Frontend) handleDeleteAgentID(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......
......@@ -56,7 +56,6 @@ import (
// handleGetModules is the handler for get requests to path /api/pf/modules
func (fe *Frontend) handleGetModules(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer fe.logErrors(r.URL.Path, cmapErr, httpErr)
var mods schemas.ModuleStatus
......@@ -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
func (fe *Frontend) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.MethodNotAllowed(w)
cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path)
fe.logErrors(r.URL.Path, cmapErr, httpErr)
......@@ -80,7 +78,6 @@ func (fe *Frontend) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
// resourceNotFound is the default handler for invalid paths
func (fe *Frontend) resourceNotFound(w http.ResponseWriter, r *http.Request) {
fe.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.NotFoundError(w)
cmapErr := errors.New("Resource not found")
fe.logErrors(r.URL.Path, cmapErr, httpErr)
......@@ -112,6 +109,14 @@ func getAgentID(r *http.Request) (masID int, agentID int, err error) {
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
func (fe *Frontend) server(port int) (serv *http.Server) {
r := mux.NewRouter()
......@@ -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("POST", "PUT", "POST").HandlerFunc(fe.methodNotAllowed)
s.PathPrefix("").HandlerFunc(fe.resourceNotFound)
s.Use(fe.loggingMiddleware)
r.HandleFunc("/", http.FileServer(http.Dir("./web")).ServeHTTP)
serv = &http.Server{
Addr: ":" + strconv.Itoa(port),
......
......@@ -61,7 +61,6 @@ import (
// handleAlive is the handler for requests to path /api/alive
func (logger *Logger) handleAlive(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var httpErr error
httpErr = httpreply.Alive(w, nil)
logger.logErrors(r.URL.Path, nil, httpErr)
......@@ -71,7 +70,6 @@ func (logger *Logger) handleAlive(w http.ResponseWriter, r *http.Request) {
// handlePostLogMsg is the handler for post requests to path
// /api/logging/{masid}/{agentid}/{topic}
func (logger *Logger) handlePostLogMsg(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer logger.logErrors(r.URL.Path, cmapErr, httpErr)
// create new log message entry
......@@ -95,7 +93,6 @@ func (logger *Logger) handlePostLogMsg(w http.ResponseWriter, r *http.Request) {
// handleGetCommunication is the handler for get requests to path
// /api/logging/{masid}/{agentid}/comm
func (logger *Logger) handleGetCommunication(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer logger.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -116,7 +113,6 @@ func (logger *Logger) handleGetCommunication(w http.ResponseWriter, r *http.Requ
// handlePutCommunication is the handler for put requests to path
// /api/logging/{masid}/{agentid}/comm
func (logger *Logger) handlePutCommunication(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer logger.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -144,7 +140,6 @@ func (logger *Logger) handlePutCommunication(w http.ResponseWriter, r *http.Requ
// handlePostLogMsgList is the handler for post requests to path /api/logging/{masid}/list
func (logger *Logger) handlePostLogMsgList(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer logger.logErrors(r.URL.Path, cmapErr, httpErr)
// create new log message entry
......@@ -168,7 +163,6 @@ func (logger *Logger) handlePostLogMsgList(w http.ResponseWriter, r *http.Reques
// handleGetLogsLatest is the handler for requests to path
// /api/logging/{masid}/{agentid}/{topic}/latest/{num}
func (logger *Logger) handleGetLogsLatest(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer logger.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -196,7 +190,6 @@ func (logger *Logger) handleGetLogsLatest(w http.ResponseWriter, r *http.Request
// handleGetLogsTime is the handler for get requests to path
// /api/logging/{masid}/{agentid}/{topic}/time/{start}/{end}
func (logger *Logger) handleGetLogsTime(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer logger.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -228,7 +221,6 @@ func (logger *Logger) handleGetLogsTime(w http.ResponseWriter, r *http.Request)
// handleGetState is the handler for get requests to path /api/state/{masid}/{agentid}
func (logger *Logger) handleGetState(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer logger.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -248,7 +240,6 @@ func (logger *Logger) handleGetState(w http.ResponseWriter, r *http.Request) {
// handlePutState is the handler for put requests to path /api/state/{masid}/{agentid}
func (logger *Logger) handlePutState(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer logger.logErrors(r.URL.Path, cmapErr, httpErr)
masID, agentID, cmapErr := getAgentID(r)
......@@ -275,7 +266,6 @@ func (logger *Logger) handlePutState(w http.ResponseWriter, r *http.Request) {
// handlePutStateList is the handler for requests to path /api/state/{masid}/list
func (logger *Logger) handlePutStateList(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
var cmapErr, httpErr error
defer logger.logErrors(r.URL.Path, cmapErr, httpErr)
vars := mux.Vars(r)
......@@ -303,7 +293,6 @@ func (logger *Logger) handlePutStateList(w http.ResponseWriter, r *http.Request)
// methodNotAllowed is the default handler for valid paths but invalid methods
func (logger *Logger) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.MethodNotAllowed(w)
cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path)
logger.logErrors(r.URL.Path, cmapErr, httpErr)
......@@ -312,7 +301,6 @@ func (logger *Logger) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
// resourceNotFound is the default handler for invalid paths
func (logger *Logger) resourceNotFound(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
httpErr := httpreply.NotFoundError(w)
cmapErr := errors.New("Resource not found")
logger.logErrors(r.URL.Path, cmapErr, httpErr)
......@@ -344,6 +332,14 @@ func getAgentID(r *http.Request) (masID int, agentID int, err error) {
return
}
// loggingMiddleware logs request before calling final handler
func (logger *Logger) loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.logInfo.Println("Received Request: ", r.Method, " ", r.URL.EscapedPath())
next.ServeHTTP(w, r)
})
}
// server creates the logger server
func (logger *Logger) server(port int) (serv *http.Server) {
r := mux.NewRouter()
......@@ -377,8 +373,8 @@ func (logger *Logger) server(port int) (serv *http.Server) {
s.Path("/state/{masid}/list").Methods("PUT").HandlerFunc(logger.handlePutStateList)
s.Path("/state/{masid}/list").Methods("POST", "DELETE", "GET").
HandlerFunc(logger.methodNotAllowed)
s.PathPrefix("").HandlerFunc(logger.resourceNotFound)
s.Use(logger.loggingMiddleware)
serv = &http.Server{
Addr: ":" + strconv.Itoa(port),
Handler: r,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment