Skip to content

Commit

Permalink
- update most recent spec document
Browse files Browse the repository at this point in the history
- refactor static configuration
- filter parking by origin
- add bike parking
  • Loading branch information
clezag committed Jun 6, 2024
1 parent 13c7c84 commit 2f53ac6
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 84 deletions.
Binary file removed documentation/240207_PianificazioneSviluppi.pdf
Binary file not shown.
Binary file not shown.
Binary file added documentation/240521_PianificazioneSviluppi.pdf
Binary file not shown.
7 changes: 3 additions & 4 deletions infrastructure/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ EXPOSE 8080
FROM base as build
WORKDIR app
COPY src/main main
COPY src/resources/ resources/
COPY src/config/ config/
CMD [ "./main"]

FROM base as dev
WORKDIR /code
# Air for auto rebuild on changes
RUN go install github.com/cosmtrek/air@latest
RUN air init
CMD ["air"]
RUN go install github.com/air-verse/air@latest
CMD ["air", "-c", ".air.toml"]
55 changes: 55 additions & 0 deletions src/.air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# SPDX-FileCopyrightText: 2024 NOI Techpark <[email protected]>
#
# SPDX-License-Identifier: CC0-1.0

root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html", "yml"]
include_file = ["config/datasets.yml"]
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[proxy]
app_port = 0
enabled = false
proxy_port = 0

[screen]
clear_on_rebuild = false
keep_scroll = true
12 changes: 12 additions & 0 deletions src/config/datasets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: 2024 NOI Techpark <[email protected]>
#
# SPDX-License-Identifier: CC0-1.0

parking:
origins:
- FBK
- A22
- Municipality Merano
#- skidata not implemented necessary metadata yet
- FAMAS
- bicincitta
103 changes: 103 additions & 0 deletions src/config/operators.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# SPDX-FileCopyrightText: 2024 NOI Techpark <[email protected]>
#
# SPDX-License-Identifier: CC0-1.0

- origin: BIKE_SHARING_BOLZANO
email: [email protected]
phone: 0471997111
url: https://opencity.comune.bolzano.it/Servizi/Bike-Sharing-Bici-Bolzano
street: Vicolo Gumer 7
town: Bolzano
postcode: 39100
country: Italy

- origin: BIKE_SHARING_MERANO
email: [email protected]
phone: 0473250111
url: https://www.comune.merano.bz.it/it/Bikesharing
street: Via Portici 192
town: Merano
postcode: 39012
country: Italy

- origin: BIKE_SHARING_PAPIN
email: [email protected]
phone: 0474913450
url: https://www.papinsport.com/rent-a-bike/
street: Via Freising 9
town: San Candido
postcode: 39038
country: Italy

- origin: HAL-API
email: [email protected]
phone: 0471061319
url: https://www.carsharing.bz.it/it/
street: Via Beda Weber 1
town: Bolzano
postcode: 39100
country: Italy

- origin: FAMAS
email: [email protected]
phone: 0471997111
url: https://opencity.comune.bolzano.it/Documenti-e-dati/Documenti-tecnici-di-supporto/Parcheggiare-in-citta
street: Vicolo Gumer 7
town: Bolzano
postcode: 39100
country: Italy

- origin: Municipality Merano
email: [email protected]
phone: 0473250111
url: https://www.comune.merano.bz.it/it/Parcheggi
street: Via Portici 192
town: Merano
postcode: 39012
country: Italy

- origin: FBK-Trento
email: [email protected]
phone: 0461884111
url: https://www.comune.trento.it/Citta/Come-orientarsi/Parcheggi
street: Via Belenzani 19
town: Trento
postcode: 38122
country: Italy

- origin: FBK-Rovereto
email: [email protected]
phone: 0464452111
url: https://www.comune.rovereto.tn.it/Servizi/Sosta-e-parcheggi
street: Piazza Podestà 11
town: Rovereto
postcode: 38068
country: Italy

- origin: A22
email: [email protected]
phone: 0461212611
url: https://www.autobrennero.it/it/in-viaggio/sosta-e-servizi/aree-di-servizio/
street: Via Berlino 10
town: Trento
postcode: 38121
country: Italy

- origin: skidata
email: [email protected]
phone: 0471312888
url: https://www.sta.bz.it
street: Via dei Conciapelli 60
town: Bolzano
postcode: 39100
country: Italy

- origin: bicincitta
email: [email protected]
phone: 0471312888
url: https://www.sta.bz.it
street: Via dei Conciapelli 60
town: Bolzano
postcode: 39100
country: Italy

