1
0
Fork 0

keyserver: rework file to not depend on globals

This commit is contained in:
Johannes Kimmel 2024-03-12 02:50:44 +01:00
parent 4dc8d11538
commit 9f21834290
1 changed files with 98 additions and 81 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"log"
"math"
"net/http"
@ -14,9 +15,8 @@ import (
)
var hoodDir string = "/home/fbl/Desktop/keyserver/hoods"
var hoods []Hood
func hoodVoronoi(lat, long float64) *Hood {
func hoodVoronoi(hoods []Hood, lat, long float64) *Hood {
var best *Hood
var bestDist float64 = math.MaxFloat64
@ -37,7 +37,7 @@ func hoodVoronoi(lat, long float64) *Hood {
return best
}
func hoodPoly(lat, long float64) *Hood {
func hoodPoly(hoods []Hood, lat, long float64) *Hood {
var matches []*Hood
var best *Hood
@ -77,7 +77,7 @@ func hoodPoly(lat, long float64) *Hood {
return best
}
func hoodId(id uint64) *Hood {
func hoodId(hoods []Hood, id uint64) *Hood {
for _, hood := range hoods {
if hood.HoodInfo.Id == id {
return &hood
@ -87,13 +87,17 @@ func hoodId(id uint64) *Hood {
return nil
}
func keyserverV2Time(w http.ResponseWriter, r *http.Request) {
start := time.Now()
func logRequestDuration(h http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
keyserverV2(w, r)
h.ServeHTTP(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) {
@ -101,91 +105,95 @@ func httpHeader(w http.ResponseWriter, r *http.Request, statusCode int) {
w.WriteHeader(statusCode)
}
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)
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)
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(id)
}
if hoodid != "" {
id, err := strconv.ParseUint(hoodid, 10, 64)
if err != nil {
httpHeader(w, r, http.StatusBadRequest)
return
}
hood = hoodId(hoods, 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(latF, longF)
hood = hoodVoronoi(latF, longF)
hoodP := hoodPoly(hoods, latF, longF)
hood = hoodVoronoi(hoods, 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() {
func parseHoods() ([]Hood, error) {
var newHoods []Hood
var err error
items, err := os.ReadDir(hoodDir)
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 {
var tmp Hood
@ -211,17 +219,26 @@ func parseHoods() {
newHoods = append(newHoods, tmp)
}
hoods = newHoods
return newHoods, nil
}
func blank(w http.ResponseWriter, r *http.Request) {
}
func main() {
parseHoods()
func run() error {
hoods, err := parseHoods()
if err != nil {
return err
}
http.HandleFunc("/v2/", keyserverV2Time)
http.Handle("/v2/", logRequestDuration(keyserverV2Handler(hoods)))
http.HandleFunc("/v2/hoods.php", blank)
log.Fatal(http.ListenAndServe(":8080", nil))
return http.ListenAndServe(":8080", nil)
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}