package main import ( "flag" "io/ioutil" "net/http" "os" "os/exec" "yumc.pw/cloud/html2pic/uuid" ) func main() { b := flag.String("b", ":8080", "Server Bind Address") lib := flag.String("lib", "/root", "Set Phantomjs Root Path") tempDir := flag.String("temp", os.TempDir(), "") flag.Parse() http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { uid, _ := uuid.NewV4() uuid := "html2pic-" + uid.String() var ( url string html string ) if r.Method == "GET" { url, html = r.FormValue("url"), r.FormValue("html") } else { url, html = r.PostFormValue("url"), r.PostFormValue("html") } tempFile := *tempDir + "/" + uuid + ".html" if html != "" { ioutil.WriteFile(tempFile, []byte("
"+html+""), os.ModeAppend) url = "file://" + tempFile os.Chmod(tempFile, 0755) defer os.Remove(tempFile) } if url == "" { w.Write([]byte("Empty Url...")) return } tempPic := *tempDir + "/" + uuid + ".png" err := exec.Command(*lib+"/phantomjs", *lib+"/screenshot.js", url, tempPic).Run() defer os.Remove(tempPic) if err != nil { w.Write([]byte(err.Error())) return } bytes, _ := ioutil.ReadFile(tempPic) w.Write(bytes) }) http.ListenAndServe(*b, nil) }