Browse Source

Add page templates + restructure

master
PrivateGER 3 years ago
parent
commit
4392d5d2fb
Signed by: PrivateGER GPG Key ID: CAE625C962F94C67
  1. 18
      PageHandlers/Index.go
  2. 16
      PageHandlers/Search.go
  3. 58
      PageHandlers/Templates.go
  4. 16
      PageHandlers/View.go
  5. 32
      PageHandlers/templates/base.html
  6. 17
      PageHandlers/templates/index.html
  7. 13
      PageHandlers/templates/search.html
  8. 21
      PageHandlers/templates/view.html
  9. 9
      main.go

18
PageHandlers/Index.go

@ -3,30 +3,16 @@ package PageHandlers
import ( import (
_ "embed" _ "embed"
"fmt" "fmt"
"html/template"
"net/http" "net/http"
"strconv" "strconv"
"ytdlp-viewer/DirectoryIndexers" "ytdlp-viewer/DirectoryIndexers"
) )
//go:embed templates/index.html
var indexTmplSource string
var indexTmpl *template.Template
type IndexPageData struct { type IndexPageData struct {
FileCount string FileCount string
Files map[string]DirectoryIndexers.VideoFile Files map[string]DirectoryIndexers.VideoFile
} }
func init() {
var err error
indexTmpl = template.New("index.tmpl")
indexTmpl, err = indexTmpl.Parse(indexTmplSource)
if err != nil {
fmt.Println(err)
}
}
func Index(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { func Index(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) {
FL.RLock() FL.RLock()
defer FL.RUnlock() defer FL.RUnlock()
@ -36,7 +22,9 @@ func Index(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndex
Files: FL.Files, Files: FL.Files,
} }
err := indexTmpl.Execute(writer, data) fmt.Println(tmpl["index.html"].Name())
err := tmpl["index.html"].ExecuteTemplate(writer, "base", data)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }

16
PageHandlers/Search.go

@ -3,7 +3,6 @@ package PageHandlers
import ( import (
_ "embed" _ "embed"
"fmt" "fmt"
"html/template"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
@ -11,19 +10,6 @@ import (
"ytdlp-viewer/DirectoryIndexers" "ytdlp-viewer/DirectoryIndexers"
) )
//go:embed templates/search.html
var searchTmplSource string
var searchTmpl *template.Template
func init() {
var err error
searchTmpl = template.New("search.tmpl")
searchTmpl, err = searchTmpl.Parse(searchTmplSource)
if err != nil {
fmt.Println(err)
}
}
type SearchPageData struct { type SearchPageData struct {
Results []DirectoryIndexers.VideoFile Results []DirectoryIndexers.VideoFile
ResultCount string ResultCount string
@ -59,7 +45,7 @@ func SearchHandler(writer http.ResponseWriter, request *http.Request, FL *Direct
SearchTerm: keys[0], SearchTerm: keys[0],
} }
err := searchTmpl.Execute(writer, data) err := tmpl["search.html"].ExecuteTemplate(writer, "base", data)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }

58
PageHandlers/Templates.go

@ -0,0 +1,58 @@
package PageHandlers
import (
_ "embed"
"fmt"
"html/template"
)
//go:embed templates/index.html
var indexTmplSource string
//go:embed templates/view.html
var viewTmplSource string
//go:embed templates/search.html
var searchTmplSource string
//go:embed templates/base.html
var baseTmplSource string
var tmpl map[string]*template.Template
func init() {
tmpl = make(map[string]*template.Template)
var err error
tmpl["index.html"] = template.New("index.html")
tmpl["index.html"], err = tmpl["index.html"].Parse(baseTmplSource)
if err != nil {
fmt.Println(err)
}
tmpl["index.html"], err = tmpl["index.html"].Parse(indexTmplSource)
if err != nil {
fmt.Println(err)
}
fmt.Println(tmpl["index.html"].Name())
tmpl["search.html"] = template.New("search.html")
tmpl["search.html"], err = tmpl["search.html"].Parse(searchTmplSource)
if err != nil {
fmt.Println(err)
}
tmpl["search.html"], err = tmpl["search.html"].Parse(baseTmplSource)
if err != nil {
fmt.Println(err)
}
tmpl["view.html"] = template.New("view.html")
tmpl["view.html"], err = tmpl["view.html"].Parse(viewTmplSource)
if err != nil {
fmt.Println(err)
}
tmpl["view.html"], err = tmpl["view.html"].Parse(baseTmplSource)
if err != nil {
fmt.Println(err)
}
}

16
PageHandlers/View.go

@ -3,16 +3,11 @@ package PageHandlers
import ( import (
_ "embed" _ "embed"
"fmt" "fmt"
"html/template"
"log" "log"
"net/http" "net/http"
"ytdlp-viewer/DirectoryIndexers" "ytdlp-viewer/DirectoryIndexers"
) )
//go:embed templates/view.html
var viewTmplSource string
var viewTmpl *template.Template
type ViewPageData struct { type ViewPageData struct {
Title string Title string
Filename string Filename string
@ -20,15 +15,6 @@ type ViewPageData struct {
Extension string Extension string
} }
func init() {
var err error
viewTmpl = template.New("view.tmpl")
viewTmpl, err = viewTmpl.Parse(viewTmplSource)
if err != nil {
fmt.Println(err)
}
}
func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) {
FL.RLock() FL.RLock()
defer FL.RUnlock() defer FL.RUnlock()
@ -51,7 +37,7 @@ func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexe
Extension: video.Extension, Extension: video.Extension,
} }
err := viewTmpl.Execute(writer, data) err := tmpl["view.html"].ExecuteTemplate(writer, "base", data)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }

32
PageHandlers/templates/base.html

