aboutsummaryrefslogblamecommitdiff
path: root/main.go
blob: 5049fe2a9d5695635dc7ff13dc2e6fd583c0d1ce (plain) (tree)
1
2
3
4
5
6
7
8
9
10


            
              
             
             

                      

                   
                 


             



                                                                                                                        
                                                                                 

                    



















                                                                   
                                  



                                               
         
                                   
                                                                    
 
package main

import (
	"flag"
	"log"
	"net"
	"net/http"
	"net/http/cgi"
	"os"
	"os/signal"
	"strings"
)

func main() {
	path := flag.String("path", "", "Path to the cgi binary")
	dir := flag.String("dir", "", "set a different working directory from the base path of 'path'")
	env := flag.String("env", "", "set environment variables for the CGI process")
	prefix := flag.String("uri-prefix", "/", "set the URL prefix when the CGI process is hosted in a sub directory")
	addr := flag.String("listen", "127.0.0.1:8080", "set the listen address")
	flag.Parse()

	listenAddr := *addr
	listenType := "unix"
	switch listenAddr[0] {
	case ':', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
		listenType = "tcp"
	}
	if listenType == "unix" {
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt)
		go func() {
			<-c
			os.Remove(listenAddr)
			os.Exit(0)
		}()
	}
	l, err := net.Listen(listenType, listenAddr)
	if err != nil {
		log.Fatalf("could not start to listen: %s", err)
	}

	forwarder := &cgi.Handler{
		Path: *path,
		Root: *prefix,
		Env:  strings.Split(*env, ","),
		Dir:  *dir,
	}
	http.Handle("/", forwarder)
	log.Printf("server stopped working: %s", http.Serve(l, nil))
}