2016-05-14 12:31:43 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-05-14 13:21:10 +02:00
|
|
|
"encoding/json"
|
2016-05-29 21:41:58 +02:00
|
|
|
"fmt"
|
2016-05-14 13:21:10 +02:00
|
|
|
"github.com/FreifunkBremen/respond-collector/models"
|
2016-05-29 21:41:58 +02:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"net/http"
|
2016-05-14 12:31:43 +02:00
|
|
|
)
|
2016-05-14 13:21:10 +02:00
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
// GEOROUND : 7 nachkommerstellen sollten genug sein (7cm genau)
|
2016-07-09 19:14:04 +02:00
|
|
|
// http://blog.3960.org/post/7309573249/genauigkeit-bei-geo-koordinaten
|
|
|
|
const GEOROUND = 0.0000001
|
|
|
|
|
|
|
|
func geoEqual(a, b float64) bool {
|
|
|
|
if (a-b) < GEOROUND && (b-a) < GEOROUND {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
// AliasesAPI struct for API
|
|
|
|
type AliasesAPI struct {
|
2016-05-14 13:21:10 +02:00
|
|
|
aliases *models.Aliases
|
2016-05-29 21:41:58 +02:00
|
|
|
config *models.Config
|
|
|
|
nodes *models.Nodes
|
2016-05-14 12:31:43 +02:00
|
|
|
}
|
2016-05-14 13:21:10 +02:00
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
// NewAliases Bind to API
|
2016-05-29 21:41:58 +02:00
|
|
|
func NewAliases(config *models.Config, router *httprouter.Router, prefix string, nodes *models.Nodes) {
|
2017-01-20 22:27:44 +01:00
|
|
|
api := &AliasesAPI{
|
2016-05-14 13:21:10 +02:00
|
|
|
aliases: models.NewAliases(config),
|
2016-05-29 21:41:58 +02:00
|
|
|
nodes: nodes,
|
|
|
|
config: config,
|
2016-05-14 13:21:10 +02:00
|
|
|
}
|
|
|
|
router.GET(prefix, api.GetAll)
|
2017-01-20 22:27:44 +01:00
|
|
|
router.GET(prefix+"/ansible", api.Ansible)
|
2016-05-29 21:41:58 +02:00
|
|
|
router.GET(prefix+"/alias/:nodeid", api.GetOne)
|
2017-01-20 22:27:44 +01:00
|
|
|
router.POST(prefix+"/alias/:nodeid", BasicAuth(api.SaveOne, []byte(config.Webserver.API.Passphrase)))
|
2016-05-16 12:24:50 +02:00
|
|
|
}
|
2016-05-29 21:41:58 +02:00
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
// GetAll request for get all aliases
|
|
|
|
func (api *AliasesAPI) GetAll(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2016-05-29 21:41:58 +02:00
|
|
|
jsonOutput(w, r, api.aliases.List)
|
2016-05-14 12:31:43 +02:00
|
|
|
}
|
2016-05-14 13:21:10 +02:00
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
// GetOne request for get one alias
|
|
|
|
func (api *AliasesAPI) GetOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
2016-05-29 21:41:58 +02:00
|
|
|
if alias := api.aliases.List[ps.ByName("nodeid")]; alias != nil {
|
|
|
|
jsonOutput(w, r, alias)
|
2016-05-14 13:21:10 +02:00
|
|
|
return
|
|
|
|
}
|
2016-05-29 21:41:58 +02:00
|
|
|
fmt.Fprint(w, "Not found: ", ps.ByName("nodeid"), "\n")
|
2016-05-14 12:31:43 +02:00
|
|
|
}
|
2016-05-14 13:21:10 +02:00
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
// SaveOne request for save a alias
|
|
|
|
func (api *AliasesAPI) SaveOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
2016-05-14 13:21:10 +02:00
|
|
|
var alias models.Alias
|
|
|
|
|
2016-05-16 12:24:50 +02:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&alias)
|
2016-05-29 21:41:58 +02:00
|
|
|
if err != nil {
|
2016-05-14 13:21:10 +02:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2016-05-29 21:41:58 +02:00
|
|
|
fmt.Fprint(w, "Decode: ", ps.ByName("nodeid"), "\n")
|
2016-05-14 13:21:10 +02:00
|
|
|
return
|
|
|
|
}
|
2016-05-29 21:41:58 +02:00
|
|
|
api.aliases.Update(ps.ByName("nodeid"), &alias)
|
2016-07-09 19:14:04 +02:00
|
|
|
fmt.Print("[api] node updated '", ps.ByName("nodeid"), "'\n")
|
2016-05-29 21:41:58 +02:00
|
|
|
jsonOutput(w, r, alias)
|
2016-05-14 12:31:43 +02:00
|
|
|
}
|
2017-01-20 22:27:44 +01:00
|
|
|
|
|
|
|
// Ansible json output
|
|
|
|
func (api *AliasesAPI) Ansible(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2016-07-09 19:14:04 +02:00
|
|
|
fmt.Print("[api] ansible\n")
|
2016-05-29 21:41:58 +02:00
|
|
|
jsonOutput(w, r, models.GenerateAnsible(api.nodes, api.aliases.List))
|
2016-05-14 12:31:43 +02:00
|
|
|
}
|