handler.go 5.76 KB
Newer Older
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
/*
Copyright 2020 Institute for Automation of Complex Power Systems,
E.ON Energy Research Center, RWTH Aachen University

This project is licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.

Apache License, Version 2.0:

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package frontend

import (
	"errors"
	"net/http"
50
	"strconv"
51
52

	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/common/httpreply"
53
54
	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/schemas"
	"github.com/gorilla/mux"
55
56
)

57
58
// handleGetModules is the handler for get requests to path /api/pf/modules
func (fe *Frontend) handleGetModules(w http.ResponseWriter, r *http.Request) {
59
	var cmapErr, httpErr error
60
61
62
63
	var mods schemas.ModuleStatus
	mods, cmapErr = fe.getModuleStatus()
	if cmapErr != nil {
		httpErr = httpreply.CMAPError(w, cmapErr.Error())
Stefan Dähling's avatar
Stefan Dähling committed
64
		fe.logErrors(r.URL.Path, cmapErr, httpErr)
65
		return
66
	}
67
	httpErr = httpreply.Resource(w, mods, cmapErr)
Stefan Dähling's avatar
Stefan Dähling committed
68
	fe.logErrors(r.URL.Path, cmapErr, httpErr)
69
70
	return
}
71

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// methodNotAllowed is the default handler for valid paths but invalid methods
func (fe *Frontend) methodNotAllowed(w http.ResponseWriter, r *http.Request) {
	httpErr := httpreply.MethodNotAllowed(w)
	cmapErr := errors.New("Error: Method not allowed on path " + r.URL.Path)
	fe.logErrors(r.URL.Path, cmapErr, httpErr)
	return
}

// resourceNotFound is the default handler for invalid paths
func (fe *Frontend) resourceNotFound(w http.ResponseWriter, r *http.Request) {
	httpErr := httpreply.NotFoundError(w)
	cmapErr := errors.New("Resource not found")
	fe.logErrors(r.URL.Path, cmapErr, httpErr)
	return
}

// logErrors logs errors if any
func (fe *Frontend) logErrors(path string, cmapErr error, httpErr error) {
90
	if cmapErr != nil {
91
		fe.logError.Println(path, cmapErr)
92
93
	}
	if httpErr != nil {
94
		fe.logError.Println(path, httpErr)
95
	}
96
	return
97
98
}

99
100
101
102
103
104
105
106
107
108
// getAgentID returns the masID and agentID from the path
func getAgentID(r *http.Request) (masID int, agentID int, err error) {
	vars := mux.Vars(r)
	masID, err = strconv.Atoi(vars["masid"])
	if err != nil {
		return
	}
	agentID, err = strconv.Atoi(vars["agentid"])
	if err != nil {
		return
Stefan Dähling's avatar
Stefan Dähling committed
109
110
111
112
	}
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
113
114
115
116
117
118
119
120
// 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)
	})
}

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// server creates the fe server
func (fe *Frontend) server(port int) (serv *http.Server) {
	r := mux.NewRouter()
	s := r.PathPrefix("/api").Subrouter()
	s.Path("/overview").Methods("GET").HandlerFunc(fe.handleGetMASs)
	s.Path("/overview").Methods("POST").HandlerFunc(fe.handlePostMAS)
	s.Path("/overview").Methods("PUT", "DELETE").HandlerFunc(fe.methodNotAllowed)
	s.Path("/ams/mas").Methods("GET").HandlerFunc(fe.handleGetMASs)
	s.Path("/ams/mas").Methods("POST").HandlerFunc(fe.handlePostMAS)
	s.Path("/ams/mas").Methods("PUT", "DELETE").HandlerFunc(fe.methodNotAllowed)
	s.Path("/ams/mas/{masid}").Methods("GET").HandlerFunc(fe.handleGetMASID)
	s.Path("/ams/mas/{masid}").Methods("DELETE").HandlerFunc(fe.handleDeleteMASID)
	s.Path("/ams/mas/{masid}").Methods("PUT", "POST").HandlerFunc(fe.methodNotAllowed)
	s.Path("/ams/mas/{masid}/agents").Methods("POST").HandlerFunc(fe.handlePostAgent)
	s.Path("/ams/mas/{masid}/agents").Methods("PUT", "GET", "DELETE").HandlerFunc(fe.methodNotAllowed)
	s.Path("/ams/mas/{masid}/agents/{agentid}").Methods("GET").HandlerFunc(fe.handleGetAgentID)
	s.Path("/ams/mas/{masid}/agents/{agentid}").Methods("DELETE").
		HandlerFunc(fe.handleDeleteAgentID)
	s.Path("/ams/mas/{masid}/agents/{agentid}").Methods("PUT", "POST").
		HandlerFunc(fe.methodNotAllowed)
	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)
Stefan Dähling's avatar
Stefan Dähling committed
144
	s.Use(fe.loggingMiddleware)
145
146
147
148
	r.HandleFunc("/", http.FileServer(http.Dir("./web")).ServeHTTP)
	serv = &http.Server{
		Addr:    ":" + strconv.Itoa(port),
		Handler: r,
149
	}
150
151
152
153
154
155
156
	return
}

// listen opens a http server listening and serving request
func (fe *Frontend) listen(serv *http.Server) (err error) {
	fe.logInfo.Println("Frontend listening on " + serv.Addr)
	err = serv.ListenAndServe()
157
158
	return
}