forked from kofemann/autoca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (40 loc) · 1.07 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
package main
import (
"flag"
"github.com/kofemann/autoca/ca"
"github.com/kofemann/autoca/config"
"github.com/kofemann/autoca/webca"
"log"
"net/http"
"os"
"strconv"
)
var configFile = flag.String("c", "config.yml", "path to config file")
func main() {
flag.Parse()
conf, err := config.GetConf(*configFile)
if err != nil {
log.Fatalf("Failed to read config file: %v\n", err)
}
ca := &autoca.AutoCA{}
err = ca.Init(conf.CA.CertFile, conf.CA.KeyFile, conf.CA.KeyPass, conf.CA.SerialDB)
if err != nil {
os.Exit(2)
}
webCa := &webca.WebCa{Ca: ca, Conf: conf}
http.HandleFunc("/v1/certificate", webCa.Handle)
if conf.Web.UseTls {
if conf.Web.GenerateCert {
_, err := os.Stat(conf.Web.CertFile)
if err != nil && os.IsNotExist(err) {
webCa.CreateLocalCerts(conf.Web.CertFile, conf.Web.KeyFile)
}
}
err = http.ListenAndServeTLS(":"+strconv.Itoa(conf.Web.Port), conf.Web.CertFile, conf.Web.KeyFile, nil)
} else {
err = http.ListenAndServe(":"+strconv.Itoa(conf.Web.Port), nil)
}
if err != nil {
log.Fatal("Failed to start server:", err)
}
}