Created
May 8, 2022 12:13
-
-
Save todashuta/766da38e982c4bd80262796dde72955c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "flag" | |
| "fmt" | |
| "log" | |
| "net" | |
| "net/http" | |
| "net/http/fcgi" | |
| "os" | |
| ) | |
| var ( | |
| sockPath = flag.String("sockpath", "/tmp/gofcgitest.sock", "socket path") | |
| ) | |
| func myLog(h http.Handler) http.Handler { | |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| log.Printf("%q %q %q\n", r.RemoteAddr, r.Method, r.URL) | |
| h.ServeHTTP(w, r) | |
| }) | |
| } | |
| func main() { | |
| flag.Parse() | |
| if _, err := os.Stat(*sockPath); err == nil { | |
| _ = os.Remove(*sockPath) | |
| log.Printf("previous sock file removed: %s", *sockPath) | |
| } | |
| http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprintf(w, "Hello fcgi!") | |
| }) | |
| ln, err := net.Listen("unix", *sockPath) | |
| if err != nil { | |
| log.Fatalf("can't listen on %s: %v", *sockPath, err) | |
| } | |
| defer ln.Close() | |
| log.Printf("listening on %s", *sockPath) | |
| log.Fatal(fcgi.Serve(ln, myLog(http.DefaultServeMux))) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment