IP cam shot.jpg is now streamed through the web server

This commit is contained in:
BroodjeAap 2020-06-29 20:12:28 +00:00
parent e1fcf85910
commit a8048c01f0
5 changed files with 27 additions and 14 deletions

33
main.go
View file

@ -1,6 +1,7 @@
package main
import (
"io/ioutil"
"log"
"net/http"
"path/filepath"
@ -13,17 +14,13 @@ type StreamURL struct {
}
func index(w http.ResponseWriter, r *http.Request) {
indexTemplate, err := template.ParseFiles(filepath.Join("templates", "index.html"))
if err != nil {
log.Fatal(err)
}
indexTemplate.Execute(w, nil)
}
func stream(w http.ResponseWriter, r *http.Request) {
if r.FormValue("URL") == "" {
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
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"),
@ -37,6 +34,22 @@ func stream(w http.ResponseWriter, r *http.Request) {
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))

0
static/css/style.css Executable file
View file

View file

@ -1 +0,0 @@
alert("test");

View file

@ -1,6 +1,6 @@
<html>
<h1>Stream URL</h1>
<form action="/stream" method="GET">
<form action="" method="GET">
<label>URL:</label>
<input type="text" name="URL" size="100">
</form>

View file

@ -1,9 +1,10 @@
<html>
<head>
<script src="/static/js/stream.js"></script>
<link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<img src="{{.URL}}">
<img src="/stream?URL={{.URL}}">
</body>
</html>