Compare commits

...

2 Commits

Author SHA1 Message Date
Jasper Bok 8826be4637 Add flag for passing config file 2023-08-24 21:21:52 +02:00
Jasper Bok eb172097c4 Add code for reading TOML config files 2023-08-24 21:18:25 +02:00
6 changed files with 50 additions and 7 deletions

22
config.go 100644
View File

@ -0,0 +1,22 @@
package main
import (
"github.com/BurntSushi/toml"
"os"
)
type Config struct {
Quays []Quay
}
func ReadConfigFromFile(filename string) (Config, error) {
var conf Config
contents, err := os.ReadFile(filename)
if err != nil {
return conf, err
}
_, err = toml.Decode(string(contents), &conf)
return conf, err
}

2
go.mod
View File

@ -1,3 +1,5 @@
module git.jasperbok.nl/jasperbok/busdepartures.git
go 1.20
require github.com/BurntSushi/toml v1.3.2

2
go.sum 100644
View File

@ -0,0 +1,2 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=

19
main.go
View File

@ -2,18 +2,35 @@ package main
import (
"flag"
"fmt"
"net/http"
"os"
)
var BusClient *http.Client
var Quays []Quay = []Quay{}
var Quays []Quay
func main() {
var confFilename string
var port int
flag.StringVar(&confFilename, "c", "", "Path to the configuration file")
flag.IntVar(&port, "p", 4444, "The port to start the server on")
flag.Parse()
if confFilename == "" {
fmt.Printf("No configuation file provided. Provide the path to a configuration file with the -c flag.\n")
os.Exit(0)
}
conf, err := ReadConfigFromFile(confFilename)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
Quays = conf.Quays
BusClient = &http.Client{}
serveHttp(port)
}

View File

@ -6,10 +6,10 @@ import (
)
type Quay struct {
ID string `json:"quayid"`
Name string `json:"name"`
Town string `json:"town"`
RoutesOfInterest []Route
ID string `json:"quayid" toml:"id"`
Name string `json:"name" toml:"name"`
Town string `json:"town" toml:"town"`
RoutesOfInterest []Route `toml:"routes_of_interest"`
}
func (q Quay) IsInterestingDeparture(d Departure) bool {

View File

@ -1,6 +1,6 @@
package main
type Route struct {
RouteShortName string `json:"route_short_name"`
RouteLongName string `json:"route_long_name"`
RouteShortName string `json:"route_short_name" toml:"short_name"`
RouteLongName string `json:"route_long_name" toml:"long_name"`
}