country-geo-locations/api/v1/server.go
Tom Neuber 8215e1f13a
All checks were successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/deploy Pipeline was successful
refactor: restructure project
2024-11-26 13:35:54 +01:00

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}
}