48 lines
956 B
Go
48 lines
956 B
Go
package controllers
|
|
|
|
import (
|
|
"addrss/pkg/router"
|
|
"fmt"
|
|
|
|
expand "github.com/openvenues/gopostal/expand"
|
|
parser "github.com/openvenues/gopostal/parser"
|
|
)
|
|
|
|
type Api struct{}
|
|
|
|
type ParseRequest struct {
|
|
Address string `json:"address"`
|
|
}
|
|
|
|
func (a Api) AddRoutes() {
|
|
router.AddPost("/v1/expand", expandAddress).Anonymous()
|
|
router.AddPost("/v1/parse", parseAddress).Anonymous()
|
|
}
|
|
|
|
func expandAddress(ctx *router.Context) {
|
|
expansions := expand.ExpandAddress("1080 Brayden Ct. Hebron KY 41048")
|
|
for i := 0; i < len(expansions); i++ {
|
|
fmt.Println(expansions[i])
|
|
}
|
|
|
|
ctx.Response.NoContent()
|
|
}
|
|
|
|
func parseAddress(ctx *router.Context) {
|
|
pr := ParseRequest{}
|
|
if err := ctx.Request.Bind(&pr); err != nil {
|
|
ctx.Response.BadRequest(err)
|
|
}
|
|
|
|
options := parser.ParserOptions{}
|
|
|
|
pa := parser.ParseAddressOptions(pr.Address, options)
|
|
addr := map[string]any{}
|
|
|
|
for i := 0; i < len(pa); i++ {
|
|
addr[pa[i].Label] = pa[i].Value
|
|
}
|
|
|
|
ctx.Response.OK(addr)
|
|
}
|