Skip to content

Instantly share code, notes, and snippets.

@funnythingz
Last active December 7, 2015 08:34
Show Gist options
  • Save funnythingz/dcbc2d326b6a5572963e to your computer and use it in GitHub Desktop.
Save funnythingz/dcbc2d326b6a5572963e to your computer and use it in GitHub Desktop.
Nginx + FastCGI daemon + goji
package main
import (
"fmt"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"net"
"net/http"
"net/http/fcgi"
"os"
"syscall"
)
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}
func main() {
errcd := daemon(0, 0)
if errcd != 0 {
fmt.Println("daemon err!!")
os.Exit(1)
}
goji.Get("/", hello)
listener, _ := net.Listen("tcp", "127.0.0.1:3000")
fcgi.Serve(listener, goji.DefaultMux)
}
func daemon(nochdir, noclose int) int {
var ret uintptr
var err syscall.Errno
ret, _, err = syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
if err != 0 {
return -1
}
switch ret {
case 0:
break
default:
os.Exit(0)
}
pid, _ := syscall.Setsid()
if pid == -1 {
return -1
}
if nochdir == 0 {
os.Chdir("/")
}
syscall.Umask(0)
if noclose == 0 {
f, e := os.OpenFile("/dev/null", os.O_RDWR, 0)
if e == nil {
fd := int(f.Fd())
syscall.Dup2(fd, int(os.Stdin.Fd()))
syscall.Dup2(fd, int(os.Stdout.Fd()))
syscall.Dup2(fd, int(os.Stderr.Fd()))
}
}
return 0
}
@funnythingz
Copy link
Author

server {
    listen       8080;
    server_name  localhost;

    location / {
        fastcgi_pass 127.0.0.1:8000;
        include fastcgi.conf;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment