Skip to content

Instantly share code, notes, and snippets.

@koduki
Last active May 13, 2019 08:12
Show Gist options
  • Save koduki/de924812e0f25c610701bea147a51425 to your computer and use it in GitHub Desktop.
Save koduki/de924812e0f25c610701bea147a51425 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "os"
import "os/exec"
import "net/http"
func main() {
port := os.Args[1]
script := os.Args[2]
http.HandleFunc("/", handler(script))
http.ListenAndServe(":"+port, nil)
}
func handler(script string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
msg := run(script)
fmt.Fprintf(w, msg)
}
}
func run(script string) string {
cmd := exec.Command(script)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
var msg string
if err == nil {
msg = "success"
} else {
msg = "error"
fmt.Println(err)
}
return msg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment