2024-11-26 13:35:54 +01:00
|
|
|
package apiv1
|
2023-12-19 13:56:03 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-12-23 18:22:37 +01:00
|
|
|
"git.ar21.de/yolokube/country-geo-locations/pkg/geoloc"
|
2023-12-19 13:56:03 +01:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
)
|
|
|
|
|
2024-11-26 13:35:54 +01:00
|
|
|
func NewRouter(lh *LocationHandler) chi.Router {
|
2023-12-19 13:56:03 +01:00
|
|
|
r := chi.NewRouter()
|
|
|
|
|
|
|
|
r.Use(render.SetContentType(render.ContentTypeJSON))
|
|
|
|
|
|
|
|
r.Route("/location", func(r chi.Router) {
|
|
|
|
r.Route("/{ipAddress}", func(r chi.Router) {
|
2024-11-26 13:35:54 +01:00
|
|
|
r.Use(lh.SearchIPHandler)
|
|
|
|
r.Get("/", lh.SearchIPHandlerFunc)
|
2023-12-19 13:56:03 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2024-11-26 13:35:54 +01:00
|
|
|
func (e *errResponse) Render(_ http.ResponseWriter, r *http.Request) error {
|
2023-12-19 13:56:03 +01:00
|
|
|
render.Status(r, e.HTTPStatusCode)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
const keyIPInfo key = iota
|
|
|
|
|
|
|
|
type ipInfoResponse struct {
|
|
|
|
*geoloc.IPInfo
|
|
|
|
}
|
|
|
|
|
2024-11-26 13:35:54 +01:00
|
|
|
func (ir *ipInfoResponse) Render(_ http.ResponseWriter, _ *http.Request) error {
|
2023-12-19 13:56:03 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIPInfoResponse(ipinfo *geoloc.IPInfo) *ipInfoResponse {
|
|
|
|
return &ipInfoResponse{IPInfo: ipinfo}
|
|
|
|
}
|