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
|
release
|
||||||
discord-auto-upload
|
discord-auto-upload
|
||||||
discord-auto-upload.exe
|
discord-auto-upload.exe
|
||||||
assets
|
|
||||||
*.png
|
*.png
|
||||||
*.jpg
|
*.jpg
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
@ -33,7 +33,6 @@ foreach my $type (keys %build) {
|
|||||||
|
|
||||||
add_extras();
|
add_extras();
|
||||||
|
|
||||||
system(qw{go generate});
|
|
||||||
foreach my $type (keys %build) {
|
foreach my $type (keys %build) {
|
||||||
local $ENV{GOOS} = $build{$type}->{env}->{GOOS};
|
local $ENV{GOOS} = $build{$type}->{env}->{GOOS};
|
||||||
local $ENV{GOARCH} = $build{$type}->{env}->{GOARCH};
|
local $ENV{GOARCH} = $build{$type}->{env}->{GOARCH};
|
||||||
|
2
dau.go
2
dau.go
@ -1,7 +1,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
//go:generate go-bindata -pkg assets -o assets/static.go -prefix data/ data
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
|||||||
module github.com/tardisx/discord-auto-upload
|
module github.com/tardisx/discord-auto-upload
|
||||||
|
|
||||||
go 1.15
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/fogleman/gg v1.3.0
|
github.com/fogleman/gg v1.3.0
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
{{ define "content" }}
|
||||||
|
|
||||||
<main role="main" class="inner DAU">
|
<main role="main" class="inner DAU">
|
||||||
<h1 class="DAU-heading">Config</h1>
|
<h1 class="DAU-heading">Config</h1>
|
||||||
@ -99,8 +100,9 @@
|
|||||||
</form>
|
</form>
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "js" }}
|
||||||
<script>
|
<script>
|
||||||
function update_sadness () {
|
function update_sadness () {
|
||||||
if ($('#input-nowatermark').prop('checked')) {
|
if ($('#input-nowatermark').prop('checked')) {
|
||||||
@ -154,3 +156,4 @@ $(document).ready(function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
{{ end }}
|
@ -1,4 +1,4 @@
|
|||||||
|
{{ define "content" }}
|
||||||
<main role="main" class="inner DAU">
|
<main role="main" class="inner DAU">
|
||||||
<h1 class="DAU-heading">Discord Auto Upload</h1>
|
<h1 class="DAU-heading">Discord Auto Upload</h1>
|
||||||
<p class="lead">Hey look, it's DAU :-)</p>
|
<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>
|
<a href="https://github.com/tardisx/discord-auto-upload" class="btn btn-lg btn-secondary" target="_blank">Learn more</a>
|
||||||
</p>
|
</p>
|
||||||
</main>
|
</main>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "js" }}
|
||||||
|
{{ end }}
|
@ -1,3 +1,4 @@
|
|||||||
|
{{ define "content" }}
|
||||||
|
|
||||||
<main role="main" class="inner DAU">
|
<main role="main" class="inner DAU">
|
||||||
<h1 class="DAU-heading">Logs</h1>
|
<h1 class="DAU-heading">Logs</h1>
|
||||||
@ -21,6 +22,10 @@
|
|||||||
</pre>
|
</pre>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "js" }}
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
var debug = 0;
|
var debug = 0;
|
||||||
var scrl = true;
|
var scrl = true;
|
||||||
@ -51,3 +56,6 @@ function get_logs() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
{{ end }}
|
@ -1,3 +1,5 @@
|
|||||||
|
{{ define "content" }}
|
||||||
|
|
||||||
<main role="main" class="inner DAU">
|
<main role="main" class="inner DAU">
|
||||||
<h1 class="DAU-heading">Uploads</h1>
|
<h1 class="DAU-heading">Uploads</h1>
|
||||||
<p class="lead">Discord-auto-upload uploads</p>
|
<p class="lead">Discord-auto-upload uploads</p>
|
||||||
@ -12,6 +14,9 @@
|
|||||||
</table>
|
</table>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "js" }}
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
@ -37,3 +42,4 @@ function get_uploads() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
{{ end }}
|
@ -1,3 +1,5 @@
|
|||||||
|
{{ define "layout" }}
|
||||||
|
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
@ -46,7 +48,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
{{.Body}}
|
{{ template "content" . }}
|
||||||
|
|
||||||
|
|
||||||
<footer class="mastfoot mt-auto">
|
<footer class="mastfoot mt-auto">
|
||||||
<div class="inner">
|
<div class="inner">
|
||||||
@ -57,4 +60,7 @@
|
|||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
{{ template "js" . }}
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
{{ end }}
|
@ -1,24 +1,27 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"text/template"
|
"strings"
|
||||||
|
|
||||||
"github.com/tardisx/discord-auto-upload/assets"
|
|
||||||
"github.com/tardisx/discord-auto-upload/config"
|
"github.com/tardisx/discord-auto-upload/config"
|
||||||
daulog "github.com/tardisx/discord-auto-upload/log"
|
daulog "github.com/tardisx/discord-auto-upload/log"
|
||||||
"github.com/tardisx/discord-auto-upload/uploads"
|
"github.com/tardisx/discord-auto-upload/uploads"
|
||||||
"github.com/tardisx/discord-auto-upload/version"
|
"github.com/tardisx/discord-auto-upload/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed data
|
||||||
|
var webFS embed.FS
|
||||||
|
|
||||||
// DAUWebServer - stuff for the web server
|
// DAUWebServer - stuff for the web server
|
||||||
type DAUWebServer struct {
|
type DAUWebServer struct {
|
||||||
ConfigChange chan int
|
ConfigChange chan int
|
||||||
@ -40,44 +43,49 @@ type errorResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getStatic(w http.ResponseWriter, r *http.Request) {
|
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) == "" {
|
path := r.URL.Path
|
||||||
sanitized_path = []byte("index.html")
|
path = strings.TrimLeft(path, "/")
|
||||||
|
if path == "" {
|
||||||
|
path = "index.html"
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := assets.Asset(string(sanitized_path))
|
extension := filepath.Ext(string(path))
|
||||||
if err != nil {
|
|
||||||
// Asset was not found.
|
|
||||||
fmt.Fprintln(w, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
extension := filepath.Ext(string(sanitized_path))
|
|
||||||
|
|
||||||
// is this a HTML file? if so wrap it in the template
|
|
||||||
if extension == ".html" {
|
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 {
|
var b struct {
|
||||||
Body string
|
Body string
|
||||||
Path string
|
Path string
|
||||||
Version string
|
Version string
|
||||||
}
|
}
|
||||||
b.Body = string(data)
|
b.Path = path
|
||||||
b.Path = string(sanitized_path)
|
|
||||||
b.Version = version.CurrentVersion
|
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
|
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
|
// TODO there should be locks around all these config accesses
|
||||||
|
Loading…
x
Reference in New Issue
Block a user