package main import ( "fmt" "github.com/gorilla/mux" "io/ioutil" "log" "net/http" "os/exec" "time" ) var ( fileindex int ) func main() { r := mux.NewRouter() r.HandleFunc("/", indexGET).Methods("GET") r.HandleFunc("/", indexPOST).Methods("POST") log.Fatal(http.ListenAndServe(":8080", r)) } func indexGET(w http.ResponseWriter, r *http.Request) { log.Println("GET") site := `
gify url:

` fmt.Fprintf(w, "%s", site) } func indexPOST(w http.ResponseWriter, r *http.Request) { log.Println("recievend gif, downloading...") url := r.PostFormValue("url") resp, err := http.Get(url) defer resp.Body.Close() if err != nil { log.Printf("Could not makr http request to %s", url) } // read the response content body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Println("Could not parse the body") } // write the content to a file ioutil.WriteFile(fmt.Sprintf("gifs/%d", fileindex), body, 0644) bodyText := string(body) log.Println("Response length: ", len(bodyText)) // Convert to rune slice to take substring. runeSlice := []rune(bodyText) fmt.Println("First chars: ", string(runeSlice[0:10])) cmd := exec.Command("killall", "mpv") cmd.Start() time.Sleep(1 * time.Second) cmd = exec.Command("mpv", "--loop-file=inf", "--fs", fmt.Sprintf("gifs/%d", fileindex)) cmd.Start() fileindex++ http.Redirect(w, r, "/", 302) }