Fixes
This commit is contained in:
parent
24132d531d
commit
6d5cca92ac
2 changed files with 19 additions and 10 deletions
22
main.go
22
main.go
|
@ -4,15 +4,14 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/neonxp/rutina"
|
"github.com/neonxp/rutina"
|
||||||
"golang.org/x/crypto/acme/autocert"
|
"golang.org/x/crypto/acme/autocert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Hosts []host
|
var httpSrv *http.Server
|
||||||
var mu sync.Mutex
|
var httpsSrv *http.Server
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := rutina.New(rutina.WithListenOsSignals())
|
r := rutina.New(rutina.WithListenOsSignals())
|
||||||
|
@ -21,14 +20,13 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
handler := getHandler(w)
|
handler := getHandler(w)
|
||||||
httpSrv := &http.Server{Addr: ":http", Handler: handler}
|
|
||||||
httpsSrv := &http.Server{Addr: ":https", Handler: handler}
|
|
||||||
|
|
||||||
// Docker
|
// Docker
|
||||||
r.Go(w.watch)
|
r.Go(w.watch)
|
||||||
|
|
||||||
// HTTPS
|
// HTTPS
|
||||||
r.Go(func(ctx context.Context) error {
|
r.Go(func(ctx context.Context) error {
|
||||||
|
httpsSrv = &http.Server{Addr: ":https", Handler: handler}
|
||||||
hosts := []string{}
|
hosts := []string{}
|
||||||
w.Range(func(key, value interface{}) bool {
|
w.Range(func(key, value interface{}) bool {
|
||||||
h := value.(host)
|
h := value.(host)
|
||||||
|
@ -43,24 +41,31 @@ func main() {
|
||||||
Prompt: autocert.AcceptTOS,
|
Prompt: autocert.AcceptTOS,
|
||||||
HostPolicy: autocert.HostWhitelist(hosts...),
|
HostPolicy: autocert.HostWhitelist(hosts...),
|
||||||
}
|
}
|
||||||
|
log.Println("https hosts:", hosts)
|
||||||
httpsSrv.TLSConfig = m.TLSConfig()
|
httpsSrv.TLSConfig = m.TLSConfig()
|
||||||
if err := httpsSrv.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
|
if err := httpsSrv.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}, rutina.RestartIfDone)
|
}, rutina.RestartIfDone)
|
||||||
|
|
||||||
r.Go(func(ctx context.Context) error {
|
r.Go(func(ctx context.Context) error {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
case <-w.update():
|
case <-w.update():
|
||||||
|
log.Println("reload https config")
|
||||||
}
|
}
|
||||||
tctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
if httpsSrv == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
tctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
return httpsSrv.Shutdown(tctx)
|
return httpsSrv.Shutdown(tctx)
|
||||||
}, rutina.RestartIfDone)
|
}, rutina.RestartIfDone)
|
||||||
|
|
||||||
// HTTP
|
// HTTP
|
||||||
r.Go(func(ctx context.Context) error {
|
r.Go(func(ctx context.Context) error {
|
||||||
|
httpSrv = &http.Server{Addr: ":http", Handler: handler}
|
||||||
if err := httpSrv.ListenAndServe(); err != http.ErrServerClosed {
|
if err := httpSrv.ListenAndServe(); err != http.ErrServerClosed {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -69,7 +74,10 @@ func main() {
|
||||||
r.Go(func(ctx context.Context) error {
|
r.Go(func(ctx context.Context) error {
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
log.Println("Graceful shutdown")
|
log.Println("Graceful shutdown")
|
||||||
tctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
if httpsSrv == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
tctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
return httpSrv.Shutdown(tctx)
|
return httpSrv.Shutdown(tctx)
|
||||||
})
|
})
|
||||||
|
|
|
@ -24,7 +24,8 @@ func newWatcher() (*watcher, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &watcher{
|
return &watcher{
|
||||||
cl: cl,
|
cl: cl,
|
||||||
|
upd: make(chan interface{}),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,8 +63,8 @@ func (w *watcher) watch(ctx context.Context) error {
|
||||||
newHost.Addr = nc.IPAddress
|
newHost.Addr = nc.IPAddress
|
||||||
_, loaded := w.LoadOrStore(newHost.Host, newHost)
|
_, loaded := w.LoadOrStore(newHost.Host, newHost)
|
||||||
if !loaded {
|
if !loaded {
|
||||||
log.Println("registered", newHost.Host, "->", newHost.Addr, newHost.Port)
|
log.Println("registered", newHost.Host, "->", newHost.Addr, newHost.Port, "tls:", newHost.TLS)
|
||||||
w.upd <- nil
|
w.upd <- struct{}{}
|
||||||
}
|
}
|
||||||
delete(toDelete, newHost.Host)
|
delete(toDelete, newHost.Host)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue