Split the static web templates to their own files

Makes it easier to work with and they will just be embedded into the
program on compilation
testing
Justin Reichardt 2022-11-16 21:34:08 -06:00
parent 498e8ef5e5
commit 0d852ce68c
3 changed files with 75 additions and 46 deletions

View File

@ -23,7 +23,6 @@ import (
"encoding/gob"
"encoding/json"
"flag"
"io"
"io/ioutil"
"log"
"net/http"
@ -31,45 +30,8 @@ import (
"os/exec"
"path"
"strconv"
"strings"
)
const template = `
<!DOCTYPE html>
<html>
<head>
<title>Download</title>
</head>
<body>
<form method="POST">
<label>URL: <input name="URL" type="text""></label>
<input type="submit" value="Download">
</form>
<script>
setInterval(()=>{
fetch("currentwork").then(response => {
if (!response.ok){
throw new Error("HTTP error " + response.status);
}
return response.text();
}).then(json => {
parsed = JSON.parse(json)
console.log(parsed);
document.getElementById("list").innerText = "";
Object.entries(parsed).forEach(([key, value]) => {document.getElementById("list").innerText += value.Status + " - " + value.URL + "\n"});
if (json === "{}"){
}
}).catch( () => {
console.log("Error retrieving setup status");
});
}
, 1000);
</script>
<p id=list></p>
</body>
</html>
`
// Used to request an operation (download)
type ops struct {
URL string
@ -152,6 +114,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
} else if r.Method == http.MethodGet {
log.Print("New Get request: " + r.URL.Path)
http.Redirect(w, r, "/static", 301)
} else if r.Method == http.MethodPost {
r.ParseForm()
go func() {
@ -159,16 +122,9 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
o.URL = r.FormValue("URL")
opsc <- o
}()
http.Redirect(w, r, "/static", 301)
} else {
}
// Modify template and send it to the requester
var insert string
for _, workItem := range currentWork {
insert += "<br>" + workItem.URL
}
temp := strings.Replace(template, "currentWork", insert, 1)
io.WriteString(w, temp)
}
// operationHandler waits for operation requests and initiates them
@ -281,6 +237,7 @@ func main() {
// Start the http server
handler := http.HandlerFunc(handleRequest)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(Content))))
http.Handle("/", handler)
log.Printf("Starting server on " + strconv.Itoa(config.port))
http.ListenAndServe(":"+strconv.Itoa(config.port), nil)

39
src/web.go Normal file
View File

@ -0,0 +1,39 @@
/*
* Copyright 2022 Justin Reichardt
*
* This file is part of viddown.
*
* fedset is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* viddown is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with fedset. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"embed"
"io/fs"
"log"
)
//go:embed web/*
var content embed.FS
var Content fs.FS
func init() {
var err error
Content, err = fs.Sub(content, "web")
if err != nil {
log.Fatal(err)
}
}

33
src/web/index.html Normal file
View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>Download</title>
</head>
<body>
<form action="/" method="POST">
<label>URL: <input name="URL" type="text""></label>
<input type="submit" value="Download">
</form>
<script>
setInterval(()=>{
fetch("/currentwork").then(response => {
if (!response.ok){
throw new Error("HTTP error " + response.status);
}
return response.text();
}).then(json => {
parsed = JSON.parse(json)
console.log(parsed);
document.getElementById("list").innerText = "";
Object.entries(parsed).forEach(([key, value]) => {document.getElementById("list").innerText += value.Status + " - " + value.URL + "\n"});
if (json === "{}"){
}
}).catch( () => {
console.log("Error retrieving setup status");
});
}
, 1000);
</script>
<p id=list></p>
</body>
</html>