forked from fbl/keyserver
Compare commits
2 Commits
4dc8d11538
...
fab15ce226
Author | SHA1 | Date |
---|---|---|
Johannes Kimmel | fab15ce226 | |
Johannes Kimmel | 9f21834290 |
|
@ -2,6 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -13,10 +15,7 @@ import (
|
||||||
geo "github.com/kellydunn/golang-geo"
|
geo "github.com/kellydunn/golang-geo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var hoodDir string = "/home/fbl/Desktop/keyserver/hoods"
|
func hoodVoronoi(hoods []Hood, lat, long float64) *Hood {
|
||||||
var hoods []Hood
|
|
||||||
|
|
||||||
func hoodVoronoi(lat, long float64) *Hood {
|
|
||||||
var best *Hood
|
var best *Hood
|
||||||
var bestDist float64 = math.MaxFloat64
|
var bestDist float64 = math.MaxFloat64
|
||||||
|
|
||||||
|
@ -37,7 +36,7 @@ func hoodVoronoi(lat, long float64) *Hood {
|
||||||
return best
|
return best
|
||||||
}
|
}
|
||||||
|
|
||||||
func hoodPoly(lat, long float64) *Hood {
|
func hoodPoly(hoods []Hood, lat, long float64) *Hood {
|
||||||
var matches []*Hood
|
var matches []*Hood
|
||||||
var best *Hood
|
var best *Hood
|
||||||
|
|
||||||
|
@ -77,7 +76,7 @@ func hoodPoly(lat, long float64) *Hood {
|
||||||
return best
|
return best
|
||||||
}
|
}
|
||||||
|
|
||||||
func hoodId(id uint64) *Hood {
|
func hoodId(hoods []Hood, id uint64) *Hood {
|
||||||
for _, hood := range hoods {
|
for _, hood := range hoods {
|
||||||
if hood.HoodInfo.Id == id {
|
if hood.HoodInfo.Id == id {
|
||||||
return &hood
|
return &hood
|
||||||
|
@ -87,13 +86,17 @@ func hoodId(id uint64) *Hood {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func keyserverV2Time(w http.ResponseWriter, r *http.Request) {
|
func logRequestDuration(h http.Handler) http.Handler {
|
||||||
start := time.Now()
|
return http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
keyserverV2(w, r)
|
h.ServeHTTP(w, r)
|
||||||
|
|
||||||
duration := time.Since(start)
|
duration := time.Since(start)
|
||||||
log.Printf("Processed request in %s", duration)
|
log.Printf("Processed request in %s", duration)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpHeader(w http.ResponseWriter, r *http.Request, statusCode int) {
|
func httpHeader(w http.ResponseWriter, r *http.Request, statusCode int) {
|
||||||
|
@ -101,91 +104,95 @@ func httpHeader(w http.ResponseWriter, r *http.Request, statusCode int) {
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func keyserverV2(w http.ResponseWriter, r *http.Request) {
|
func keyserverV2Handler(hoods []Hood) http.Handler {
|
||||||
hoodid := r.URL.Query().Get("hoodid")
|
return http.HandlerFunc(
|
||||||
lat := r.URL.Query().Get("lat")
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
long := r.URL.Query().Get("long")
|
hoodid := r.URL.Query().Get("hoodid")
|
||||||
lon := r.URL.Query().Get("lon")
|
lat := r.URL.Query().Get("lat")
|
||||||
var hood *Hood = hoodId(0)
|
long := r.URL.Query().Get("long")
|
||||||
|
lon := r.URL.Query().Get("lon")
|
||||||
|
var hood *Hood = hoodId(hoods, 0)
|
||||||
|
|
||||||
if lon != "" && long != "" {
|
if lon != "" && long != "" {
|
||||||
httpHeader(w, r, http.StatusBadRequest)
|
httpHeader(w, r, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if lon != "" {
|
if lon != "" {
|
||||||
long = lon
|
long = lon
|
||||||
}
|
}
|
||||||
|
|
||||||
if hoodid != "" {
|
if hoodid != "" {
|
||||||
id, err := strconv.ParseUint(hoodid, 10, 64)
|
id, err := strconv.ParseUint(hoodid, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpHeader(w, r, http.StatusBadRequest)
|
httpHeader(w, r, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hood = hoodId(id)
|
hood = hoodId(hoods, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
if lat != "" {
|
if lat != "" {
|
||||||
if long == "" {
|
if long == "" {
|
||||||
httpHeader(w, r, http.StatusBadRequest)
|
httpHeader(w, r, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse numbers
|
// parse numbers
|
||||||
lat := strings.Replace(lat, ",", ".", -1)
|
lat := strings.Replace(lat, ",", ".", -1)
|
||||||
long := strings.Replace(long, ",", ".", -1)
|
long := strings.Replace(long, ",", ".", -1)
|
||||||
|
|
||||||
latF, err := strconv.ParseFloat(lat, 64)
|
latF, err := strconv.ParseFloat(lat, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpHeader(w, r, http.StatusBadRequest)
|
httpHeader(w, r, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
longF, err := strconv.ParseFloat(long, 64)
|
longF, err := strconv.ParseFloat(long, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpHeader(w, r, http.StatusBadRequest)
|
httpHeader(w, r, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hoodP := hoodPoly(latF, longF)
|
hoodP := hoodPoly(hoods, latF, longF)
|
||||||
hood = hoodVoronoi(latF, longF)
|
hood = hoodVoronoi(hoods, latF, longF)
|
||||||
|
|
||||||
if hoodP != nil {
|
if hoodP != nil {
|
||||||
hood = hoodP
|
hood = hoodP
|
||||||
}
|
}
|
||||||
|
|
||||||
if hood == nil {
|
if hood == nil {
|
||||||
httpHeader(w, r, http.StatusInternalServerError)
|
httpHeader(w, r, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else if long != "" {
|
} else if long != "" {
|
||||||
httpHeader(w, r, http.StatusBadRequest)
|
httpHeader(w, r, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if hood == nil {
|
if hood == nil {
|
||||||
log.Print("No hood found")
|
log.Print("No hood found")
|
||||||
httpHeader(w, r, http.StatusNotFound)
|
httpHeader(w, r, http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := json.MarshalIndent(hood, "", " ")
|
b, err := json.MarshalIndent(hood, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Marshaling error: %s", err)
|
log.Printf("Marshaling error: %s", err)
|
||||||
httpHeader(w, r, http.StatusInternalServerError)
|
httpHeader(w, r, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Add("Content-Type", "application/json; charset=utf-8")
|
w.Header().Add("Content-Type", "application/json; charset=utf-8")
|
||||||
w.Write(b)
|
w.Write(b)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseHoods() {
|
func parseHoods(hoodDir string) ([]Hood, error) {
|
||||||
var newHoods []Hood
|
var newHoods []Hood
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
items, err := os.ReadDir(hoodDir)
|
items, err := os.ReadDir(hoodDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf(`Error opening dir ("%s"): %s`, hoodDir, err)
|
return nil, fmt.Errorf(`Error opening dir ("%s"): %w`, hoodDir, err)
|
||||||
}
|
}
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
var tmp Hood
|
var tmp Hood
|
||||||
|
@ -211,17 +218,31 @@ func parseHoods() {
|
||||||
newHoods = append(newHoods, tmp)
|
newHoods = append(newHoods, tmp)
|
||||||
}
|
}
|
||||||
|
|
||||||
hoods = newHoods
|
return newHoods, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func blank(w http.ResponseWriter, r *http.Request) {
|
func blank(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func run() error {
|
||||||
parseHoods()
|
hoodDir := flag.String("hoods", "./hoods", "Directory of hood json files")
|
||||||
|
addr := flag.String("addr", ":8080", "HTTP listen addr")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
http.HandleFunc("/v2/", keyserverV2Time)
|
hoods, err := parseHoods(*hoodDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Handle("/v2/", logRequestDuration(keyserverV2Handler(hoods)))
|
||||||
http.HandleFunc("/v2/hoods.php", blank)
|
http.HandleFunc("/v2/hoods.php", blank)
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Printf("Starting HTTP Server on http://%s", *addr)
|
||||||
|
return http.ListenAndServe(*addr, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := run(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue