Skip to content

Commit

Permalink
restart HTTPS server on certificate renewal
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Sep 28, 2023
1 parent 970dea3 commit c15c5f4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 17 deletions.
90 changes: 73 additions & 17 deletions fed/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"fmt"
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/cfg"
"github.com/fsnotify/fsnotify"
"log/slog"
"net"
"net/http"
"sync"
"time"
)

Expand Down Expand Up @@ -74,27 +76,81 @@ func ListenAndServe(ctx context.Context, db *sql.DB, resolver *Resolver, actor *
return err
}

server := http.Server{
Addr: addr,
Handler: mux,
ErrorLog: slog.NewLogLogger(log.Handler(), slog.Level(cfg.LogLevel)),
BaseContext: func(net.Listener) context.Context {
return ctx
},
ReadTimeout: time.Second * 30,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
w, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer w.Close()

go func() {
<-ctx.Done()
server.Shutdown(context.Background())
}()

if err := server.ListenAndServeTLS(cert, key); err != nil && !errors.Is(err, http.ErrServerClosed) {
if err := w.Add(cert); err != nil {
return err
}
if err := w.Add(key); err != nil {
return err
}

for ctx.Err() == nil {
var wg sync.WaitGroup
serverCtx, stopServer := context.WithCancel(ctx)

server := http.Server{
Addr: addr,
Handler: mux,
ErrorLog: slog.NewLogLogger(log.Handler(), slog.Level(cfg.LogLevel)),
BaseContext: func(net.Listener) context.Context {
return serverCtx
},
ReadTimeout: time.Second * 30,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}

wg.Add(1)
go func() {
<-serverCtx.Done()
server.Shutdown(context.Background())
wg.Done()
}()

wg.Add(1)
go func() {
defer wg.Done()

for {
select {
case <-serverCtx.Done():
server.Shutdown(context.Background())
return

case event, ok := <-w.Events:
if !ok {
continue
}

if !event.Has(fsnotify.Write) && !event.Has(fsnotify.Create) && !event.Has(fsnotify.Rename) {
continue
}

log.Info("Stopping HTTPS server: file has changed", "name", event.Name)
server.Shutdown(context.Background())
return

case <-w.Errors:
}
}
}()

log.Info("Starting HTTPS server")
err := server.ListenAndServeTLS(cert, key)

stopServer()
wg.Wait()

if err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
}

return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/dimkr/tootik
go 1.21

require (
github.com/fsnotify/fsnotify v1.6.0
github.com/go-fed/httpsig v1.1.0
github.com/mattn/go-sqlite3 v1.14.17
github.com/stretchr/testify v1.8.4
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI=
github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
Expand All @@ -17,6 +19,7 @@ golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
Expand Down

0 comments on commit c15c5f4

Please sign in to comment.