From c8b706aee623bdd15405e08911ac2e969c462795 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 11 Sep 2023 21:44:41 +0300 Subject: [PATCH] feat: embed static files and templates in binary --- .gitignore | 2 +- bindata.go | 9 +++++++++ main.go | 24 +++++++++++++++++------- {templates => static/css}/pico.min.css | 0 templates/download.html | 4 ++-- templates/templates.go | 7 ------- 6 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 bindata.go rename {templates => static/css}/pico.min.css (100%) delete mode 100644 templates/templates.go diff --git a/.gitignore b/.gitignore index 071b4af..8b56bfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tmp/ main - +viddl diff --git a/bindata.go b/bindata.go new file mode 100644 index 0000000..3f601d4 --- /dev/null +++ b/bindata.go @@ -0,0 +1,9 @@ +package main + +import "embed" + +//go:embed templates/* +var TemplatesFS embed.FS + +//go:embed static/css/* +var PublicFS embed.FS diff --git a/main.go b/main.go index e751cab..7162093 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ import ( "net/http" "time" - "gitea.hbanafa.com/hesham/viddl/templates" ) const ( @@ -79,17 +78,29 @@ func writeJSONResponse(w http.ResponseWriter, s string) http.ResponseWriter { } -var views *template.Template +var templates *template.Template + +// TODO: Change all this to have a unified context +var AppURL string = "http://localhost:8080" +type HTMLBaseData struct { + Formats []DownloadFormats + AppURL string +} + +var appData HTMLBaseData = HTMLBaseData{ + AppURL: AppURL, +} func init() { log.Println("[ init ] Starting...") - views = template.Must(template.ParseFS(templates.TemplatesFS , "*.html")) + templates = template.Must(template.ParseFS(TemplatesFS , "templates/*.html")) } func main() { handler := http.NewServeMux() + handler.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(PublicFS)))) handler.HandleFunc( "/download", @@ -131,8 +142,7 @@ func main() { log.Println("URL from convx", downloadURL) // Serve Button Template - tmpl := template.Must(template.ParseFiles("templates/download-result.html")) - err = tmpl.Execute(w, downloadURL) + err = templates.ExecuteTemplate(w,"download-result.html", downloadURL) if err != nil { log.Println(err.Error()) w.WriteHeader(500) @@ -141,14 +151,14 @@ func main() { }, ) handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - tmpl := template.Must(template.ParseFiles("templates/download.html")) formats := []DownloadFormats{} formats = append(formats, DownloadFormats{ VideoRes: "720p", audioOnly: false, videoOnly: false, }) - err := tmpl.Execute(w, formats) + appData.Formats = formats + err := templates.ExecuteTemplate(w, "download.html", appData) if err != nil { log.Println(err.Error()) w.WriteHeader(500) diff --git a/templates/pico.min.css b/static/css/pico.min.css similarity index 100% rename from templates/pico.min.css rename to static/css/pico.min.css diff --git a/templates/download.html b/templates/download.html index 1a4d45f..6749229 100644 --- a/templates/download.html +++ b/templates/download.html @@ -3,7 +3,7 @@ - + YT Download @@ -15,7 +15,7 @@