From 0d852ce68cb1676921dd24e4a352f1800f25ff93 Mon Sep 17 00:00:00 2001 From: Justin Reichardt Date: Wed, 16 Nov 2022 21:34:08 -0600 Subject: [PATCH] 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 --- src/viddown.go | 49 +++------------------------------------------- src/web.go | 39 ++++++++++++++++++++++++++++++++++++ src/web/index.html | 33 +++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 46 deletions(-) create mode 100644 src/web.go create mode 100644 src/web/index.html diff --git a/src/viddown.go b/src/viddown.go index 70c703c..d19d057 100644 --- a/src/viddown.go +++ b/src/viddown.go @@ -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 = ` - - - - Download - - -
- - -
- -

- - -` - // 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 += "
" + 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) diff --git a/src/web.go b/src/web.go new file mode 100644 index 0000000..7611efe --- /dev/null +++ b/src/web.go @@ -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 . + */ + +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) + } +} diff --git a/src/web/index.html b/src/web/index.html new file mode 100644 index 0000000..8196ff9 --- /dev/null +++ b/src/web/index.html @@ -0,0 +1,33 @@ + + + + Download + + +
+ + +
+ +

+ +