Browse Source

Add viper as configmanagment

Signed-off-by: Alexander Karl <akarl@darknebu.la>
master
Alexander Karl 3 years ago
parent
commit
e93fcbc383
  1. 9
      api/influx.go
  2. 3
      api/tankapi.go
  3. 15
      main.go
  4. 6
      struct/config.go
  5. 5
      struct/endpoint.go
  6. 8
      struct/influx.go

9
api/influx.go

@ -2,6 +2,7 @@ package api
import (
"github.com/influxdata/influxdb/client/v2"
"github.com/spf13/viper"
"log"
"time"
)
@ -10,9 +11,9 @@ func InsertInflux(pName string, pE5Price float64, pE10Price float64, pDieselPric
// Create a new HTTPClient
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
Username: "tankapi",
Password: "TankAPI123!._",
Addr: viper.GetString("Database.Url"),
Username: viper.GetString("Database.Username"),
Password: viper.GetString("Database.Password"),
})
if err != nil {
@ -22,7 +23,7 @@ func InsertInflux(pName string, pE5Price float64, pE10Price float64, pDieselPric
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: "tankapi",
Database: viper.GetString("Database.Database"),
Precision: "s",
})
if err != nil {

3
api/tankapi.go

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"git.darknebu.la/akarl/tank-api/struct"
"github.com/spf13/viper"
"io/ioutil"
"log"
"net/http"
@ -13,7 +14,7 @@ import (
func GetPrices() {
fmt.Println("Cron runs now!")
url := "https://creativecommons.tankerkoenig.de/json/list.php?lat=51.6648467&lng=6.6090983&rad=10&sort=dist&type=all&apikey=090270da-7e5c-444a-e6b5-83a122f7e20c"
url := fmt.Sprintf("https://creativecommons.tankerkoenig.de/json/list.php?lat=51.6648467&lng=6.6090983&rad=10&sort=dist&type=all&apikey=%s", viper.GetString("Endpoint.Api"))
tankApiClient := http.Client{
Timeout: time.Second * 2,

15
main.go

@ -3,11 +3,26 @@ package main
import (
"fmt"
"git.darknebu.la/akarl/tank-api/api"
"git.darknebu.la/akarl/tank-api/struct"
"github.com/jasonlvhit/gocron"
"github.com/spf13/viper"
"log"
)
func main() {
viper.SetConfigName("config")
viper.AddConfigPath(".")
var configuration structure.Configuration
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
}
err := viper.Unmarshal(&configuration)
if err != nil {
log.Fatalf("unable to decode into struct, %v", err)
}
gocron.Every(1).Minute().Do(api.GetPrices)
_, time := gocron.NextRun()

6
struct/config.go

@ -0,0 +1,6 @@
package structure
type Configuration struct {
Endpoint EndpointConfiguration
Database DatabaseConfiguration
}

5
struct/endpoint.go

@ -0,0 +1,5 @@
package structure
type EndpointConfiguration struct {
Apikey string
}

8
struct/influx.go

@ -0,0 +1,8 @@
package structure
type DatabaseConfiguration struct {
User string
Password string
Database string
Url string
}
Loading…
Cancel
Save