2015-09-08 14:33:10 +03:00
package main
import (
2015-09-09 01:22:34 +03:00
"encoding/json"
2015-09-12 16:10:03 +03:00
"fmt"
2015-09-24 18:16:13 +03:00
"io/ioutil"
"net/http"
2015-09-24 15:32:37 +03:00
log "github.com/Sirupsen/logrus"
2015-09-11 19:47:54 +03:00
"github.com/elazarl/go-bindata-assetfs"
2015-09-12 16:10:03 +03:00
"github.com/gorilla/mux"
2015-09-08 14:33:10 +03:00
)
type WebProvider struct {
2015-09-24 15:32:37 +03:00
Address string
2015-09-22 22:00:29 +03:00
CertFile , KeyFile string
2015-09-08 14:33:10 +03:00
}
type Page struct {
2015-09-23 02:17:21 +03:00
Configurations configs
2015-09-08 14:33:10 +03:00
}
2015-09-22 07:16:21 +03:00
func ( provider * WebProvider ) Provide ( configurationChan chan <- configMessage ) {
2015-09-08 14:33:10 +03:00
systemRouter := mux . NewRouter ( )
2015-09-24 18:16:13 +03:00
systemRouter . Methods ( "GET" ) . Path ( "/" ) . Handler ( http . HandlerFunc ( GetHTMLConfigHandler ) )
2015-09-16 20:08:01 +03:00
systemRouter . Methods ( "GET" ) . Path ( "/health" ) . Handler ( http . HandlerFunc ( GetHealthHandler ) )
2015-09-15 19:35:32 +03:00
systemRouter . Methods ( "GET" ) . Path ( "/api" ) . Handler ( http . HandlerFunc ( GetConfigHandler ) )
2015-09-23 02:17:21 +03:00
systemRouter . Methods ( "GET" ) . Path ( "/api/{provider}" ) . Handler ( http . HandlerFunc ( GetConfigHandler ) )
systemRouter . Methods ( "PUT" ) . Path ( "/api/{provider}" ) . Handler ( http . HandlerFunc (
2015-09-11 19:47:54 +03:00
func ( rw http . ResponseWriter , r * http . Request ) {
2015-09-23 02:17:21 +03:00
vars := mux . Vars ( r )
if vars [ "provider" ] != "web" {
rw . WriteHeader ( http . StatusBadRequest )
fmt . Fprintf ( rw , "Only 'web' provider can be updated through the REST API" )
return
}
2015-09-09 01:22:34 +03:00
configuration := new ( Configuration )
b , _ := ioutil . ReadAll ( r . Body )
2015-09-11 19:47:54 +03:00
err := json . Unmarshal ( b , configuration )
2015-09-12 16:10:03 +03:00
if err == nil {
2015-09-22 07:16:21 +03:00
configurationChan <- configMessage { "web" , configuration }
2015-09-09 10:16:56 +03:00
GetConfigHandler ( rw , r )
2015-09-12 16:10:03 +03:00
} else {
2015-09-24 15:32:37 +03:00
log . Errorf ( "Error parsing configuration %+v" , err )
2015-09-09 10:16:56 +03:00
http . Error ( rw , fmt . Sprintf ( "%+v" , err ) , http . StatusBadRequest )
2015-09-09 01:22:34 +03:00
}
2015-09-11 19:47:54 +03:00
} ) )
2015-09-23 02:17:21 +03:00
systemRouter . Methods ( "GET" ) . Path ( "/api/{provider}/backends" ) . Handler ( http . HandlerFunc ( GetBackendsHandler ) )
systemRouter . Methods ( "GET" ) . Path ( "/api/{provider}/backends/{backend}" ) . Handler ( http . HandlerFunc ( GetBackendHandler ) )
systemRouter . Methods ( "GET" ) . Path ( "/api/{provider}/backends/{backend}/servers" ) . Handler ( http . HandlerFunc ( GetServersHandler ) )
systemRouter . Methods ( "GET" ) . Path ( "/api/{provider}/backends/{backend}/servers/{server}" ) . Handler ( http . HandlerFunc ( GetServerHandler ) )
systemRouter . Methods ( "GET" ) . Path ( "/api/{provider}/frontends" ) . Handler ( http . HandlerFunc ( GetFrontendsHandler ) )
systemRouter . Methods ( "GET" ) . Path ( "/api/{provider}/frontends/{frontend}" ) . Handler ( http . HandlerFunc ( GetFrontendHandler ) )
2015-09-15 19:35:32 +03:00
systemRouter . Methods ( "GET" ) . PathPrefix ( "/static/" ) . Handler ( http . StripPrefix ( "/static/" , http . FileServer ( & assetfs . AssetFS { Asset : Asset , AssetDir : AssetDir , Prefix : "static" } ) ) )
2015-09-08 14:33:10 +03:00
2015-09-22 22:00:29 +03:00
go func ( ) {
if len ( provider . CertFile ) > 0 && len ( provider . KeyFile ) > 0 {
2015-09-24 15:32:37 +03:00
err := http . ListenAndServeTLS ( provider . Address , provider . CertFile , provider . KeyFile , systemRouter )
if err != nil {
2015-09-24 12:39:18 +03:00
log . Fatal ( "Error creating server: " , err )
}
2015-09-22 22:00:29 +03:00
} else {
2015-09-24 12:39:18 +03:00
err := http . ListenAndServe ( provider . Address , systemRouter )
2015-09-24 15:32:37 +03:00
if err != nil {
2015-09-24 12:39:18 +03:00
log . Fatal ( "Error creating server: " , err )
}
2015-09-22 22:00:29 +03:00
}
} ( )
2015-09-08 14:33:10 +03:00
}
func GetConfigHandler ( rw http . ResponseWriter , r * http . Request ) {
2015-09-23 02:17:21 +03:00
templatesRenderer . JSON ( rw , http . StatusOK , currentConfigurations )
2015-09-08 14:33:10 +03:00
}
2015-09-24 18:16:13 +03:00
func GetHTMLConfigHandler ( response http . ResponseWriter , request * http . Request ) {
2015-09-23 02:17:21 +03:00
templatesRenderer . HTML ( response , http . StatusOK , "configuration" , Page { Configurations : currentConfigurations } )
2015-09-08 14:33:10 +03:00
}
2015-09-12 14:20:54 +03:00
2015-09-16 20:08:01 +03:00
func GetHealthHandler ( rw http . ResponseWriter , r * http . Request ) {
2015-09-12 14:20:54 +03:00
templatesRenderer . JSON ( rw , http . StatusOK , metrics . Data ( ) )
}
2015-09-15 19:35:32 +03:00
func GetBackendsHandler ( rw http . ResponseWriter , r * http . Request ) {
2015-09-23 02:17:21 +03:00
vars := mux . Vars ( r )
templatesRenderer . JSON ( rw , http . StatusOK , currentConfigurations [ vars [ "provider" ] ] . Backends )
2015-09-15 19:35:32 +03:00
}
func GetBackendHandler ( rw http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
2015-09-16 20:08:01 +03:00
id := vars [ "backend" ]
2015-09-23 02:17:21 +03:00
if backend , ok := currentConfigurations [ vars [ "provider" ] ] . Backends [ id ] ; ok {
2015-09-16 20:08:01 +03:00
templatesRenderer . JSON ( rw , http . StatusOK , backend )
2015-09-24 15:32:37 +03:00
} else {
2015-09-16 23:41:01 +03:00
http . NotFound ( rw , r )
2015-09-16 20:08:01 +03:00
}
2015-09-15 19:35:32 +03:00
}
func GetFrontendsHandler ( rw http . ResponseWriter , r * http . Request ) {
2015-09-23 02:17:21 +03:00
vars := mux . Vars ( r )
templatesRenderer . JSON ( rw , http . StatusOK , currentConfigurations [ vars [ "provider" ] ] . Frontends )
2015-09-15 19:35:32 +03:00
}
func GetFrontendHandler ( rw http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
2015-09-16 20:08:01 +03:00
id := vars [ "frontend" ]
2015-09-23 02:17:21 +03:00
if frontend , ok := currentConfigurations [ vars [ "provider" ] ] . Frontends [ id ] ; ok {
2015-09-16 20:08:01 +03:00
templatesRenderer . JSON ( rw , http . StatusOK , frontend )
2015-09-24 15:32:37 +03:00
} else {
2015-09-16 23:41:01 +03:00
http . NotFound ( rw , r )
2015-09-16 20:08:01 +03:00
}
2015-09-15 19:35:32 +03:00
}
2015-09-16 20:08:01 +03:00
func GetServersHandler ( rw http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
backend := vars [ "backend" ]
2015-09-23 02:17:21 +03:00
if backend , ok := currentConfigurations [ vars [ "provider" ] ] . Backends [ backend ] ; ok {
2015-09-16 20:08:01 +03:00
templatesRenderer . JSON ( rw , http . StatusOK , backend . Servers )
2015-09-24 15:32:37 +03:00
} else {
2015-09-16 23:41:01 +03:00
http . NotFound ( rw , r )
2015-09-16 20:08:01 +03:00
}
}
func GetServerHandler ( rw http . ResponseWriter , r * http . Request ) {
vars := mux . Vars ( r )
backend := vars [ "backend" ]
server := vars [ "server" ]
2015-09-23 02:17:21 +03:00
if backend , ok := currentConfigurations [ vars [ "provider" ] ] . Backends [ backend ] ; ok {
2015-09-16 20:08:01 +03:00
if server , ok := backend . Servers [ server ] ; ok {
templatesRenderer . JSON ( rw , http . StatusOK , server )
2015-09-24 15:32:37 +03:00
} else {
2015-09-16 23:41:01 +03:00
http . NotFound ( rw , r )
2015-09-16 20:08:01 +03:00
}
2015-09-24 15:32:37 +03:00
} else {
2015-09-16 23:41:01 +03:00
http . NotFound ( rw , r )
2015-09-16 20:08:01 +03:00
}
2015-09-22 11:33:37 +03:00
}