54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package apiv1
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.ar21.de/yolokube/country-geo-locations/pkg/geoloc"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/render"
|
|
)
|
|
|
|
func NewRouter(lh *LocationHandler) chi.Router {
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(render.SetContentType(render.ContentTypeJSON))
|
|
|
|
r.Route("/location", func(r chi.Router) {
|
|
r.Route("/{ipAddress}", func(r chi.Router) {
|
|
r.Use(lh.SearchIPHandler)
|
|
r.Get("/", lh.SearchIPHandlerFunc)
|
|
})
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
type errResponse struct {
|
|
Err error `json:"-"`
|
|
HTTPStatusCode int `json:"-"`
|
|
|
|
StatusText string `json:"status"`
|
|
AppCode int64 `json:"code,omitempty"`
|
|
ErrorText string `json:"error,omitempty"`
|
|
}
|
|
|
|
func (e *errResponse) Render(_ http.ResponseWriter, r *http.Request) error {
|
|
render.Status(r, e.HTTPStatusCode)
|
|
return nil
|
|
}
|
|
|
|
type key int
|
|
|
|
const keyIPInfo key = iota
|
|
|
|
type ipInfoResponse struct {
|
|
*geoloc.IPInfo
|
|
}
|
|
|
|
func (ir *ipInfoResponse) Render(_ http.ResponseWriter, _ *http.Request) error {
|
|
return nil
|
|
}
|
|
|
|
func newIPInfoResponse(ipinfo *geoloc.IPInfo) *ipInfoResponse {
|
|
return &ipInfoResponse{IPInfo: ipinfo}
|
|
}
|