1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-15 22:40:27 +00:00
mycorrhiza/httpd.go

55 lines
1.2 KiB
Go
Raw Normal View History

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)
}
}