@ -0,0 +1,32 @@
{{define "base"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
* {
color: white;
}
input{
color: black;
}
</style>
{{template "head" .}}
</head>
<body style="background-color: #212121">
<div style="background-color: #424242; padding: 8px; border-radius: 15px; margin-bottom: 50px">
<h1 style="float: left">yt-dlp Archive Viewer</h1>
<form action="/search" method="get" style="float: right; margin: auto">
<label>
Search term:
<input type="text" name="term" required>
</label>
</form>
<div style="clear:both;"></div>
</div>
<div style="padding: 10px">
{{template "body" .}}
</div>
</body>
</html>
{{end}}

17
PageHandlers/templates/index.html

@ -1,11 +1,9 @@
<!DOCTYPE html> {{define "head"}}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ytdlp-viewer | {{.FileCount}}</title> <title>ytdlp-viewer | {{.FileCount}}</title>
</head> {{end}}
<body>
<center> {{define "body"}}
<div style="text-align: center;">
<h1>yt-dlp Viewer</h1> <h1>yt-dlp Viewer</h1>
<h2>{{.FileCount}} files in this archive</h2> <h2>{{.FileCount}} files in this archive</h2>
@ -19,6 +17,5 @@
{{range .Files}} {{range .Files}}
<a href="/view?id={{.Id}}">{{.Id}} - {{.Title}}</a><br /> <a href="/view?id={{.Id}}">{{.Id}} - {{.Title}}</a><br />
{{end}} {{end}}
</center> </div>
</body> {{end}}
</html>

13
PageHandlers/templates/search.html

@ -1,10 +1,8 @@
<!DOCTYPE html> {{define "head"}}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Search result | {{.SearchTerm}}</title> <title>Search result | {{.SearchTerm}}</title>
</head> {{end}}
<body>
{{define "body"}}
<form action="/search" method="get"> <form action="/search" method="get">
<label> <label>
Search term: Search term:
@ -15,5 +13,4 @@
{{range .Results}} {{range .Results}}
<a href="/view?id={{.Id}}">{{.Id}} - {{.Title}}</a><br /> <a href="/view?id={{.Id}}">{{.Id}} - {{.Title}}</a><br />
{{end}} {{end}}
</body> {{end}}
</html>

21
PageHandlers/templates/view.html

@ -1,20 +1,14 @@
<!DOCTYPE html> {{define "head"}}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{.Title}} | {{.Id}}</title> <title>{{.Title}} | {{.Id}}</title>
<link href="https://vjs.zencdn.net/7.15.4/video-js.css" rel="stylesheet" /> <link href="https://vjs.zencdn.net/7.15.4/video-js.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/videojs-seek-buttons/dist/videojs-seek-buttons.css" rel="stylesheet"> <link href="https://cdn.jsdelivr.net/npm/videojs-seek-buttons/dist/videojs-seek-buttons.css" rel="stylesheet">
<style> <style>
.centeredvideo {
display: block;
margin-left: auto;
margin-right: auto
}
</style> </style>
</head> {{end}}
<body>
<video class="video-js vjs-big-play-centered centeredvideo" id="player" height="480" width="720" controls autoplay data-setup="{}"> {{define "body"}}
<video class="video-js vjs-big-play-centered centeredvideo" id="player" height="720" width="1280" controls autoplay data-setup="{}">
<source src="/videos/{{.Filename}}"> <source src="/videos/{{.Filename}}">
</video> </video>
<h1>{{.Title}}</h1> <h1>{{.Title}}</h1>
@ -33,5 +27,4 @@
back: 10 back: 10
}); });
</script> </script>
</body> {{end}}
</html>

9
main.go

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"time"
"ytdlp-viewer/DirectoryIndexers" "ytdlp-viewer/DirectoryIndexers"
"ytdlp-viewer/PageHandlers" "ytdlp-viewer/PageHandlers"
) )
@ -16,12 +17,17 @@ func main() {
} }
var FL DirectoryIndexers.FileList var FL DirectoryIndexers.FileList
go func() {
for {
resultChannel := make(chan DirectoryIndexers.FileList) resultChannel := make(chan DirectoryIndexers.FileList)
fmt.Println("Starting scanner at", path) fmt.Println("Starting scanner at", path)
go DirectoryIndexers.Index(path, resultChannel) go DirectoryIndexers.Index(path, resultChannel)
FL = <-resultChannel FL = <-resultChannel
time.Sleep(60 * time.Second)
}
}()
/*FL.RLock() /*FL.RLock()
fmt.Println(strconv.Itoa(len(FL.Files))) fmt.Println(strconv.Itoa(len(FL.Files)))
@ -31,12 +37,15 @@ func main() {
FL.RUnlock()*/ FL.RUnlock()*/
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
fmt.Println(time.Now().String(),request.URL)
PageHandlers.Index(writer, request, &FL) PageHandlers.Index(writer, request, &FL)
}) })
http.HandleFunc("/search", func(writer http.ResponseWriter, request *http.Request) { http.HandleFunc("/search", func(writer http.ResponseWriter, request *http.Request) {
fmt.Println(time.Now().String(),request.URL)
PageHandlers.SearchHandler(writer, request, &FL) PageHandlers.SearchHandler(writer, request, &FL)
}) })
http.HandleFunc("/view", func(writer http.ResponseWriter, request *http.Request) { http.HandleFunc("/view", func(writer http.ResponseWriter, request *http.Request) {
fmt.Println(time.Now().String(),request.URL)
PageHandlers.View(writer, request, &FL) PageHandlers.View(writer, request, &FL)
}) })
http.Handle("/videos/", http.StripPrefix("/videos/", http.FileServer(http.Dir(path)))) http.Handle("/videos/", http.StripPrefix("/videos/", http.FileServer(http.Dir(path))))

Loading…
Cancel
Save