Author: Adam Evyčędo <git@apiote.xyz>
change auth format in config
config/config.go | 43 +++++++++--------------------------- go.mod | 2 go.sum | 4 +- traffic/berlin_vbb.go | 2 traffic/brussels_stib_mivb.go | 4 +- traffic/convert.go | 2 traffic/feeds.go | 2 traffic/gzm_ztm.go | 2 traffic/poznan_ztm.go | 2
diff --git a/config/config.go b/config/config.go index ef2135954b60161a45530ed7b40a2f3be84e8ca8..0bd4d287632e25e58867f13877e1b9c5c689c67e 100644 --- a/config/config.go +++ b/config/config.go @@ -1,24 +1,24 @@ package config import ( - "bufio" "os" - "strings" + "github.com/BurntSushi/toml" "notabug.org/apiote/gott" ) // TODO change to dirty -type BrusselsConfig struct { - ApiKey string +type Auth struct { + ApiKey string + KeyHeader string } type Config struct { FeedsPath string EnabledFeeds []string ListenAddress string - Brussels BrusselsConfig + Auth map[string]Auth } type result struct { @@ -34,40 +34,18 @@ args.configFile = configFile return gott.Tuple{args}, err } -func scan(input ...interface{}) interface{} { +func scan(input ...interface{}) (interface{}, error) { args := input[0].(result) configFile := args.configFile config := args.config + decoder := toml.NewDecoder(configFile) + _, err := decoder.Decode(&config) - scanner := bufio.NewScanner(configFile) - for scanner.Scan() { - line := scanner.Text() - if line == "" || line[0] == '#' { - continue - } - assignment := strings.Split(line, "=") - key := strings.Trim(assignment[0], " ") - value := strings.Trim(assignment[1], " ") - if key == "feeds_path" { - config.FeedsPath = strings.ReplaceAll(value, "\"", "") - } - if key == "enabled_feeds" { - value = strings.Replace(strings.Replace(value, "[", "", 1), "]", "", 1) - elements := strings.Split(value, ",") - for _, e := range elements { - e = strings.ReplaceAll(strings.Trim(e, " "), "\"", "") - config.EnabledFeeds = append(config.EnabledFeeds, e) - } - } - if key == "listen_address" { - config.ListenAddress = strings.ReplaceAll(value, "\"", "") - } - } args.config = config configFile.Close() - return gott.Tuple{args} + return gott.Tuple{args}, err } func Read(configPath string) (Config, error) { @@ -78,12 +56,13 @@ config: Config{ EnabledFeeds: []string{}, FeedsPath: "/var/lib/szczanieckiej", ListenAddress: ":51354", + Auth: map[string]Auth{}, }, }, } output, err := gott.NewResult(input). Bind(openFile). - Map(scan). + Bind(scan). Finish() if err != nil { return Config{}, err diff --git a/go.mod b/go.mod index 5176471d0d5f2492c33aa5c1224b28281a1f10d0..8f06c3af0e49087fc8af1474abecfd67e0133639 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,10 @@ require ( apiote.xyz/p/gott/v2 v2.0.3 git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9 + github.com/BurntSushi/toml v1.3.2 github.com/adrg/strutil v0.3.0 github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9 github.com/dhconnelly/rtreego v1.1.0 - github.com/golang/protobuf v1.5.2 github.com/sahilm/fuzzy v0.1.0 github.com/ulikunitz/xz v0.5.10 github.com/yuin/gopher-lua v1.1.1 diff --git a/go.sum b/go.sum index 9b3adca23b170eda0f3d1a9b6c31a277809d86d0..fcec52967dceef6e728603771128098c073365ed 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ apiote.xyz/p/gott/v2 v2.0.3/go.mod h1:H87aFMqvof1DWBzJuxzLaQRby4+PrIvFRwMfTTB6lK8= git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw= git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9 h1:Ahny8Ud1LjVMMAlt8utUFKhhxJtwBAualvsbc/Sk7cE= git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9/go.mod h1:BVJwbDfVjCjoFiKrhkei6NdGcZYpkDkdyCdg1ukytRA= +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/adrg/strutil v0.3.0 h1:bi/HB2zQbDihC8lxvATDTDzkT4bG7PATtVnDYp5rvq4= github.com/adrg/strutil v0.3.0/go.mod h1:Jz0wzBVE6Uiy9wxo62YEqEY1Nwto3QlLl1Il5gkLKWU= github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9 h1:rdWOzitWlNYeUsXmz+IQfa9NkGEq3gA/qQ3mOEqBU6o= @@ -13,8 +15,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dhconnelly/rtreego v1.1.0 h1:ejMaqN03N1s6Bdg6peGkNgBnYYSBHzcK8yhSPCB+rHE= github.com/dhconnelly/rtreego v1.1.0/go.mod h1:SDozu0Fjy17XH1svEXJgdYq8Tah6Zjfa/4Q33Z80+KM= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= diff --git a/traffic/berlin_vbb.go b/traffic/berlin_vbb.go index 908820e76bd0a3b6fbfcbf19814c1557edf2fa8e..0c8f6ac6cd00c9fce3459cb229e7be5f18b86f2d 100644 --- a/traffic/berlin_vbb.go +++ b/traffic/berlin_vbb.go @@ -49,7 +49,7 @@ func (z VbbBerlin) RealtimeFeeds() map[RealtimeFeedType]string { return map[RealtimeFeedType]string{} } -func (z VbbBerlin) LuaUpdatesScript(cfg config.Config) string { +func (z VbbBerlin) LuaUpdatesScript(config.Auth) string { return ` function getUpdates(tripID, sequence) local http = require("http") diff --git a/traffic/brussels_stib_mivb.go b/traffic/brussels_stib_mivb.go index 5807f0bbd93a5a4b2cf6e56b828167dc722324f9..87d17092228fbbda97513539c5317805abcf8705 100644 --- a/traffic/brussels_stib_mivb.go +++ b/traffic/brussels_stib_mivb.go @@ -59,14 +59,14 @@ func (z StibMivbBrussels) RealtimeFeeds() map[RealtimeFeedType]string { return map[RealtimeFeedType]string{} } -func (z StibMivbBrussels) LuaUpdatesScript(cfg config.Config) string { +func (z StibMivbBrussels) LuaUpdatesScript(auth config.Auth) string { return ` function getUpdates(tripID, sequence, stopID) local http = require("http") local json = require("json") response, error_message = http.get("https://stibmivb.opendatasoft.com/api/explore/v2.1/catalog/datasets/waiting-time-rt-production/records", { - query="apikey=` + cfg.Brussels.ApiKey + `&where=pointid%20%3D%20%22" .. stopID .. "%22&limit=20", + query="apikey=` + auth.ApiKey + `&where=pointid%20%3D%20%22" .. stopID .. "%22&limit=20", timeout="30s" }) diff --git a/traffic/convert.go b/traffic/convert.go index ca7846c72fbb54e846d9a095e9f1fbd6af52fce0..bd44f5a25d5c2ca2e0686bebad081d5cc2264ecb 100644 --- a/traffic/convert.go +++ b/traffic/convert.go @@ -1383,7 +1383,7 @@ return fmt.Errorf("while creating file: %w", err) } defer updatesFile.Close() - script := c.Feed.LuaUpdatesScript(c.config) + script := c.Feed.LuaUpdatesScript(c.config.Auth[c.Feed.String()]) if script == "" { return nil } diff --git a/traffic/feeds.go b/traffic/feeds.go index e69e05d5d25656e3d4e333c94e8822ccf1b74d6d..2fff8174fb578beebb06caf581e83a4af55fb9cd 100644 --- a/traffic/feeds.go +++ b/traffic/feeds.go @@ -18,7 +18,7 @@ fmt.Stringer ConvertVehicles() ([]Vehicle, error) GetVersions(time.Time, *time.Location) ([]Version, error) RealtimeFeeds() map[RealtimeFeedType]string - LuaUpdatesScript(config.Config) string + LuaUpdatesScript(config.Auth) string Transformer() transform.Transformer Name() string Flags() FeedFlags diff --git a/traffic/gzm_ztm.go b/traffic/gzm_ztm.go index f5c2d315092da3e594f1536c60a0e79e37464650..9259c7a153c048601fc37c933c5a5c861b377f0c 100644 --- a/traffic/gzm_ztm.go +++ b/traffic/gzm_ztm.go @@ -68,7 +68,7 @@ } // return map[RealtimeFeedType]string{} } -func (z GzmZtm) LuaUpdatesScript(c config.Config) string { +func (z GzmZtm) LuaUpdatesScript(config.Auth) string { return "" } diff --git a/traffic/poznan_ztm.go b/traffic/poznan_ztm.go index b316b53da0b84fd440cd3f3f61dcc315e076ffce..0f027d2b9fbd269a767926f59d0fd26687dbaeab 100644 --- a/traffic/poznan_ztm.go +++ b/traffic/poznan_ztm.go @@ -141,7 +141,7 @@ VEHICLE_POSITIONS: "https://ztm.poznan.pl/en/dla-deweloperow/getGtfsRtFile/?file=feeds.pb", } } -func (z ZtmPoznan) LuaUpdatesScript(c config.Config) string { +func (z ZtmPoznan) LuaUpdatesScript(config.Auth) string { return "" }