This repository has been archived by the owner on Apr 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (56 loc) · 1.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"github.com/emvi/logbuch"
"github.com/pirsch-analytics/hetzner-lb-acmedns/cert"
"github.com/robfig/cron/v3"
"os"
"os/signal"
)
var (
caURL, acmednsURL, hetznerAPIToken string
)
func configureLogging() {
logbuch.SetFormatter(logbuch.NewFieldFormatter("2006-01-02T15:04:05", "\t"))
logLevel := os.Getenv("HLBA_LOG_LEVEL")
if logLevel == "debug" {
logbuch.SetLevel(logbuch.LevelDebug)
} else {
logbuch.SetLevel(logbuch.LevelInfo)
}
}
func loadConfig() {
caURL = os.Getenv("HLBA_CA_URL")
acmednsURL = os.Getenv("HLBA_ACMEDNS_URL")
hetznerAPIToken = os.Getenv("HLBA_HETZNER_API_TOKEN")
if caURL == "" || acmednsURL == "" || hetznerAPIToken == "" {
logbuch.Fatal("Configuration missing. Make sure you set the HLBA_CA_URL, HLBA_ACMEDNS_URL, and HLBA_HETZNER_API_TOKEN!", logbuch.Fields{
"HLBA_CA_URL": caURL,
"HLBA_ACMEDNS_URL": acmednsURL,
"HLBA_HETZNER_API_TOKEN": hetznerAPIToken,
})
}
}
func updateCertificates() {
defer func() {
if r := recover(); r != nil {
logbuch.Error("An unexpected error occured during certificate routine", logbuch.Fields{"err": r})
}
}()
cert.UpdateCertificates(caURL, acmednsURL, hetznerAPIToken)
}
func main() {
configureLogging()
loadConfig()
// run once on startup
updateCertificates()
// schedule for once a day
c := cron.New()
if _, err := c.AddFunc("@daily", updateCertificates); err != nil {
logbuch.Fatal("Error configuring cron job", logbuch.Fields{"err": err})
}
c.Start()
sigint := make(chan os.Signal)
signal.Notify(sigint, os.Interrupt)
<-sigint
logbuch.Info("Shutting down server...")
}