feat(go): implement server to parse address
This commit is contained in:
parent
d355f46856
commit
1b96d3aebc
51
go/addresses.go
Normal file
51
go/addresses.go
Normal file
@ -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
|
||||
}
|
23
go/main.go
23
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))
|
||||
}
|
||||
|
100
go/static/css/index.css
Normal file
100
go/static/css/index.css
Normal file
@ -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;
|
||||
}
|
18
go/templates/fragments/results.html
Normal file
18
go/templates/fragments/results.html
Normal file
@ -0,0 +1,18 @@
|
||||
<dl>
|
||||
<dt>Logradouro</dt>
|
||||
<dd>{{ .Result.Road }}</dd>
|
||||
<dt>Número</dt>
|
||||
<dd>{{ .Result.HouseNumber }}</dd>
|
||||
<dt>Unidade</dt>
|
||||
<dd>{{ .Result.Unit }}</dd>
|
||||
<dt>Torre</dt>
|
||||
<dd>{{ .Result.House }}</dd>
|
||||
<dt>Bairro</dt>
|
||||
<dd>{{ .Result.Neighborhood }}</dd>
|
||||
<dt>Cidade</dt>
|
||||
<dd>{{ .Result.City }}</dd>
|
||||
<dt>Estado</dt>
|
||||
<dd>{{ .Result.Estado }}</dd>
|
||||
<dt><abbr title="Có de Endereçamento Postal">CEP</abbr></dt>
|
||||
<dd>{{ .Result.PostalCode }}</dd>
|
||||
</dl>
|
27
go/templates/index.html
Normal file
27
go/templates/index.html
Normal file
@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="pt-BR">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Parser de Endereço</title>
|
||||
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
|
||||
<link rel="stylesheet" href="/static/index.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<section>
|
||||
<div>
|
||||
<input
|
||||
type="search"
|
||||
name="address"
|
||||
placeholder="Endereço: Rua, Número, Complement. Bairro - Cidade/Estado. CEP"
|
||||
hx-get="/parse"
|
||||
hx-trigger="keyup changed delay:500ms"
|
||||
hx-target="#address" />
|
||||
</div>
|
||||
|
||||
<div id="address"></div>
|
||||
</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user