Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api definition for Lookup and Register responses #7

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions api/v0/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package v0

import (
v2 "github.com/m-lab/locate/api/v2"
"github.com/m-lab/uuid-annotator/annotator"
)

// LookupResponse is returned by a lookup request.
type LookupResponse struct {
Error *v2.Error `json:",omitempty"`
Lookup *Lookup `json:",omitempty"`
}

// Lookup is returned for a successful lookup request.
type Lookup struct {
IATA string
}

// RegisterResponse is returned by a register request.
type RegisterResponse struct {
Error *v2.Error `json:",omitempty"`
Registration *Registration `json:",omitempty"`
}

// Network contains IPv4 and IPv6 addresses.
type Network struct {
IPv4 string
IPv6 string
}

// ServerAnnotation is used by the uuid-annotator.
// From: https://github.com/m-lab/uuid-annotator/blob/main/siteannotator/server.go#L83-L90
type ServerAnnotation struct {
Annotation annotator.ServerAnnotations
Network Network
Type string
}

// Registration is returned for a successful registration request.
type Registration struct {
// Hostname is the dynamic DNS name. Hostname should be available immediately.
Hostname string

// Annotation is the metadata used by the uuid-annotator for all server annotations.
Annotation *ServerAnnotation `json:",omitempty"`
// Heartbeat is the registration message used by the heartbeat service to register with the Locate API.
Heartbeat *v2.Registration `json:",omitempty"`
}
47 changes: 40 additions & 7 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package handler

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"

v0 "github.com/m-lab/autojoin/api/v0"
"github.com/m-lab/go/rtx"
v2 "github.com/m-lab/locate/api/v2"
)

// Server maintains shared state for the server.
Expand Down Expand Up @@ -35,27 +40,46 @@ func (s *Server) Reload(ctx context.Context) {

// Lookup is a handler used to find the nearest IATA given client IP or lat/lon metadata.
func (s *Server) Lookup(rw http.ResponseWriter, req *http.Request) {
resp := v0.LookupResponse{}
country := rawCountry(req)
if country == "" {
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(rw, "could not determine country")
resp.Error = &v2.Error{
Type: "?country=<country>",
Title: "could not determine country from request",
Status: http.StatusBadRequest,
}
rw.WriteHeader(resp.Error.Status)
writeResponse(rw, resp)
return
}
rlat, rlon := rawLatLon(req)
lat, errLat := strconv.ParseFloat(rlat, 64)
lon, errLon := strconv.ParseFloat(rlon, 64)
if errLat != nil || errLon != nil {
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(rw, "could not determine lat/lon")
resp.Error = &v2.Error{
Type: "?lat=<lat>&lon=<lon>",
Title: "could not determine lat/lon from request",
Status: http.StatusBadRequest,
}
rw.WriteHeader(resp.Error.Status)
writeResponse(rw, resp)
return
}
code, err := s.Iata.Lookup(country, lat, lon)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(rw, "could not determine iata code: %v", err)
resp.Error = &v2.Error{
Type: "internal error",
Title: "could not determine iata from request",
Status: http.StatusInternalServerError,
}
rw.WriteHeader(resp.Error.Status)
writeResponse(rw, resp)
return
}
fmt.Fprintf(rw, "%s\n", code)
resp.Lookup = &v0.Lookup{
IATA: code,
}
writeResponse(rw, resp)
}

// Register is a handler used by autonodes to register with M-Lab on startup.
Expand Down Expand Up @@ -104,3 +128,12 @@ func rawLatLon(req *http.Request) (string, string) {
// TODO: lookup with request IP.
return "", ""
}

func writeResponse(rw http.ResponseWriter, resp interface{}) error {
b, err := json.MarshalIndent(resp, "", " ")
// NOTE: marshal can only fail on incompatible types, like functions. The
// panic will be caught by the http server handler.
rtx.PanicOnError(err, "failed to marshal response")
rw.Write(b)
return nil
}
12 changes: 8 additions & 4 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package handler

import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"

v0 "github.com/m-lab/autojoin/api/v0"
"github.com/m-lab/go/testingx"
)

type fakeIataFinder struct {
Expand Down Expand Up @@ -102,9 +105,10 @@ func TestServer_Lookup(t *testing.T) {
if rw.Code != tt.wantCode {
t.Errorf("Lookup() returned wrong code; got %d, want %d", rw.Code, tt.wantCode)
}
resp := strings.Trim(rw.Body.String(), "\n")
if rw.Code == http.StatusOK && resp != tt.wantIata {
t.Errorf("Lookup() returned wrong iata; got %s, want %s", resp, tt.wantIata)
resp := &v0.LookupResponse{}
testingx.Must(t, json.Unmarshal(rw.Body.Bytes(), resp), "failed to parse response")
if rw.Code == http.StatusOK && (resp.Lookup == nil || resp.Lookup.IATA != tt.wantIata) {
t.Errorf("Lookup() returned wrong iata; got %#v, want %s", resp, tt.wantIata)
}
})
}
Expand Down