Skip to content

Commit

Permalink
Merge pull request #144 from shyim/add-unix-socket-support
Browse files Browse the repository at this point in the history
feat: add unix socket listing support
  • Loading branch information
jmattheis authored Dec 9, 2022
2 parents b018267 + c31eba7 commit 1b15de0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions screego.config.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ SCREEGO_TLS_CERT_FILE=
SCREEGO_TLS_KEY_FILE=

# The address the http server will listen on.
# Formats:
# - host:port
# Example: 127.0.0.1:5050
# - unix socket (must be prefixed with unix:)
# Example: unix:/my/file/path.socket
SCREEGO_SERVER_ADDRESS=0.0.0.0:5050

# The address the TURN server will listen on.
Expand Down
35 changes: 26 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package server

import (
"context"
"net"
"net/http"
"os"
"os/signal"
"strings"
"time"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -32,19 +34,34 @@ func startServer(mux *mux.Router, address, cert, key string) (*http.Server, chan

shutdown := make(chan error)
go func() {
if cert != "" || key != "" {
log.Info().Str("addr", address).Msg("Start HTTP with tls")
err := srv.ListenAndServeTLS(cert, key)
shutdown <- err
} else {
log.Info().Str("addr", address).Msg("Start HTTP")
err := srv.ListenAndServe()
shutdown <- err
}
err := listenAndServe(srv, address, cert, key)
shutdown <- err
}()
return srv, shutdown
}

func listenAndServe(srv *http.Server, address, cert, key string) error {
var err error
var listener net.Listener

if strings.HasPrefix(address, "unix:") {
listener, err = net.Listen("unix", strings.TrimPrefix(address, "unix:"))
} else {
listener, err = net.Listen("tcp", address)
}
if err != nil {
return err
}

if cert != "" || key != "" {
log.Info().Str("addr", address).Msg("Start HTTP with tls")
return srv.ServeTLS(listener, cert, key)
} else {
log.Info().Str("addr", address).Msg("Start HTTP")
return srv.Serve(listener)
}
}

func shutdownOnInterruptSignal(server *http.Server, timeout time.Duration, shutdown chan<- error) {
interrupt := make(chan os.Signal, 1)
notifySignal(interrupt, os.Interrupt)
Expand Down

0 comments on commit 1b15de0

Please sign in to comment.