Correctly load .env file

Signed-off-by: Lukas Bachschwell <lukas@lbsfilm.at>
This commit is contained in:
Lukas Bachschwell 2022-02-27 22:31:40 +01:00
parent 5eca7031ec
commit c6742917d2
No known key found for this signature in database
GPG Key ID: CCC6AA87CC8DF425
2 changed files with 11 additions and 4 deletions

1
go.sum
View File

@ -724,6 +724,7 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jonboulle/clockwork v0.2.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= github.com/jonboulle/clockwork v0.2.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=

14
main.go
View File

@ -10,6 +10,7 @@ import (
"regexp" "regexp"
"github.com/99designs/httpsignatures-go" "github.com/99designs/httpsignatures-go"
"github.com/joho/godotenv"
"github.com/woodpecker-ci/woodpecker/server/model" "github.com/woodpecker-ci/woodpecker/server/model"
) )
@ -30,18 +31,23 @@ var overrideConfiguration string
func main() { func main() {
log.Println("Woodpecker central config server") log.Println("Woodpecker central config server")
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
secretToken := os.Getenv("CONFIG_SERVICE_SECRET") secretToken := os.Getenv("CONFIG_SERVICE_SECRET")
host := os.Getenv("CONFIG_SERVICE_HOST") host := os.Getenv("CONFIG_SERVICE_HOST")
filterRegex := os.Getenv("CONFIG_SERVICE_OVERRIDE_FILTER") filterRegex := os.Getenv("CONFIG_SERVICE_OVERRIDE_FILTER")
if secretToken == "" && host == "" { if secretToken == "" && host == "" {
log.Println("Please make sure CONFIG_SERVICE_HOST and CONFIG_SERVICE_SECRET are set properly") log.Fatal("Please make sure CONFIG_SERVICE_HOST and CONFIG_SERVICE_SECRET are set properly")
os.Exit(1)
} }
filter := regexp.MustCompile(filterRegex) filter := regexp.MustCompile(filterRegex)
http.HandleFunc("/ciconfig", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/ciconfig", func(w http.ResponseWriter, r *http.Request) {
log.Println("Incoming Request!")
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
return return
@ -90,8 +96,8 @@ func main() {
}) })
err := http.ListenAndServe(os.Getenv("CONFIG_SERVICE_HOST"), nil) err = http.ListenAndServe(os.Getenv("CONFIG_SERVICE_HOST"), nil)
if err != nil { if err != nil {
log.Printf("Error on listen: %v", err) log.Fatalf("Error on listen: %v", err)
} }
} }