mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-15 22:40:27 +00:00
295c209472
It allows for more control and things like listening on Unix sockets or binding to 127.0.0.1 rather than 0.0.0.0. Config changes: - "Port = 1737" changed to "ListenAddr = 127.0.0.1:1737" - new env variable LISTEN_ADDR
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
|
)
|
|
|
|
func serveHTTP() {
|
|
server := &http.Server{
|
|
ReadTimeout: 300 * time.Second,
|
|
WriteTimeout: 300 * time.Second,
|
|
IdleTimeout: 300 * time.Second,
|
|
Handler: http.DefaultServeMux,
|
|
}
|
|
|
|
if strings.HasPrefix(cfg.ListenAddr, "/") {
|
|
startUnixSocketServer(server, cfg.ListenAddr)
|
|
} else {
|
|
server.Addr = cfg.ListenAddr
|
|
startHTTPServer(server)
|
|
}
|
|
}
|
|
|
|
func startUnixSocketServer(server *http.Server, socketFile string) {
|
|
os.Remove(socketFile)
|
|
|
|
listener, err := net.Listen("unix", socketFile)
|
|
if err != nil {
|
|
log.Fatalf("Failed to start a server: %v", err)
|
|
}
|
|
defer listener.Close()
|
|
|
|
if err := os.Chmod(socketFile, 0666); err != nil {
|
|
log.Fatalf("Failed to set socket permissions: %v", err)
|
|
}
|
|
|
|
log.Printf("Listening on Unix socket %s", cfg.ListenAddr)
|
|
if err := server.Serve(listener); err != http.ErrServerClosed {
|
|
log.Fatalf("Failed to start a server: %v", err)
|
|
}
|
|
}
|
|
|
|
func startHTTPServer(server *http.Server) {
|
|
log.Printf("Listening on %s", server.Addr)
|
|
if err := server.ListenAndServe(); err != http.ErrServerClosed {
|
|
log.Fatalf("Failed to start a server: %v", err)
|
|
}
|
|
}
|