drone-yaml-server/main.go

76 lines
1.6 KiB
Go
Raw Normal View History

2021-08-08 10:37:18 +02:00
// Copyright 2019 the Drone Authors. All rights reserved.
// Use of this source code is governed by the Blue Oak Model License
// that can be found in the LICENSE file.
package main
import (
"net/http"
2021-08-14 18:36:10 +02:00
"time"
2021-08-08 10:37:18 +02:00
"git.entr0py.de/garionion/drone-yaml-server/plugin"
"github.com/drone/drone-go/plugin/config"
_ "github.com/joho/godotenv/autoload"
"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
)
// spec provides the plugin settings.
type spec struct {
Bind string `envconfig:"DRONE_BIND"`
Debug bool `envconfig:"DRONE_DEBUG"`
Secret string `envconfig:"DRONE_SECRET"`
2021-08-14 18:36:10 +02:00
Repo string `envconfig:"DRONE_YAML_REPO"`
Branch string `envconfig:"DRONE_BRANCH"`
Token string `envconfig:"DRONE_TOKEN"`
FetchFrequency time.Duration `envconfig:"DRONE_FETCH_FREQUENCY"`
2021-08-08 10:37:18 +02:00
}
func main() {
spec := new(spec)
err := envconfig.Process("", spec)
if err != nil {
logrus.Fatal(err)
}
if spec.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
if spec.Secret == "" {
logrus.Fatalln("missing secret key")
}
if spec.Bind == "" {
spec.Bind = ":3000"
}
2021-08-14 18:36:10 +02:00
if spec.Repo == "" {
logrus.Fatalln("missing repo")
}
if spec.Branch == "" {
spec.Branch = "main"
}
if spec.FetchFrequency == 0 {
spec.FetchFrequency = 10 * time.Minute
}
2021-08-08 10:37:18 +02:00
handler := config.Handler(
plugin.New(
2021-08-14 18:36:10 +02:00
spec.Repo,
spec.Branch,
spec.Token,
spec.FetchFrequency,
2021-08-08 10:37:18 +02:00
),
spec.Secret,
logrus.StandardLogger(),
)
logrus.Infof("server listening on address %s", spec.Bind)
http.Handle("/", handler)
logrus.Fatal(http.ListenAndServe(spec.Bind, nil))
}