feat: embed static files and templates in binary

This commit is contained in:
HeshamTB 2023-09-11 21:44:41 +03:00
parent 98c98d16b4
commit c8b706aee6
6 changed files with 29 additions and 17 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
tmp/ tmp/
main main
viddl

9
bindata.go Normal file
View File

@ -0,0 +1,9 @@
package main
import "embed"
//go:embed templates/*
var TemplatesFS embed.FS
//go:embed static/css/*
var PublicFS embed.FS

24
main.go
View File

@ -9,7 +9,6 @@ import (
"net/http" "net/http"
"time" "time"
"gitea.hbanafa.com/hesham/viddl/templates"
) )
const ( 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() { func init() {
log.Println("[ init ] Starting...") log.Println("[ init ] Starting...")
views = template.Must(template.ParseFS(templates.TemplatesFS , "*.html")) templates = template.Must(template.ParseFS(TemplatesFS , "templates/*.html"))
} }
func main() { func main() {
handler := http.NewServeMux() handler := http.NewServeMux()
handler.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(PublicFS))))
handler.HandleFunc( handler.HandleFunc(
"/download", "/download",
@ -131,8 +142,7 @@ func main() {
log.Println("URL from convx", downloadURL) log.Println("URL from convx", downloadURL)
// Serve Button Template // Serve Button Template
tmpl := template.Must(template.ParseFiles("templates/download-result.html")) err = templates.ExecuteTemplate(w,"download-result.html", downloadURL)
err = tmpl.Execute(w, downloadURL)
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
w.WriteHeader(500) w.WriteHeader(500)
@ -141,14 +151,14 @@ func main() {
}, },
) )
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/download.html"))
formats := []DownloadFormats{} formats := []DownloadFormats{}
formats = append(formats, DownloadFormats{ formats = append(formats, DownloadFormats{
VideoRes: "720p", VideoRes: "720p",
audioOnly: false, audioOnly: false,
videoOnly: false, videoOnly: false,
}) })
err := tmpl.Execute(w, formats) appData.Formats = formats
err := templates.ExecuteTemplate(w, "download.html", appData)
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
w.WriteHeader(500) w.WriteHeader(500)

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css"> <link rel="stylesheet" href="{{ .AppURL }}/static/static/css/pico.min.css">
<script src="https://unpkg.com/htmx.org@1.9.5"></script> <script src="https://unpkg.com/htmx.org@1.9.5"></script>
<title>YT Download</title> <title>YT Download</title>
</head> </head>
@ -15,7 +15,7 @@
<!-- Select --> <!-- Select -->
<select required> <select required>
<option value="" disabled selected>Format</option> <option value="" disabled selected>Format</option>
{{ range . }} <option>{{ .VideoRes }}</option>{{ end }} {{ range .Formats }} <option>{{ .VideoRes }}</option>{{ end }}
</select> </select>
</div> </div>
<button id="get-btn">Get <button id="get-btn">Get

View File

@ -1,7 +0,0 @@
package templates
import "embed"
//go:embed *.html
var TemplatesFS embed.FS