Rework to use golang 1.16 embed package instead of go-bindata. Rework templates to be less insane
This commit is contained in:
parent
3a65a60fcb
commit
87acf0aefb
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,7 +2,6 @@ dist
|
||||
release
|
||||
discord-auto-upload
|
||||
discord-auto-upload.exe
|
||||
assets
|
||||
*.png
|
||||
*.jpg
|
||||
.DS_Store
|
||||
|
@ -33,7 +33,6 @@ foreach my $type (keys %build) {
|
||||
|
||||
add_extras();
|
||||
|
||||
system(qw{go generate});
|
||||
foreach my $type (keys %build) {
|
||||
local $ENV{GOOS} = $build{$type}->{env}->{GOOS};
|
||||
local $ENV{GOARCH} = $build{$type}->{env}->{GOARCH};
|
||||
|
2
dau.go
2
dau.go
@ -1,7 +1,5 @@
|
||||
package main
|
||||
|
||||
//go:generate go-bindata -pkg assets -o assets/static.go -prefix data/ data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module github.com/tardisx/discord-auto-upload
|
||||
|
||||
go 1.15
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/fogleman/gg v1.3.0
|
||||
|
@ -1,3 +1,4 @@
|
||||
{{ define "content" }}
|
||||
|
||||
<main role="main" class="inner DAU">
|
||||
<h1 class="DAU-heading">Config</h1>
|
||||
@ -99,8 +100,9 @@
|
||||
</form>
|
||||
|
||||
</main>
|
||||
{{ end }}
|
||||
|
||||
|
||||
{{ define "js" }}
|
||||
<script>
|
||||
function update_sadness () {
|
||||
if ($('#input-nowatermark').prop('checked')) {
|
||||
@ -154,3 +156,4 @@ $(document).ready(function() {
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{{ end }}
|
@ -1,4 +1,4 @@
|
||||
|
||||
{{ define "content" }}
|
||||
<main role="main" class="inner DAU">
|
||||
<h1 class="DAU-heading">Discord Auto Upload</h1>
|
||||
<p class="lead">Hey look, it's DAU :-)</p>
|
||||
@ -6,3 +6,7 @@
|
||||
<a href="https://github.com/tardisx/discord-auto-upload" class="btn btn-lg btn-secondary" target="_blank">Learn more</a>
|
||||
</p>
|
||||
</main>
|
||||
{{ end }}
|
||||
|
||||
{{ define "js" }}
|
||||
{{ end }}
|
@ -1,3 +1,4 @@
|
||||
{{ define "content" }}
|
||||
|
||||
<main role="main" class="inner DAU">
|
||||
<h1 class="DAU-heading">Logs</h1>
|
||||
@ -21,6 +22,10 @@
|
||||
</pre>
|
||||
</main>
|
||||
|
||||
{{ end }}
|
||||
|
||||
{{ define "js" }}
|
||||
|
||||
<script>
|
||||
var debug = 0;
|
||||
var scrl = true;
|
||||
@ -51,3 +56,6 @@ function get_logs() {
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
{{ end }}
|
@ -1,3 +1,5 @@
|
||||
{{ define "content" }}
|
||||
|
||||
<main role="main" class="inner DAU">
|
||||
<h1 class="DAU-heading">Uploads</h1>
|
||||
<p class="lead">Discord-auto-upload uploads</p>
|
||||
@ -12,6 +14,9 @@
|
||||
</table>
|
||||
</main>
|
||||
|
||||
{{ end }}
|
||||
|
||||
{{ define "js" }}
|
||||
<script>
|
||||
|
||||
$(document).ready(function() {
|
||||
@ -37,3 +42,4 @@ function get_uploads() {
|
||||
}
|
||||
|
||||
</script>
|
||||
{{ end }}
|
@ -1,3 +1,5 @@
|
||||
{{ define "layout" }}
|
||||
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
@ -46,7 +48,8 @@
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{{.Body}}
|
||||
{{ template "content" . }}
|
||||
|
||||
|
||||
<footer class="mastfoot mt-auto">
|
||||
<div class="inner">
|
||||
@ -57,4 +60,7 @@
|
||||
|
||||
|
||||
</body>
|
||||
{{ template "js" . }}
|
||||
</html>
|
||||
|
||||
{{ end }}
|
@ -1,24 +1,27 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"text/template"
|
||||
"strings"
|
||||
|
||||
"github.com/tardisx/discord-auto-upload/assets"
|
||||
"github.com/tardisx/discord-auto-upload/config"
|
||||
daulog "github.com/tardisx/discord-auto-upload/log"
|
||||
"github.com/tardisx/discord-auto-upload/uploads"
|
||||
"github.com/tardisx/discord-auto-upload/version"
|
||||
)
|
||||
|
||||
//go:embed data
|
||||
var webFS embed.FS
|
||||
|
||||
// DAUWebServer - stuff for the web server
|
||||
type DAUWebServer struct {
|
||||
ConfigChange chan int
|
||||
@ -40,44 +43,49 @@ type errorResponse struct {
|
||||
}
|
||||
|
||||
func getStatic(w http.ResponseWriter, r *http.Request) {
|
||||
// haha this is dumb and I should change it
|
||||
re := regexp.MustCompile(`[^a-zA-Z0-9\.]`)
|
||||
path := r.URL.Path[1:]
|
||||
sanitized_path := re.ReplaceAll([]byte(path), []byte("_"))
|
||||
|
||||
if string(sanitized_path) == "" {
|
||||
sanitized_path = []byte("index.html")
|
||||
path := r.URL.Path
|
||||
path = strings.TrimLeft(path, "/")
|
||||
if path == "" {
|
||||
path = "index.html"
|
||||
}
|
||||
|
||||
data, err := assets.Asset(string(sanitized_path))
|
||||
if err != nil {
|
||||
// Asset was not found.
|
||||
fmt.Fprintln(w, err)
|
||||
}
|
||||
extension := filepath.Ext(string(path))
|
||||
|
||||
extension := filepath.Ext(string(sanitized_path))
|
||||
|
||||
// is this a HTML file? if so wrap it in the template
|
||||
if extension == ".html" {
|
||||
wrapper, _ := assets.Asset("wrapper.tmpl")
|
||||
t := template.Must(template.New("wrapper").Parse(string(wrapper)))
|
||||
|
||||
t, err := template.ParseFS(webFS, "data/wrapper.tmpl", "data/"+path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Printf("req: %s", r.URL.Path)
|
||||
|
||||
var b struct {
|
||||
Body string
|
||||
Path string
|
||||
Version string
|
||||
}
|
||||
b.Body = string(data)
|
||||
b.Path = string(sanitized_path)
|
||||
b.Path = path
|
||||
b.Version = version.CurrentVersion
|
||||
t.Execute(w, b)
|
||||
|
||||
err = t.ExecuteTemplate(w, "layout", b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
} else {
|
||||
otherStatic, err := webFS.ReadFile("data/" + path)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("problem with '%s': %v", path, err)
|
||||
}
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(extension))
|
||||
|
||||
w.Write(otherStatic)
|
||||
return
|
||||
}
|
||||
|
||||
// otherwise we are a static thing
|
||||
w.Header().Set("Content-Type", mime.TypeByExtension(extension))
|
||||
|
||||
w.Write(data)
|
||||
//
|
||||
}
|
||||
|
||||
// TODO there should be locks around all these config accesses
|
||||
|
Loading…
x
Reference in New Issue
Block a user