aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrégoire Duchêne <gduchene@awhk.org>2021-07-04 16:31:15 +0100
committerGrégoire Duchêne <gduchene@awhk.org>2021-07-04 16:37:52 +0100
commit9a51680e1580bcecaad9e295368b9f98b853eab6 (patch)
treeed8c18c0d5f64e08c99690f3ccf8f587a7331418
parent80b5a6680e2fc4d2f1e425ff2e5f1cfbbd529d9e (diff)
Add systemd socket support
-rw-r--r--README.md26
-rw-r--r--archlinux/PKGBUILD1
-rw-r--r--go.mod1
-rw-r--r--go.sum11
-rw-r--r--main.go67
-rw-r--r--systemd/go-import-redirect.service2
-rw-r--r--systemd/go-import-redirect.socket11
7 files changed, 80 insertions, 39 deletions
diff --git a/README.md b/README.md
index 2b975db..fee26c7 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,7 @@
# go-import-redirect
A thing that tells `go get` where to get Go packages and redirects users
-to https://godoc.org. It runs on AWS Lambda, but can easily be adapted
-to run elsewhere.
+to https://godoc.org.
You need to set up the following environment variables for it to work:
@@ -11,9 +10,22 @@ You need to set up the following environment variables for it to work:
e.g. `golang.org/x/` for `golang.org/x/image`, and
* `VCS` for the type of VCS you are using, e.g. `git`.
-Additionally, the version under `gcp/` will use `ADDR` for the address
-to listen on, defaulting to every address on port 8080. See
-https://golang.org/pkg/net/#Dial for details of the address format.
+There are three modes of operation:
-If `ADDR` is set and starts with `/`, go-import-redirect will treat it
-as the path of a UNIX socket to create and listen on.
+1. **As a systemd service.** In this mode, it is recommended to enable
+ the companion systemd socket and configure that. systemd will only
+ start the service when needed. If you are not using the companion
+ systemd socket but rather the systemd service directly, you will need
+ to set the `ADDR` environment variable to tell go-import-redirect how
+ to listen for incoming connections. If `ADDR` is set and starts with
+ `/`, go-import-redirect will treat it as a UNIX socket path. If
+ `ADDR` is not set, it will default to `:8080`. See
+ https://golang.org/pkg/net/#Dial for more details.
+
+2. **Inside a Docker container.** In this mode, the port 8080 will be
+ exposed and no other environment variable besides the ones above need
+ to be set.
+
+3. **As an AWS Lambda.** This is very similar to Docker. You will need
+ to set the environment variables above. Note that this version needs
+ to be compiled as a Linux binary and with the `aws` build tag set.
diff --git a/archlinux/PKGBUILD b/archlinux/PKGBUILD
index e8a538d..a6e3830 100644
--- a/archlinux/PKGBUILD
+++ b/archlinux/PKGBUILD
@@ -24,5 +24,6 @@ package() {
cd ${pkgname}
install -Dm755 ${pkgname} ${pkgdir}/usr/bin/${pkgname}
install -Dm644 systemd/${pkgname}.service ${pkgdir}/usr/lib/systemd/system/${pkgname}.service
+ install -Dm644 systemd/${pkgname}.socket ${pkgdir}/usr/lib/systemd/system/${pkgname}.socket
install -Dm644 LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}
diff --git a/go.mod b/go.mod
index 490ec99..81b1634 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ go 1.12
require (
github.com/aws/aws-lambda-go v1.22.0
github.com/kr/pretty v0.1.0 // indirect
+ go.awhk.org/gosdd v0.2.0
golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
diff --git a/go.sum b/go.sum
index dfcaae1..c9ad55b 100644
--- a/go.sum
+++ b/go.sum
@@ -1,38 +1,31 @@
-github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aws/aws-lambda-go v1.22.0 h1:X7BKqIdfoJcbsEIi+Lrt5YjX1HnZexIbNWOQgkYKgfE=
github.com/aws/aws-lambda-go v1.22.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU=
-github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
-github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
-github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
-github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
-github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
+go.awhk.org/gosdd v0.2.0 h1:fNqC+9LtwfwUxDDq0d8cIoFllbzZmi8+fUIlew5D7vc=
+go.awhk.org/gosdd v0.2.0/go.mod h1:9LeQ9cX4ZghIWv6kF9ljk83NzsZJqQR3/R61DbsU3YI=
golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b h1:lAZ0/chPUDWwjqosYR0X4M490zQhMsiJ4K3DbA7o+3g=
golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
diff --git a/main.go b/main.go
index c2b331e..8cc70fd 100644
--- a/main.go
+++ b/main.go
@@ -19,6 +19,8 @@ import (
"time"
"golang.org/x/sys/unix"
+
+ "go.awhk.org/gosdd"
)
func redirect(resp http.ResponseWriter, req *http.Request) {
@@ -50,28 +52,14 @@ func main() {
signal.Notify(done, os.Interrupt, unix.SIGTERM)
go func() {
- var (
- addr = os.Getenv("ADDR")
- l net.Listener
- err error
- )
- if addr != "" && addr[0] == '/' {
- if l, err = net.Listen("unix", addr); err != nil {
- log.Fatalln("net.Listen:", err)
- }
- // We do not do any authorization anyway, so 0666 makes sense here.
- if err = os.Chmod(addr, 0666); err != nil {
- log.Fatalln("os.Chmod:", err)
- }
- } else {
- if addr == "" {
- addr = ":8080"
- }
- if l, err = net.Listen("tcp", addr); err != nil {
- log.Fatalln("net.Listen:", err)
- }
+ ln, err := listenSD()
+ if err != nil {
+ log.Fatalln("listenSD:", err)
+ }
+ if ln == nil {
+ ln = listenEnv()
}
- if err = srv.Serve(l); err != nil && err != http.ErrServerClosed {
+ if err := srv.Serve(ln); err != nil && err != http.ErrServerClosed {
log.Fatalln("server.ListenAndServe:", err)
}
}()
@@ -83,3 +71,40 @@ func main() {
log.Fatalln("server.Shutdown:", err)
}
}
+
+func listenEnv() net.Listener {
+ addr := os.Getenv("ADDR")
+ if addr == "" || addr[0] != '/' {
+ if addr == "" {
+ addr = ":8080"
+ }
+ ln, err := net.Listen("tcp", ":8080")
+ if err != nil {
+ log.Fatalln("net.Listen:", err)
+ }
+ return ln
+ }
+ ln, err := net.Listen("unix", addr)
+ if err != nil {
+ log.Fatalln("net.Listen:", err)
+ }
+ // We do not do any authorization anyway, so 0666 makes sense here.
+ if err = os.Chmod(addr, 0666); err != nil {
+ log.Fatalln("os.Chmod:", err)
+ }
+ return ln
+}
+
+func listenSD() (net.Listener, error) {
+ fds, err := gosdd.SDListenFDs(true)
+ if err != nil {
+ if err == gosdd.ErrNoSDSupport {
+ return nil, nil
+ }
+ return nil, err
+ }
+ if len(fds) == 0 {
+ return nil, nil
+ }
+ return net.FileListener(fds[0])
+}
diff --git a/systemd/go-import-redirect.service b/systemd/go-import-redirect.service
index 0691753..63de3f3 100644
--- a/systemd/go-import-redirect.service
+++ b/systemd/go-import-redirect.service
@@ -7,10 +7,8 @@
WantedBy=multi-user.target
[Service]
- Environment=ADDR=%t/go-import-redirect/socket
ExecStart=go-import-redirect
DynamicUser=true
- RuntimeDirectory=go-import-redirect
[Unit]
Description=go-import-redirect
diff --git a/systemd/go-import-redirect.socket b/systemd/go-import-redirect.socket
new file mode 100644
index 0000000..1fc2996
--- /dev/null
+++ b/systemd/go-import-redirect.socket
@@ -0,0 +1,11 @@
+# SPDX-FileCopyrightText: © 2021 Grégoire Duchêne <gduchene@awhk.org>
+# SPDX-License-Identifier: ISC
+
+[Unit]
+Description=go-import-redirect Socket
+
+[Socket]
+ListenStream=%t/go-import-redirect
+
+[Install]
+WantedBy=sockets.target