Skip to content

Commit 7a9978e

Browse files
enhance(backend): allow disabling HTTP port completely
Signed-off-by: Yumechi <yume@yumechi.jp>
1 parent 1818c5d commit 7a9978e

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

config.example.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
server:
55
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.
66
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".
7-
port: 80 # the port the HTTP server will listen on
7+
port: 80 # the port the HTTP server will listen on, use -1 to disable the HTTP server
88

99
ssl:
1010
enabled: false # if https should be enabled

runner/runner.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@ func Run(router http.Handler, conf *config.Configuration) error {
2222
shutdown := make(chan error)
2323
go doShutdownOnSignal(shutdown)
2424

25-
httpListener, err := startListening("plain connection", conf.Server.ListenAddr, conf.Server.Port, conf.Server.KeepAlivePeriodSeconds)
26-
if err != nil {
27-
return err
25+
s := &http.Server{Handler: router}
26+
27+
hasListener := false
28+
29+
if conf.Server.Port >= 0 {
30+
httpListener, err := startListening("plain connection", conf.Server.ListenAddr, conf.Server.Port, conf.Server.KeepAlivePeriodSeconds)
31+
if err != nil {
32+
return err
33+
}
34+
hasListener = true
35+
defer httpListener.Close()
36+
go func() {
37+
err := s.Serve(httpListener)
38+
doShutdown(shutdown, err)
39+
}()
2840
}
29-
defer httpListener.Close()
3041

31-
s := &http.Server{Handler: router}
3242
if conf.Server.SSL.Enabled {
3343
if conf.Server.SSL.LetsEncrypt.Enabled {
3444
applyLetsEncrypt(s, conf)
@@ -40,19 +50,20 @@ func Run(router http.Handler, conf *config.Configuration) error {
4050
if err != nil {
4151
return err
4252
}
53+
hasListener = true
4354
defer httpsListener.Close()
4455

4556
go func() {
4657
err := s.ServeTLS(httpsListener, conf.Server.SSL.CertFile, conf.Server.SSL.CertKey)
4758
doShutdown(shutdown, err)
4859
}()
4960
}
50-
go func() {
51-
err := s.Serve(httpListener)
52-
doShutdown(shutdown, err)
53-
}()
5461

55-
err = <-shutdown
62+
if !hasListener {
63+
log.Fatalln("No listener started, both plain and TLS listeners are disabled")
64+
}
65+
66+
err := <-shutdown
5667
fmt.Println("Shutting down:", err)
5768

5869
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)

0 commit comments

Comments
 (0)