-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_api.go
53 lines (45 loc) · 1.27 KB
/
web_api.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
package main
import (
"crypto/tls"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/acme/autocert"
"net/http"
"strconv"
)
func setupWebApi(cnf *config) {
if cnf.Logging.Prod {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
registerRoutes(cnf, r)
if cnf.Server.UseManualCert {
var x509 tls.Certificate
x509, err := tls.LoadX509KeyPair(cnf.Server.ManualFullChain, cnf.Server.ManualPrivate)
if err != nil {
errorLogger.Fatalln(err)
}
server := &http.Server{
Addr: ":" + strconv.Itoa(cnf.Server.Port),
Handler: r,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{ x509 },
},
}
go errorLogger.Fatalln(server.ListenAndServe())
} else if cnf.Server.UseAutoCert {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(cnf.Server.Url),
Cache: autocert.DirCache(cnf.Server.CertPath),
}
server := &http.Server{
Addr: ":" + strconv.Itoa(cnf.Server.Port),
Handler: r,
TLSConfig: certManager.TLSConfig(),
}
go errorLogger.Fatalln(server.ListenAndServeTLS("", ""))
// go errorLogger.Fatalln(http.l(":"+strconv.Itoa(cnf.Server.Port), certManager.HTTPHandler(server.Handler)))
} else {
go errorLogger.Fatalln(r.Run(":" + strconv.Itoa(cnf.Server.Port)))
}
}