Make keepalive period configurable
This commit is contained in:
parent
7993d0eef8
commit
909eeff406
|
|
@ -329,7 +329,7 @@ func TestMultipleClients(t *testing.T) {
|
|||
userIDs := []uint{1, 1, 1, 2, 2, 3}
|
||||
i := 0
|
||||
server, api := bootTestServer(func(context *gin.Context) {
|
||||
auth.RegisterAuthentication(context, nil, userIDs[i], "t"+string(userIDs[i]))
|
||||
auth.RegisterAuthentication(context, nil, userIDs[i], "t"+fmt.Sprint(userIDs[i]))
|
||||
i++
|
||||
})
|
||||
defer server.Close()
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
# Save it to `config.yml` when edited
|
||||
|
||||
server:
|
||||
keepaliveperiodseconds: 0 # 0 = use system default; set the interval in which keepalive packages will be sent. Only change this value if you know what you are doing.
|
||||
listenaddr: "" # the address to bind on, leave empty to bind on all addresses
|
||||
port: 80 # the port the HTTP server will listen on
|
||||
|
||||
|
|
|
|||
|
|
@ -11,9 +11,11 @@ import (
|
|||
// Configuration is stuff that can be configured externally per env variables or config file (config.yml).
|
||||
type Configuration struct {
|
||||
Server struct {
|
||||
ListenAddr string `default:""`
|
||||
Port int `default:"80"`
|
||||
SSL struct {
|
||||
KeepAlivePeriodSeconds int
|
||||
ListenAddr string `default:""`
|
||||
Port int `default:"80"`
|
||||
|
||||
SSL struct {
|
||||
Enabled *bool `default:"false"`
|
||||
RedirectToHTTPS *bool `default:"true"`
|
||||
ListenAddr string `default:""`
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
package runner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gotify/server/v2/config"
|
||||
|
|
@ -18,7 +21,7 @@ func Run(engine *gin.Engine, conf *config.Configuration) {
|
|||
|
||||
if *conf.Server.SSL.Enabled {
|
||||
if *conf.Server.SSL.RedirectToHTTPS {
|
||||
httpHandler = redirectToHTTPS(string(conf.Server.SSL.Port))
|
||||
httpHandler = redirectToHTTPS(strconv.Itoa(conf.Server.SSL.Port))
|
||||
}
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", conf.Server.SSL.ListenAddr, conf.Server.SSL.Port)
|
||||
|
|
@ -38,12 +41,24 @@ func Run(engine *gin.Engine, conf *config.Configuration) {
|
|||
}
|
||||
fmt.Println("Started Listening for TLS connection on " + addr)
|
||||
go func() {
|
||||
log.Fatal(s.ListenAndServeTLS(conf.Server.SSL.CertFile, conf.Server.SSL.CertKey))
|
||||
listener := startListening(addr, conf.Server.KeepAlivePeriodSeconds)
|
||||
log.Fatal(s.ServeTLS(listener, conf.Server.SSL.CertFile, conf.Server.SSL.CertKey))
|
||||
}()
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%d", conf.Server.ListenAddr, conf.Server.Port)
|
||||
fmt.Println("Started Listening for plain HTTP connection on " + addr)
|
||||
log.Fatal(http.ListenAndServe(addr, httpHandler))
|
||||
server := &http.Server{Addr: addr, Handler: httpHandler}
|
||||
|
||||
log.Fatal(server.Serve(startListening(addr, conf.Server.KeepAlivePeriodSeconds)))
|
||||
}
|
||||
|
||||
func startListening(addr string, keepAlive int) net.Listener {
|
||||
lc := net.ListenConfig{KeepAlive: time.Duration(keepAlive) * time.Second}
|
||||
conn, err := lc.Listen(context.Background(), "tcp", addr)
|
||||
if err != nil {
|
||||
log.Fatalln("Could not listen on", addr, err)
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
func redirectToHTTPS(port string) http.HandlerFunc {
|
||||
|
|
|
|||
Loading…
Reference in New Issue