first try of graph.json
This commit is contained in:
parent
12c0fd4b70
commit
311352a4ca
@ -22,10 +22,13 @@ func NewAliases (config *models.Config, router *httprouter.Router,prefix string,
|
|||||||
}
|
}
|
||||||
router.GET(prefix, api.GetAll)
|
router.GET(prefix, api.GetAll)
|
||||||
router.GET(prefix+"/ansible", api.AnsibleDiff)
|
router.GET(prefix+"/ansible", api.AnsibleDiff)
|
||||||
router.GET(prefix+"/alias/:nodeid", api.GetOne)
|
router.GET(prefix+"/cleanup", api.Cleanup)
|
||||||
router.POST(prefix+"/alias/:nodeid", api.SaveOne)
|
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) {
|
func (api *ApiAliases) GetAll(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
jsonOutput(w,api.aliases.List)
|
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) {
|
func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
var alias models.Alias
|
var alias models.Alias
|
||||||
err := json.NewDecoder(r.Body).Decode(&alias)
|
|
||||||
|
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&alias)
|
||||||
if err != nil{
|
if err != nil{
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
fmt.Fprint(w, "Decode: ", ps.ByName("nodeid"),"\n")
|
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)
|
jsonOutput(w,alias)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
func (api *ApiAliases) Cleanup(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
diff := api.aliases.List
|
api.cleaner()
|
||||||
//TODO diff between List and api.nodes (for run not at all)
|
jsonOutput(w,api.aliases.List)
|
||||||
jsonOutput(w,models.GenerateAnsible(api.nodes,diff))
|
}
|
||||||
|
func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
|
api.cleaner()
|
||||||
|
jsonOutput(w,models.GenerateAnsible(api.nodes,api.aliases.List))
|
||||||
}
|
}
|
||||||
|
31
api/lib.go
31
api/lib.go
@ -1,8 +1,13 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
"net/http"
|
"net/http"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"encoding/base64"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
func jsonOutput(w http.ResponseWriter,data interface{}){
|
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.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(js)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -21,6 +21,7 @@ type Config struct {
|
|||||||
Address string `yaml:"address"`
|
Address string `yaml:"address"`
|
||||||
Webroot string `yaml:"webroot"`
|
Webroot string `yaml:"webroot"`
|
||||||
Api struct {
|
Api struct {
|
||||||
|
Passphrase string `yaml:"passphrase"`
|
||||||
NewNode bool `yaml:"newnode"`
|
NewNode bool `yaml:"newnode"`
|
||||||
Aliases bool `yaml:"aliases"`
|
Aliases bool `yaml:"aliases"`
|
||||||
} `yaml:"api"`
|
} `yaml:"api"`
|
||||||
|
Loading…
Reference in New Issue
Block a user