Skip to content

Add support for listening on unix socket #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions imaginary.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

var (
aAddr = flag.String("a", "", "Bind address")
aUnixSocket = flag.String("unix-socket", "", "Path to the unix socket to listen on. If specified - address and port flags are ignored")
aPort = flag.Int("p", 8088, "Port to listen")
aVers = flag.Bool("v", false, "Show version")
aVersl = flag.Bool("version", false, "Show version")
Expand Down Expand Up @@ -58,6 +59,7 @@ const usage = `imaginary %s

Usage:
imaginary -p 80
imaginary -unix-socket /var/run/imaginary.sock
imaginary -cors
imaginary -concurrency 10
imaginary -path-prefix /api/v1
Expand All @@ -76,6 +78,7 @@ Usage:
Options:

-a <addr> Bind address [default: *]
-unix-socket Path to the unix socket to listen on. If specified - address and port flags are ignored
-p <port> Bind port [default: 8088]
-h, -help Show help
-v, -version Show version
Expand Down Expand Up @@ -138,6 +141,7 @@ func main() {
opts := ServerOptions{
Port: port,
Address: *aAddr,
UnixSocket: *aUnixSocket,
CORS: *aCors,
AuthForwarding: *aAuthForwarding,
EnableURLSource: *aEnableURLSource,
Expand Down
9 changes: 9 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"log"
"net"
"net/http"
"net/url"
"os"
Expand All @@ -17,6 +18,7 @@ import (
type ServerOptions struct {
Port int
Burst int
UnixSocket string
Concurrency int
HTTPCacheTTL int
HTTPReadTimeout int
Expand Down Expand Up @@ -98,6 +100,13 @@ func Server(o ServerOptions) {
}

func listenAndServe(s *http.Server, o ServerOptions) error {
if o.UnixSocket != "" {
listener, err := net.Listen("unix", o.UnixSocket)
if err != nil {
return err
}
return s.Serve(listener)
}
if o.CertFile != "" && o.KeyFile != "" {
return s.ListenAndServeTLS(o.CertFile, o.KeyFile)
}
Expand Down