-
Notifications
You must be signed in to change notification settings - Fork 0
/
certcal.go
59 lines (49 loc) · 1.34 KB
/
certcal.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
package main
import (
"fmt"
"github.com/alecthomas/kong"
"github.com/mrtazz/certcal/handler"
"github.com/mrtazz/certcal/hosts"
log "github.com/sirupsen/logrus"
"net/http"
"time"
)
var (
version = ""
goversion = ""
)
// CLI defines the command line arguments
var CLI struct {
Serve struct {
Hosts []string `required:"" help:"hosts to check certs for." env:"CERTCAL_HOSTS"`
Interval string `required:"" help:"interval in which to check certs" env:"CERTCAL_INTERVAL" default:"24h"`
Port int `help:"port for the server to listen on" env:"PORT" default:"3000"`
} `cmd:"" help:"run the server."`
}
func main() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
logger := log.WithFields(log.Fields{
"package": "main",
})
ctx := kong.Parse(&CLI)
switch ctx.Command() {
case "serve":
duration, err := time.ParseDuration(CLI.Serve.Interval)
if err != nil {
logger.Error("unable to parse CERTCAL_INTERVAL, defaulting to 1 day")
duration = 24 * time.Hour
}
hosts.AddHosts(CLI.Serve.Hosts)
hosts.UpdateEvery(duration)
address := fmt.Sprintf(":%d", CLI.Serve.Port)
logger.WithFields(log.Fields{
"address": address,
}).Info("starting web server")
http.HandleFunc("/hosts", handler.Handler)
http.ListenAndServe(address, nil)
default:
logger.Error("Unknown command: " + ctx.Command())
}
}