WIP: Add UNIX socket support.

This commit is contained in:
luk3yx 2020-06-15 17:44:51 +12:00
parent a093f741df
commit a29e6f1cd6
3 changed files with 46 additions and 8 deletions

View File

@ -5,9 +5,13 @@
name: Test
# The address and port to bind on.
# address: "[::]"
address: "[::]"
port: 5000
# Alternatively, lurkcoin can bind on a UNIX domain socket.
# network_protocol: unix
# address: "/tmp/lurkcoin.sock"
# TLS (optional).
tls:
enable: false

View File

@ -25,6 +25,7 @@ import (
"log"
"lurkcoin"
"lurkcoin/databases"
"net"
"net/http"
"os"
"strings"
@ -35,6 +36,10 @@ type Config struct {
// the default server name for the v2 API.
Name string `yaml:"name"`
// The network protocol to use when binding to the socket. Defaults to
// "tcp", can be set to "unix" for example.
NetworkProtocol string `yaml:"network_protocol"`
// The address to bind to (optional) and port.
Address string `yaml:"address"`
Port uint16 `yaml:"port"`
@ -115,17 +120,45 @@ func StartServer(config *Config) {
router := MakeHTTPRouter(db, config)
address := fmt.Sprintf("%s:%d", config.Address, config.Port)
urlAddress := address
if config.Address == "" {
var address, networkProtocol, urlAddress string
switch config.NetworkProtocol {
case "", "tcp":
if config.Port == 0 {
address = config.Address
} else {
address = fmt.Sprintf("%s:%d", address, config.Port)
}
networkProtocol = "tcp"
urlAddress = address
if address != "" && address[0] == ':' {
urlAddress = "[::]" + urlAddress
}
case "unix":
address = config.Address
networkProtocol = "unix"
urlAddress = "unix:" + address + ":"
if config.Port != 0 {
log.Fatal("The port option is invalid with UNIX sockets.")
}
default:
log.Fatalf("Unrecognised network protocol: %q", config.NetworkProtocol)
}
if config.TLS.Enable {
log.Printf("Starting server on https://%s/", urlAddress)
} else {
log.Printf("Starting server on http://%s/", urlAddress)
}
// Bind to the address
var ln net.Listener
ln, err = net.Listen(networkProtocol, address)
if err != nil {
log.Fatal(err)
}
// Switch to the logfile
if config.Logfile != "" {
f, err := os.OpenFile(
config.Logfile,
@ -151,10 +184,11 @@ func StartServer(config *Config) {
server.SetKeepAlivesEnabled(false)
}
// Serve the webpage
if config.TLS.Enable {
err = server.ListenAndServeTLS(config.TLS.CertFile, config.TLS.KeyFile)
err = server.ServeTLS(ln, config.TLS.CertFile, config.TLS.KeyFile)
} else {
err = server.ListenAndServe()
err = server.Serve(ln)
}
log.Fatal(err)

View File

@ -35,7 +35,7 @@ import (
)
const SYMBOL = "¤"
const VERSION = "3.0.0"
const VERSION = "3.0.1 alpha"
// Note that public source code is required by the AGPL
const SOURCE_URL = "https://github.com/luk3yx/lurkcoin-core"