mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-04 18:19:54 +00:00
6fdab4be34
What a wonderful package!
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(handler http.Handler) {
|
|
server := &http.Server{
|
|
ReadTimeout: 300 * time.Second,
|
|
WriteTimeout: 300 * time.Second,
|
|
IdleTimeout: 300 * time.Second,
|
|
Handler: handler,
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|