Compare commits

..

No commits in common. "fab15ce226cd9ed4c734b0f4ce881289b05280a7" and "4dc8d11538f009941c434a0c7eda26098ccbb792" have entirely different histories.

View File

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