2018-05-08 05:16:01 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/gorilla/mux"
|
2019-04-20 03:27:14 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"net/http"
|
2019-04-28 23:49:43 +02:00
|
|
|
"strings"
|
2019-04-20 03:27:14 +02:00
|
|
|
"sync"
|
2018-05-08 05:16:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
apiRoutes.Path("/routes").Methods("GET").
|
|
|
|
Headers("Accept", "application/json").
|
|
|
|
HandlerFunc(routesListHandler)
|
|
|
|
apiRoutes.Path("/routes").Methods("POST").
|
|
|
|
Headers("Content-Type", "application/json").
|
|
|
|
HandlerFunc(routesCreateHandler)
|
2019-04-20 03:27:14 +02:00
|
|
|
apiRoutes.Path("/defaultRoute").Methods("POST").
|
|
|
|
Headers("Content-Type", "application/json").
|
|
|
|
HandlerFunc(routesSetDefault)
|
2018-05-08 05:16:01 +02:00
|
|
|
apiRoutes.Path("/routes/{serverAddress}").Methods("DELETE").HandlerFunc(routesDeleteHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func routesListHandler(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
mappings := Routes.GetMappings()
|
|
|
|
bytes, err := json.Marshal(mappings)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to marshal mappings")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writer.Write(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func routesDeleteHandler(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
serverAddress := mux.Vars(request)["serverAddress"]
|
|
|
|
if serverAddress != "" {
|
|
|
|
if Routes.DeleteMapping(serverAddress) {
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
} else {
|
|
|
|
writer.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func routesCreateHandler(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
var definition = struct {
|
|
|
|
ServerAddress string
|
2019-04-20 03:27:14 +02:00
|
|
|
Backend string
|
2018-05-08 05:16:01 +02:00
|
|
|
}{}
|
|
|
|
|
|
|
|
defer request.Body.Close()
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(request.Body)
|
|
|
|
err := decoder.Decode(&definition)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Unable to get request body")
|
|
|
|
writer.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Routes.CreateMapping(definition.ServerAddress, definition.Backend)
|
|
|
|
writer.WriteHeader(http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
2019-04-20 03:27:14 +02:00
|
|
|
func routesSetDefault(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
var body = struct {
|
|
|
|
Backend string
|
|
|
|
}{}
|
|
|
|
|
|
|
|
defer request.Body.Close()
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(request.Body)
|
|
|
|
err := decoder.Decode(&body)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Unable to parse request")
|
|
|
|
writer.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Routes.SetDefaultRoute(body.Backend)
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2018-05-08 05:16:01 +02:00
|
|
|
type IRoutes interface {
|
|
|
|
RegisterAll(mappings map[string]string)
|
|
|
|
// FindBackendForServerAddress returns the host:port for the external server address, if registered.
|
|
|
|
// Otherwise, an empty string is returned
|
|
|
|
FindBackendForServerAddress(serverAddress string) string
|
|
|
|
GetMappings() map[string]string
|
|
|
|
DeleteMapping(serverAddress string) bool
|
|
|
|
CreateMapping(serverAddress string, backend string)
|
2019-04-20 03:27:14 +02:00
|
|
|
SetDefaultRoute(backend string)
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var Routes IRoutes = &routesImpl{}
|
|
|
|
|
2019-04-28 23:49:43 +02:00
|
|
|
func NewRoutes() IRoutes {
|
|
|
|
r := &routesImpl{
|
|
|
|
mappings: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-05-08 05:16:01 +02:00
|
|
|
func (r *routesImpl) RegisterAll(mappings map[string]string) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
r.mappings = mappings
|
|
|
|
}
|
|
|
|
|
|
|
|
type routesImpl struct {
|
|
|
|
sync.RWMutex
|
2019-04-20 03:27:14 +02:00
|
|
|
mappings map[string]string
|
|
|
|
defaultRoute string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routesImpl) SetDefaultRoute(backend string) {
|
|
|
|
r.defaultRoute = backend
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"backend": backend,
|
|
|
|
}).Info("Using default route")
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routesImpl) FindBackendForServerAddress(serverAddress string) string {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
2019-04-28 23:49:43 +02:00
|
|
|
addressParts := strings.Split(serverAddress, `\x00`)
|
|
|
|
|
2018-05-08 05:16:01 +02:00
|
|
|
if r.mappings == nil {
|
2019-04-20 03:27:14 +02:00
|
|
|
return r.defaultRoute
|
2018-05-08 05:16:01 +02:00
|
|
|
} else {
|
2019-04-20 03:27:14 +02:00
|
|
|
|
2019-04-28 23:49:43 +02:00
|
|
|
if route, exists := r.mappings[addressParts[0]]; exists {
|
2019-04-20 03:27:14 +02:00
|
|
|
return route
|
|
|
|
} else {
|
|
|
|
return r.defaultRoute
|
|
|
|
}
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routesImpl) GetMappings() map[string]string {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
|
|
|
result := make(map[string]string, len(r.mappings))
|
|
|
|
for k, v := range r.mappings {
|
|
|
|
result[k] = v
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routesImpl) DeleteMapping(serverAddress string) bool {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
logrus.WithField("serverAddress", serverAddress).Info("Deleting route")
|
|
|
|
|
|
|
|
if _, ok := r.mappings[serverAddress]; ok {
|
|
|
|
delete(r.mappings, serverAddress)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routesImpl) CreateMapping(serverAddress string, backend string) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"serverAddress": serverAddress,
|
2019-04-20 03:27:14 +02:00
|
|
|
"backend": backend,
|
2018-05-08 05:16:01 +02:00
|
|
|
}).Info("Creating route")
|
|
|
|
r.mappings[serverAddress] = backend
|
2019-04-20 03:27:14 +02:00
|
|
|
}
|