forked from bouncepaw/mycorrhiza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd.go
54 lines (45 loc) · 1.19 KB
/
httpd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)
}
}