diff --git a/config.yaml b/config.yaml index d4087ce..f10bb9d 100644 --- a/config.yaml +++ b/config.yaml @@ -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 diff --git a/lurkcoin/api/config.go b/lurkcoin/api/config.go index 937e3c5..b4fd244 100644 --- a/lurkcoin/api/config.go +++ b/lurkcoin/api/config.go @@ -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 == "" { - urlAddress = "[::]" + urlAddress + + 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) diff --git a/lurkcoin/misc.go b/lurkcoin/misc.go index b128dd0..8c5d307 100644 --- a/lurkcoin/misc.go +++ b/lurkcoin/misc.go @@ -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"