first try of graph.json

This commit is contained in:
root 2016-05-16 12:24:50 +02:00
parent 12c0fd4b70
commit 311352a4ca
3 changed files with 46 additions and 8 deletions

View File

@ -22,10 +22,13 @@ func NewAliases (config *models.Config, router *httprouter.Router,prefix string,
}
router.GET(prefix, api.GetAll)
router.GET(prefix+"/ansible", api.AnsibleDiff)
router.GET(prefix+"/alias/:nodeid", api.GetOne)
router.POST(prefix+"/alias/:nodeid", api.SaveOne)
router.GET(prefix+"/cleanup", api.Cleanup)
router.GET(prefix+"/alias/:nodeid", BasicAuth(api.GetOne, []byte(config.Webserver.Api.Passphrase)))
router.POST(prefix+"/alias/:nodeid", BasicAuth(api.SaveOne,[]byte(config.Webserver.Api.Passphrase)))
}
func (api *ApiAliases) cleaner(){
// clean up the aliases by correct values in nodes
}
func (api *ApiAliases) GetAll(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
jsonOutput(w,api.aliases.List)
}
@ -40,8 +43,8 @@ func (api *ApiAliases) GetOne(w http.ResponseWriter, r *http.Request, ps httprou
func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var alias models.Alias
err := json.NewDecoder(r.Body).Decode(&alias)
err := json.NewDecoder(r.Body).Decode(&alias)
if err != nil{
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Fprint(w, "Decode: ", ps.ByName("nodeid"),"\n")
@ -51,8 +54,11 @@ func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httpro
jsonOutput(w,alias)
}
func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
diff := api.aliases.List
//TODO diff between List and api.nodes (for run not at all)
jsonOutput(w,models.GenerateAnsible(api.nodes,diff))
func (api *ApiAliases) Cleanup(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
api.cleaner()
jsonOutput(w,api.aliases.List)
}
func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
api.cleaner()
jsonOutput(w,models.GenerateAnsible(api.nodes,api.aliases.List))
}

View File

@ -1,8 +1,13 @@
package api
import (
"bytes"
"strings"
"net/http"
"encoding/json"
"encoding/base64"
"github.com/julienschmidt/httprouter"
)
func jsonOutput(w http.ResponseWriter,data interface{}){
@ -15,3 +20,29 @@ func jsonOutput(w http.ResponseWriter,data interface{}){
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func BasicAuth(h httprouter.Handle, pass []byte) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
const basicAuthPrefix string = "Basic "
// Get the Basic Authentication credentials
auth := r.Header.Get("Authorization")
if strings.HasPrefix(auth, basicAuthPrefix) {
// Check credentials
payload, err := base64.StdEncoding.DecodeString(auth[len(basicAuthPrefix):])
if err == nil {
pair := bytes.SplitN(payload, []byte(":"), 2)
if len(pair) == 2 &&
bytes.Equal(pair[1], pass) {
// Delegate request to the given handle
h(w, r, ps)
return
}
}
}
// Request Basic Authentication otherwise
w.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
}

View File

@ -21,6 +21,7 @@ type Config struct {
Address string `yaml:"address"`
Webroot string `yaml:"webroot"`
Api struct {
Passphrase string `yaml:"passphrase"`
NewNode bool `yaml:"newnode"`
Aliases bool `yaml:"aliases"`
} `yaml:"api"`