-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support running on demand systemd socket activation
- Loading branch information
Showing
8 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Feature: Allow running rest-server via systemd socket activation | ||
|
||
We've added the option to have systemd create the listening socket and start the rest-server on demand. | ||
|
||
https://github.com/restic/rest-server/issues/126 | ||
https://github.com/restic/rest-server/pull/151 | ||
https://github.com/restic/rest-server/pull/127 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// +build !windows | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
"github.com/coreos/go-systemd/activation" | ||
) | ||
|
||
// findListener tries to find a listener via systemd socket activation. If that | ||
// fails, it tries to create a listener on addr. | ||
func findListener(addr string) (listener net.Listener, err error) { | ||
// try systemd socket activation | ||
listeners, err := activation.Listeners() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
switch len(listeners) { | ||
case 0: | ||
// no listeners found, listen manually | ||
listener, err = net.Listen("tcp", addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("listen on %v failed: %w", addr, err) | ||
} | ||
|
||
log.Printf("start server on %v", addr) | ||
return listener, nil | ||
|
||
case 1: | ||
// one listener supplied by systemd, use that one | ||
// | ||
// for testing, run rest-server with systemd-socket-activate as follows: | ||
// | ||
// systemd-socket-activate -l 8080 ./rest-server | ||
log.Printf("systemd socket activation mode") | ||
return listeners[0], nil | ||
|
||
default: | ||
return nil, fmt.Errorf("got %d listeners from systemd, expected one", len(listeners)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
) | ||
|
||
// findListener creates a listener. | ||
func findListener(addr string) (listener net.Listener, err error) { | ||
// listen manually | ||
listener, err = net.Listen("tcp", addr) | ||
if err != nil { | ||
return nil, fmt.Errorf("listen on %v failed: %w", addr, err) | ||
} | ||
|
||
log.Printf("start server on %v", addr) | ||
return listener, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[Socket] | ||
ListenStream = 8080 | ||
|
||
[Install] | ||
WantedBy = sockets.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters