From 1b96d3aebcdd857d7dc48ee4fdf237ffae8087b6 Mon Sep 17 00:00:00 2001 From: Joao P Dubas Date: Mon, 29 Jan 2024 12:31:04 +0000 Subject: [PATCH] feat(go): implement server to parse address --- go/addresses.go | 51 ++++++++++++++ go/main.go | 23 +++++-- go/static/css/index.css | 100 ++++++++++++++++++++++++++++ go/templates/fragments/results.html | 18 +++++ go/templates/index.html | 27 ++++++++ 5 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 go/addresses.go create mode 100644 go/static/css/index.css create mode 100644 go/templates/fragments/results.html create mode 100644 go/templates/index.html diff --git a/go/addresses.go b/go/addresses.go new file mode 100644 index 0000000..9b9a663 --- /dev/null +++ b/go/addresses.go @@ -0,0 +1,51 @@ +package main + +import parser "github.com/openvenues/gopostal/parser" + +type Address struct { + Country string + State string + City string + Neighborhood string + Road string + HouseNumber string + House string + Unit string + PostalCode string +} + +func parseAddress(address string) Address { + components := parser.ParseAddress(address) + return newFromComponents(components) +} + +func newFromComponents(components []parser.ParsedComponent) Address { + address := Address{} + for _, component := range components { + switch component.Label { + case "house": + address.House = component.Value + case "house_number": + address.HouseNumber = component.Value + case "road": + address.Road = component.Value + case "unit": + address.Unit = component.Value + case "postcode": + address.PostalCode = component.Value + case "suburb": + address.Neighborhood = component.Value + case "city_district": + address.Neighborhood = component.Value + case "city": + address.City = component.Value + case "state_district": + address.Neighborhood = component.Value + case "state": + address.State = component.Value + case "country": + address.Country = component.Value + } + } + return address +} diff --git a/go/main.go b/go/main.go index 7f8d9b9..ed563b8 100644 --- a/go/main.go +++ b/go/main.go @@ -1,12 +1,25 @@ package main import ( - "fmt" - - parser "github.com/openvenues/gopostal/parser" + "html/template" + "log" + "net/http" ) func main() { - addressComponents := parser.ParseAddress("rua pedroso xavier 277, apto 51 torre 2. vila albertina, 02732-020, são paulo, sp, brasil") - fmt.Println(addressComponents) + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + tmpl := template.Must(template.ParseFiles("./templates/index.html")) + tmpl.Execute(w, nil) + }) + + http.HandleFunc("/parse", func(w http.ResponseWriter, r *http.Request) { + tmpl := template.Must(template.ParseFiles("./templates/fragments/results.html")) + data := map[string]Address{"Result": parseAddress(r.URL.Query().Get("address"))} + tmpl.Execute(w, data) + }) + + log.Println("App running on 9000...") + log.Fatal(http.ListenAndServe(":9000", nil)) } diff --git a/go/static/css/index.css b/go/static/css/index.css new file mode 100644 index 0000000..3d50d69 --- /dev/null +++ b/go/static/css/index.css @@ -0,0 +1,100 @@ +:root { + --bg-color: #131415; + --primary-color: #9575CD; + --margin: 40px; + --margin-sm: 20px; + --radius: 6px; + --text-color: #FFF; + --header-height: 100px; +} + +body { + margin: 0; + background: var(--bg-color); + color: var(--text-color); + font-family: Arial, sans-serif; +} + +ul { + margin: 0; + padding: 0; + list-style-type: none; +} + +header { + background: #24292d; + height: var(--header-height); +} + +header ul { + display: flex; +} + +header ul li { + height: var(--header-height); + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + padding: 0 var(--margin-sm); +} + +header ul li h5 { + font-size: 24px; + font-weight: 400; + margin: 0; +} + +header ul li h6 { + font-size: 14px; + font-weight: 100; + margin: 5px 0 0; +} + +header ul li h5 strong { + color: #81c784; +} + +section { + height: calc(100vh - var(--header-height)); + display: flex; + flex-direction: column; + align-items: center; +} + +section input { + height: 60px; + line-height: 60px; + background: transparent; + border: 2px solid var(--primary-color); + border-radius: var(--radius); + color: var(--text-color); + font-size: 20px; + outline: none !important; + padding: 0 20px; + width: 300px; + margin-top: var(--margin); +} + +#search-results ul li { + color: #FFF; + display: flex; +} + +#search-results ul li button { + align-self: center; + height: 34px; + padding: 0 15px; + border: 0; + border-radius: var( --radius); + margin-left: var(--margin-sm); + background: var(--primary-color); + color: #FFF; + font-weight: bold; + text-transform: uppercase; + cursor: pointer; +} + +#spinner { + display: none; +} diff --git a/go/templates/fragments/results.html b/go/templates/fragments/results.html new file mode 100644 index 0000000..4e3215b --- /dev/null +++ b/go/templates/fragments/results.html @@ -0,0 +1,18 @@ +
+
Logradouro
+
{{ .Result.Road }}
+
Número
+
{{ .Result.HouseNumber }}
+
Unidade
+
{{ .Result.Unit }}
+
Torre
+
{{ .Result.House }}
+
Bairro
+
{{ .Result.Neighborhood }}
+
Cidade
+
{{ .Result.City }}
+
Estado
+
{{ .Result.Estado }}
+
CEP
+
{{ .Result.PostalCode }}
+
diff --git a/go/templates/index.html b/go/templates/index.html new file mode 100644 index 0000000..b82a643 --- /dev/null +++ b/go/templates/index.html @@ -0,0 +1,27 @@ + + + + + + Parser de Endereço + + + + +
+
+
+ +
+ +
+
+
+ +