From 7a9978ed9875077e221178e76346e3c7971d7e32 Mon Sep 17 00:00:00 2001 From: Yumechi Date: Fri, 6 Feb 2026 22:53:06 +0800 Subject: [PATCH] enhance(backend): allow disabling HTTP port completely Signed-off-by: Yumechi --- config.example.yml | 2 +- runner/runner.go | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/config.example.yml b/config.example.yml index 393e9ea4..df4490ca 100644 --- a/config.example.yml +++ b/config.example.yml @@ -4,7 +4,7 @@ server: keepaliveperiodseconds: 0 # 0 = use Go default (15s); -1 = disable keepalive; set the interval in which keepalive packets 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. Prefix with "unix:" to create a unix socket. Example: "unix:/tmp/gotify.sock". - port: 80 # the port the HTTP server will listen on + port: 80 # the port the HTTP server will listen on, use -1 to disable the HTTP server ssl: enabled: false # if https should be enabled diff --git a/runner/runner.go b/runner/runner.go index c1ce239c..9be340b4 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -22,13 +22,23 @@ func Run(router http.Handler, conf *config.Configuration) error { shutdown := make(chan error) go doShutdownOnSignal(shutdown) - httpListener, err := startListening("plain connection", conf.Server.ListenAddr, conf.Server.Port, conf.Server.KeepAlivePeriodSeconds) - if err != nil { - return err + s := &http.Server{Handler: router} + + hasListener := false + + if conf.Server.Port >= 0 { + httpListener, err := startListening("plain connection", conf.Server.ListenAddr, conf.Server.Port, conf.Server.KeepAlivePeriodSeconds) + if err != nil { + return err + } + hasListener = true + defer httpListener.Close() + go func() { + err := s.Serve(httpListener) + doShutdown(shutdown, err) + }() } - defer httpListener.Close() - s := &http.Server{Handler: router} if conf.Server.SSL.Enabled { if conf.Server.SSL.LetsEncrypt.Enabled { applyLetsEncrypt(s, conf) @@ -40,6 +50,7 @@ func Run(router http.Handler, conf *config.Configuration) error { if err != nil { return err } + hasListener = true defer httpsListener.Close() go func() { @@ -47,12 +58,12 @@ func Run(router http.Handler, conf *config.Configuration) error { doShutdown(shutdown, err) }() } - go func() { - err := s.Serve(httpListener) - doShutdown(shutdown, err) - }() - err = <-shutdown + if !hasListener { + log.Fatalln("No listener started, both plain and TLS listeners are disabled") + } + + err := <-shutdown fmt.Println("Shutting down:", err) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)