-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
52 lines (42 loc) · 918 Bytes
/
server.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
package main
import (
"context"
"net/http"
"os"
"golang.org/x/crypto/acme/autocert"
)
type server struct {
Addr string
Handler http.Handler
CertDir string
Domains []string
}
func (s *server) serve(ctx context.Context) error {
srv := &http.Server{
Addr: s.Addr,
Handler: s.Handler,
}
if s.CertDir != "" && len(s.Domains) > 0 {
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
}
m.HostPolicy = autocert.HostWhitelist(s.Domains...)
if err := os.MkdirAll(s.CertDir, os.ModePerm); err != nil {
return err
}
m.Cache = autocert.DirCache(s.CertDir)
srv.Handler = m.HTTPHandler(nil)
crtSrv := &http.Server{
Handler: s.Handler,
}
//TODO return errors
go crtSrv.Serve(m.Listener())
defer crtSrv.Shutdown(context.Background())
}
//TODO return errors
go srv.ListenAndServe()
<-ctx.Done()
//TODO return errors
srv.Shutdown(context.Background())
return nil
}