Last active
December 7, 2015 08:34
-
-
Save funnythingz/dcbc2d326b6a5572963e to your computer and use it in GitHub Desktop.
Nginx + FastCGI daemon + goji
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 ( | |
"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 | |
} |
Author
funnythingz
commented
Feb 19, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment