add bootstrap assets

This commit is contained in:
Balakrishnan Balasubramanian 2022-07-14 15:47:19 -04:00
parent 1567035954
commit 5a8af2699b
3 changed files with 41 additions and 2 deletions

7
assets/bootstrap.css vendored Normal file

File diff suppressed because one or more lines are too long

34
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"bufio"
"embed"
_ "embed"
"flag"
"fmt"
@ -24,11 +25,16 @@ import (
//go:embed templates/index.html
var page string
//go:embed assets
var assetsFS embed.FS
var (
ytdlCmd = []string{"youtube-dl"}
videosPath = "./vids"
videosUrl = "/vids"
cachePath = "./cache"
assetsPath = "./assets"
saveAssets = false
dbPath = "./db.json"
port = 8080
)
@ -42,6 +48,8 @@ func parse() {
flag.StringVar(&videosUrl, "videosurl", videosUrl, "Prefix of the url, i.e. https://domain.com/<this var>/<video filename>")
flag.StringVar(&cachePath, "cachepath", cachePath, "Path where temporary download files are saved")
flag.StringVar(&dbPath, "dbpath", dbPath, "Path where downloaded info is saved")
flag.StringVar(&assetsPath, "assetspath", assetsPath, "Path where css files are saved and served")
flag.BoolVar(&saveAssets, "saveassets", saveAssets, "Should the assets be saved in dir, so can be served by web server")
flag.IntVar(&port, "port", port, "Port to listen on")
flag.Parse()
@ -51,6 +59,9 @@ func parse() {
os.MkdirAll(cachePath, 0755)
os.MkdirAll(videosPath, 0755)
os.MkdirAll(path.Dir(dbPath), 0755)
if saveAssets {
os.MkdirAll(assetsPath, 0755)
}
}
func main() {
@ -70,7 +81,28 @@ func main() {
log.Panic(err)
}
defer d.Save()
http.Handle("/assets/", http.FileServer(http.Dir("./")))
assets := []string{"assets/bootstrap.css"}
if saveAssets {
for _, assetFile := range assets {
fname := strings.TrimPrefix(assetFile, "assets/")
contents, err := assetsFS.ReadFile(assetFile)
if err != nil {
log.Panic(err)
}
destination := path.Join(assetsPath, fname)
err = os.WriteFile(destination, contents, 0644)
if err != nil {
log.Printf("failed writing asset, assetFile: %s, destination: %s\n", assetFile, destination)
}
}
}
for _, assetFile := range assets {
http.Handle("/"+assetFile, http.FileServer(http.FS(assetsFS)))
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
d.Run(func(d *db.Jdb) {

View File

@ -6,7 +6,7 @@
<title>Youtube Downloader UI</title>
<!-- Diable favicon requests: https://stackoverflow.com/a/13416784 -->
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<link href="assets/bootstrap/css/bootstrap.css" rel="stylesheet" >
<link href="assets/bootstrap.css" rel="stylesheet" >
<script>
// https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event
window.addEventListener('DOMContentLoaded', () => {