simplified library and added better examples and net.UnixListener support

This commit is contained in:
ston1th 2017-04-19 20:42:19 +02:00
commit 694ac1e8a9
7 changed files with 95 additions and 61 deletions

View file

@ -24,8 +24,9 @@ func main() {
fmt.Println("Failed to listen on FD 3:", err)
os.Exit(1)
}
_, port, _ := net.SplitHostPort(l.Addr().String())
http.Serve(l, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "pid %d\nuid %d\ngid %d", os.Getpid(), os.Getuid(), os.Getgid())
fmt.Fprintf(w, "port %s\npid %d\nuid %d\ngid %d", port, os.Getpid(), os.Getuid(), os.Getgid())
}))
}

52
example/port80and443.go Normal file
View file

@ -0,0 +1,52 @@
package main
import (
"fmt"
"net"
"net/http"
"os"
"git.giftfish.de/ston1th/godrop"
)
func main() {
cfg := godrop.Config{
User: "nobody",
Group: "nobody",
}
err := godrop.MultiDrop(cfg, func() ([]net.Listener, error) {
l1, err := net.Listen("tcp", ":80")
if err != nil {
return nil, err
}
l2, err := net.Listen("tcp", ":443")
return []net.Listener{l1, l2}, err
})
if err != nil {
fmt.Println(err)
}
l1, err := godrop.GetListener()
if err != nil {
fmt.Println("Failed to listen on FD 3:", err)
os.Exit(1)
}
_, port1, _ := net.SplitHostPort(l1.Addr().String())
l2, err := godrop.GetListenerFd(4)
if err != nil {
fmt.Println("Failed to listen on FD 4:", err)
os.Exit(1)
}
_, port2, _ := net.SplitHostPort(l2.Addr().String())
go func() {
http.Serve(l1, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "port %s\npid %d\nuid %d\ngid %d", port1, os.Getpid(), os.Getuid(), os.Getgid())
}))
}()
http.Serve(l2, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "port %s\npid %d\nuid %d\ngid %d", port2, os.Getpid(), os.Getuid(), os.Getgid())
}))
}