3 changes: 3 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/xml"
"log/slog"
"net/http"
"opendatahub/sta-nap-export/netex"
nParking "opendatahub/sta-nap-export/netex/parking"
nSharing "opendatahub/sta-nap-export/netex/sharing"
"opendatahub/sta-nap-export/siri"
Expand All @@ -19,6 +20,8 @@ import (

func main() {
InitLogger()
netex.InitConfig()

r := gin.New()

if os.Getenv("GIN_LOG") == "PRETTY" {
Expand Down
78 changes: 78 additions & 0 deletions src/netex/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-FileCopyrightText: 2024 NOI Techpark <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package netex

import (
"log"
"os"
"path/filepath"
"runtime"
"testing"

"gopkg.in/yaml.v3"
)

type Config struct {
operators []operatorCfg
dataset datasetCfg
}

var Cfg Config

type datasetCfg struct {
Parking struct {
Origins []string
}
}

type operatorCfg struct {
Origin string
Email string
Phone string
Url string
Street string
Town string
Postcode string
Country string
}

func InitConfig() {
Cfg = *ReadConfig()
}
func ReadConfig() *Config {
cfg := Config{}
readYaml(fixRelPath("config", "operators.yml"), &cfg.operators)
readYaml(fixRelPath("config", "datasets.yml"), &cfg.dataset)
return &cfg
}

func readYaml(path string, o any) {
f, err := os.ReadFile(path)
if err != nil {
log.Panicln("Cannot open config", path, err)
}

if err := yaml.Unmarshal(f, o); err != nil {
log.Panicln("Cannot unmarshal Operators config", path, err)
}
}

func fixRelPath(path ...string) string {
cwd, _ := os.Getwd()
// https://stackoverflow.com/questions/31873396/is-it-possible-to-get-the-current-root-of-package-structure-as-a-string-in-golan
// Relative paths are a pain in the butt with unit tests because they always execute from the module they are in
// This is a hack to always start from root folder and compose the full "absolute" path
if testing.Testing() {
_, b, _, _ := runtime.Caller(0)
root := filepath.Join(filepath.Dir(b), "../..")
cwd = filepath.Join(root, "src")
}

return filepath.Join(append([]string{cwd}, path...)...)
}

func (c *Config) ParkingOrigins() []string {
return c.dataset.Parking.Origins
}
61 changes: 5 additions & 56 deletions src/netex/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,23 @@ package netex

import (
"log"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/gocarina/gocsv"
"golang.org/x/exp/maps"
)

type operatorCfg struct {
Origin string `csv:"origin"`
Email string `csv:"email"`
Phone string `csv:"phone"`
Url string `csv:"url"`
Street string `csv:"street"`
Town string `csv:"town"`
Postcode string `csv:"postcode"`
Country string `csv:"country"`
}

func readOps(path string) []operatorCfg {
f, err := os.Open(path)
if err != nil {
wd, _ := os.Getwd()
log.Panicln("Cannot open Operators csv.", wd, err)
}
defer f.Close()

ops := []operatorCfg{}
if err := gocsv.UnmarshalFile(f, &ops); err != nil {
log.Panic("Cannot unmarshal Operators csv", err)
}
return ops
}

var ops []operatorCfg

func getCsvPath() string {
// https://stackoverflow.com/questions/31873396/is-it-possible-to-get-the-current-root-of-package-structure-as-a-string-in-golan
// Relative paths are a pain in the butt with unit tests because they always execute from the module they are in
// This is a hack to always start from root folder and compose the full "absolute" path
if testing.Testing() {
_, b, _, _ := runtime.Caller(0)
root := filepath.Join(filepath.Dir(b), "../..")
return filepath.Join(root, "src", "resources", "operators.csv")
}

cwd, _ := os.Getwd()
return filepath.Join(cwd, "resources", "operators.csv")
}

func mapByOrigin(p []operatorCfg) map[string]operatorCfg {
ret := make(map[string]operatorCfg)
for _, o := range p {
ret[o.Origin] = o
}
return ret
}
func opsByOrigin() map[string]operatorCfg {
if ops == nil {
ops = readOps(getCsvPath())
}
return mapByOrigin(ops)
}

func GetOperator(id string) Operator {
cfg, found := opsByOrigin()[id]
func (c *Config) GetOperator(id string) Operator {
mapped := mapByOrigin(c.operators)
cfg, found := mapped[id]
if !found {
log.Panic("Unable to map operator. Probably got some origin that we shouldn't have?")
log.Panicln("Unable to map operator. Probably got some origin that we shouldn't have?", id, maps.Keys(mapped))
}

o := Operator{}
Expand Down
12 changes: 2 additions & 10 deletions src/netex/operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,9 @@ import (
"gotest.tools/v3/assert"
)

func TestReadOps(t *testing.T) {
ops := readOps(getCsvPath())
if len(ops) == 0 {
t.Log("Operators loaded empty")
t.Fail()
}
}

func TestOpsContent(t *testing.T) {
ops := readOps(getCsvPath())
mapped := mapByOrigin(ops)
cfg := ReadConfig()
mapped := mapByOrigin(cfg.operators)

bsb := mapped["BIKE_SHARING_BOLZANO"]
assert.Equal(t, "[email protected]", bsb.Email)
Expand Down
Loading

0 comments on commit 2f53ac6

Please sign in to comment.