streamwatcher/main.go

60 lines
1.2 KiB
Go
Executable file

package main
import (
"io/ioutil"
"log"
"net/http"
"path/filepath"
"text/template"
)
// StreamURL Unexported
type StreamURL struct {
URL string
}
func index(w http.ResponseWriter, r *http.Request) {
if r.FormValue("URL") == "" {
indexTemplate, err := template.ParseFiles(filepath.Join("templates", "index.html"))
if err != nil {
log.Fatal(err)
}
indexTemplate.Execute(w, nil)
return
}
streamURL := StreamURL{
URL: r.FormValue("URL"),
}
streamTemplate, err := template.ParseFiles(filepath.Join("templates", "stream.html"))
if err != nil {
log.Fatal(err)
}
streamTemplate.Execute(w, streamURL)
}
func stream(w http.ResponseWriter, r *http.Request) {
if r.FormValue("URL") == "" {
return
}
//w.Header().Set("Content-Type", "image/jpeg")
URL := r.FormValue("URL")
resp, err := http.Get(URL)
if err != nil {
log.Fatal(err)
}
img, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
w.Write([]byte(img))
}
func main() {
staticFileServer := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", staticFileServer))
http.HandleFunc("/", index)
http.HandleFunc("/stream", stream)
log.Fatal(http.ListenAndServe(":8080", nil))
}