Skip to content

Instantly share code, notes, and snippets.

@todashuta
Created May 8, 2022 12:13
Show Gist options
  • Select an option

  • Save todashuta/766da38e982c4bd80262796dde72955c to your computer and use it in GitHub Desktop.

Select an option

Save todashuta/766da38e982c4bd80262796dde72955c to your computer and use it in GitHub Desktop.
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