2016年9月30日 星期五

GO Web cgi server (直接執行 go )

package main

import(
  "net/http/cgi"
  "log"
  "net/http"
)

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
    handler := new(cgi.Handler)
    handler.Path = "/usr/local/bin/go"
    script := "/root/goweb/" + r.URL.Path
    log.Println(handler.Path)
    handler.Dir = "/root/goweb/"
    args := []string{"run", script}
    handler.Args = append(handler.Args, args...)
    handler.Env = append(handler.Env, "GOPATH=/root/goweb")
    handler.Env = append(handler.Env, "GOROOT=/usr/local/go")
    log.Println(handler.Args)

    handler.ServeHTTP(w, r)
  })

  log.Fatal(http.ListenAndServe(":8080",nil))
}

沒有留言: