whatismyip/server/tls.go

48 lines
990 B
Go
Raw Normal View History

2023-03-20 15:33:45 +00:00
package server
import (
"context"
"errors"
"log"
"net/http"
"github.com/dcarrillo/whatismyip/internal/setting"
)
2024-03-23 16:41:34 +00:00
type TLS struct {
2023-03-20 15:33:45 +00:00
server *http.Server
handler *http.Handler
ctx context.Context
}
2024-03-23 16:41:34 +00:00
func NewTLSServer(ctx context.Context, handler *http.Handler) *TLS {
return &TLS{
2023-03-20 15:33:45 +00:00
handler: handler,
ctx: ctx,
}
}
2024-03-23 16:41:34 +00:00
func (t *TLS) Start() {
2023-03-20 15:33:45 +00:00
t.server = &http.Server{
Addr: setting.App.TLSAddress,
Handler: *t.handler,
ReadTimeout: setting.App.Server.ReadTimeout,
WriteTimeout: setting.App.Server.WriteTimeout,
}
log.Printf("Starting TLS server listening on %s", setting.App.TLSAddress)
go func() {
if err := t.server.ListenAndServeTLS(setting.App.TLSCrtPath, setting.App.TLSKeyPath); err != nil &&
!errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()
}
2024-03-23 16:41:34 +00:00
func (t *TLS) Stop() {
log.Printf("Stopping TLS server...")
2023-03-20 15:33:45 +00:00
if err := t.server.Shutdown(t.ctx); err != nil {
log.Printf("TLS server forced to shutdown: %s", err)
}
}