go1.16 and embed

This commit is contained in:
ston1th 2021-02-18 22:36:49 +01:00
commit 8e7bcfc21b
37 changed files with 98 additions and 1134 deletions

View file

@ -11,16 +11,16 @@ all: $(PROGRAM)
setup:
$(CC) get github.com/client9/misspell
release: clean generate gofmt codeqa
release: clean gofmt codeqa
$(ENV) $(CC) $(BUILD) $(LDFLAGS)
release-vendor: clean generate gofmt codeqa
release-vendor: clean gofmt codeqa
$(ENV) $(CC) $(BUILD) -mod=vendor $(LDFLAGS)
vendor: clean generate gofmt
vendor: clean gofmt
$(ENV) $(CC) $(BUILD) -mod=vendor $(LDFLAGS)
$(PROGRAM): clean generate gofmt
$(PROGRAM): clean gofmt
$(ENV) $(CC) $(BUILD) $(GCFLAGS) $(LDFLAGS)
clean:
@ -28,9 +28,6 @@ clean:
codeqa: misspell golint test
generate:
$(CC) generate
gofmt:
gofmt -w pkg/ main.go
@ -51,4 +48,4 @@ else
$(info skipping tests of other platforms)
endif
.PHONY: setup release release-vendor vendor build clean codeqa generate gofmt golint misspell test
.PHONY: setup release release-vendor vendor build clean codeqa gofmt golint misspell test

2
go.mod
View file

@ -1,6 +1,6 @@
module git.giftfish.de/ston1th/docstore
go 1.13
go 1.16
require (
git.giftfish.de/ston1th/authdav v1.1.0

View file

@ -22,10 +22,7 @@ const (
TagsPrefix = "/tags"
DeletePrefix = "/delete"
WebDavPrefix = "/webdav"
Favicon = "/favicon.ico"
BootstrapCSS = "/bootstrap.css"
CustomCSS = "/custom.css"
StaticPrefix = "/static"
DataDir = "data"
)

View file

@ -37,9 +37,7 @@ var reserved = []string{
core.MovePrefix,
core.DeletePrefix,
core.WebDavPrefix,
core.Favicon,
core.BootstrapCSS,
core.CustomCSS,
core.StaticPrefix,
}
func checkPath(path string) error {

View file

@ -97,24 +97,6 @@ func authHandler(h ctxHandler) ctxHandler {
}
}
func staticHandler(ctx *Context) {
var err error
ctx.SetHeader("Content-Type", "text/css; charset=utf-8")
ctx.SetHeader("Expires", time.Now().UTC().Add(max).Format(http.TimeFormat))
switch ctx.Path() {
case core.BootstrapCSS:
err = ctx.Write(ctx.Srv.res["bootstrap.css"])
case core.CustomCSS:
err = ctx.Write(ctx.Srv.res["custom.css"])
case core.Favicon:
ctx.SetHeader("Content-Type", "image/x-icon")
err = ctx.Write(ctx.Srv.res["favicon.ico"])
}
if err != nil {
log.Println("static:", err)
}
}
type registerNotFoundHandler struct {
s *HTTPServer
}

View file

@ -10,31 +10,15 @@ type route struct {
Methods []string
}
var static = []route{
var registerRoutes = []route{
{
core.Favicon,
staticHandler,
[]string{"GET"},
},
{
core.BootstrapCSS,
staticHandler,
[]string{"GET"},
},
{
core.CustomCSS,
staticHandler,
[]string{"GET"},
core.IndexURI,
jwtRegisterHandler(
registerHandler),
[]string{"GET", "POST"},
},
}
var registerRoutes = append(static, route{
core.IndexURI,
jwtRegisterHandler(
registerHandler),
[]string{"GET", "POST"},
})
var prefixRoutes = []route{
{
core.RawPrefix,
@ -94,7 +78,7 @@ var prefixRoutes = []route{
},
}
var routes = append(static, []route{
var routes = []route{
{
core.SearchURI,
jwtHandler(
@ -177,4 +161,4 @@ var routes = append(static, []route{
logoutHandler),
[]string{"GET", "POST"},
},
}...)
}

View file

@ -5,6 +5,7 @@ package server
import (
"bytes"
"context"
"embed"
"errors"
"git.giftfish.de/ston1th/authdav"
"git.giftfish.de/ston1th/docstore/pkg/core"
@ -100,12 +101,16 @@ func (s *HTTPServer) Stop() error {
func (s *HTTPServer) register() http.Handler {
r := mux.NewRouter()
r.NotFoundHandler = &registerNotFoundHandler{s}
r.Handle("/static/{file}", http.StripPrefix("/", http.FileServer(http.FS(static))))
for _, v := range registerRoutes {
r.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
}
return r
}
//go:embed static/*
var static embed.FS
func (s *HTTPServer) buildRoutes() http.Handler {
r := mux.NewRouter()
r.NotFoundHandler = &notFoundHandler{s}
@ -115,6 +120,7 @@ func (s *HTTPServer) buildRoutes() http.Handler {
h := authdav.NewWebdavBasicAuth(core.WebDavPrefix, fs, nil, webdavLogger, s.DB, "DocStore WebDav")
r.PathPrefix(core.WebDavPrefix).Handler(h)
}
r.Handle("/static/{file}", http.StripPrefix("/", http.FileServer(http.FS(static))))
for _, v := range routes {
r.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
}

View file

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

Before After
Before After

File diff suppressed because one or more lines are too long

View file

@ -2,9 +2,9 @@
<html lang="en">
<head>
<title>DocStore | {{.Title}}</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" integrity="sha256-bgotk/IPq4Yvt6s8AQGPTM5ZjPjQ6rrBRa2EyxpnFSc="></link>
<link rel="stylesheet" type="text/css" href="/bootstrap.css" media="screen" integrity="sha256-3YeveuySvBgp2GItUS8eV1R/9T1zcPMyvw22SyVf8qo="></link>
<link rel="stylesheet" type="text/css" href="/custom.css" media="screen" integrity="sha256-wLiyjr/D8ZaaIPLjUmZ3zdm45/HW/cy4kCVTViwkPeI="></link>
<link rel="shortcut icon" type="image/x-icon" href="/static/favicon.ico" integrity="sha256-bgotk/IPq4Yvt6s8AQGPTM5ZjPjQ6rrBRa2EyxpnFSc="></link>
<link rel="stylesheet" type="text/css" href="/static/bootstrap.css" media="screen" integrity="sha256-3YeveuySvBgp2GItUS8eV1R/9T1zcPMyvw22SyVf8qo="></link>
<link rel="stylesheet" type="text/css" href="/static/custom.css" media="screen" integrity="sha256-wLiyjr/D8ZaaIPLjUmZ3zdm45/HW/cy4kCVTViwkPeI="></link>
<meta name="theme-color" content="#375a7f">
<meta name="description" content="{{.Title}}">
</head>

View file

@ -1,113 +0,0 @@
#!/bin/bash
# Copyright (C) 2019 Marius Schellenberger
# this file generates Go source code for the gowiki
# it contains html and css and uses the contents
# of the templates directory
templates_go="pkg/server/templates.go"
echo "Generating ${templates_go}"
if [ ! -d templates ]; then
echo "error: templates directory not found" 1>&2
exit 2
fi
cat << EOF > ${templates_go}
// Copyright (C) 2019 Marius Schellenberger
// This file is auto-generated by templates.sh
// using the contents of the templates directory
package server
import (
"encoding/base64"
"errors"
"git.giftfish.de/ston1th/docstore/pkg/log"
"html/template"
)
EOF
echo "const (" >> ${templates_go}
for f in $(ls templates/*.html); do
fname=${f/templates\//}
fname=${fname/.html/}
echo " ${fname} = \`$(cat ${f})\`" >> ${templates_go}
done
for f in $(ls templates/*.css); do
fname=${f/templates\//}
fname=${fname/.css/CSS}
echo " ${fname} = \`$(cat ${f})\`" >> ${templates_go}
done
echo " favicon = \"$(base64 -w 0 templates/favicon.ico)\"" >> ${templates_go}
cat << EOF >> ${templates_go}
)
func (s *HTTPServer) loadTemplates() {
var fatal bool
s.templ = make(map[string]*template.Template)
s.res = make(map[string][]byte)
parse := func(html ...string) (temp *template.Template) {
var err error
temp = template.New("")
for _, s := range html {
temp, err = temp.Parse(s)
if err != nil {
log.Println("parse:", err)
fatal = true
return
}
}
return
}
// resources
s.res["bootstrap.css"] = []byte(bootstrapCSS)
s.res["custom.css"] = []byte(customCSS)
fav, err := base64.StdEncoding.DecodeString(favicon)
if err != nil {
log.Println("favicon:", err)
fatal = true
}
s.res["favicon.ico"] = fav
// register
s.templ["registerHandler"] = parse(index, menu, register)
s.templ["dirHandler"] = parse(index, menu, dir)
s.templ["fileHandler"] = parse(index, menu, file)
s.templ["uploadHandler"] = parse(index, menu, upload)
s.templ["newDirHandler"] = parse(index, menu, newDir)
s.templ["moveHandler"] = parse(index, menu, move)
s.templ["enoentHandler"] = parse(index, menu, enoent)
s.templ["deleteHandler"] = parse(index, menu, delete)
s.templ["notFoundHandler"] = parse(index, menu, notFound)
s.templ["forbiddenHandler"] = parse(index, menu, forbidden)
s.templ["iseHandler"] = parse(index, menu, ise)
s.templ["loginHandler"] = parse(index, menu, login)
s.templ["loginTotpHandler"] = parse(index, menu, loginTotp)
s.templ["searchHandler"] = parse(index, menu, search)
s.templ["userEditHandler"] = parse(index, menu, userEdit)
// totp
s.templ["userTotpHandler"] = parse(index, menu, userTotp)
// tags
s.templ["tagsHandler"] = parse(index, menu, tagsList)
s.templ["tagsNewHandler"] = parse(index, menu, tagsNew)
s.templ["tagsEditHandler"] = parse(index, menu, tagsEdit)
s.templ["tagsDelHandler"] = parse(index, menu, tagsDel)
// stats
s.templ["statsHandler"] = parse(index, menu, stats)
// logs
s.templ["logsHandler"] = parse(index, menu, logs)
if fatal {
log.Fatal("parse: ", errors.New("parsing templates failed"))
}
}
EOF

39
vendor/modules.txt vendored
View file

@ -1,14 +1,20 @@
# git.giftfish.de/ston1th/authdav v1.1.0
## explicit
git.giftfish.de/ston1th/authdav
# git.giftfish.de/ston1th/godrop/v2 v2.1.0
## explicit
git.giftfish.de/ston1th/godrop/v2
# git.giftfish.de/ston1th/jwt/v3 v3.2.0
## explicit
git.giftfish.de/ston1th/jwt/v3
# github.com/RoaringBitmap/roaring v0.5.5
## explicit
github.com/RoaringBitmap/roaring
# github.com/bcampbell/qs v0.0.0-20161004022730-15caa99abd01
## explicit
github.com/bcampbell/qs
# github.com/blevesearch/bleve v1.0.12
## explicit
github.com/blevesearch/bleve
github.com/blevesearch/bleve/analysis
github.com/blevesearch/bleve/analysis/analyzer/custom
@ -75,46 +81,79 @@ github.com/couchbase/vellum/levenshtein
github.com/couchbase/vellum/regexp
github.com/couchbase/vellum/utf8
# github.com/cpuguy83/go-md2man/v2 v2.0.0
## explicit
github.com/cpuguy83/go-md2man/v2/md2man
# github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d
## explicit
# github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548
## explicit
# github.com/cznic/strutil v0.0.0-20181122101858-275e90344537
## explicit
# github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51
## explicit
# github.com/facebookgo/stack v0.0.0-20160209184415-751773369052
## explicit
# github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870
## explicit
# github.com/glycerine/go-unsnap-stream v0.0.0-20190901134440-81cf024a9e0a
## explicit
github.com/glycerine/go-unsnap-stream
# github.com/golang/protobuf v1.4.3
## explicit
github.com/golang/protobuf/proto
# github.com/golang/snappy v0.0.2
## explicit
github.com/golang/snappy
# github.com/gorilla/mux v1.8.0
## explicit
github.com/gorilla/mux
# github.com/jmhodges/levigo v1.0.0
## explicit
# github.com/mschoch/smat v0.2.0
github.com/mschoch/smat
# github.com/philhofer/fwd v1.1.0
github.com/philhofer/fwd
# github.com/pquerna/otp v1.3.0
## explicit
github.com/pquerna/otp
github.com/pquerna/otp/hotp
github.com/pquerna/otp/totp
# github.com/remyoudompheng/bigfft v0.0.0-20190321074620-2f0d2b0e0001
## explicit
# github.com/russross/blackfriday/v2 v2.1.0
## explicit
github.com/russross/blackfriday/v2
# github.com/steveyen/gtreap v0.1.0
github.com/steveyen/gtreap
# github.com/tecbot/gorocksdb v0.0.0-20181010114359-8752a9433481
## explicit
# github.com/tinylib/msgp v1.1.4
## explicit
github.com/tinylib/msgp/msgp
# github.com/urfave/cli v1.22.5
## explicit
github.com/urfave/cli
# github.com/willf/bitset v1.1.11
## explicit
github.com/willf/bitset
# go.etcd.io/bbolt v1.3.5
## explicit
go.etcd.io/bbolt
# golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9
## explicit
golang.org/x/crypto/bcrypt
golang.org/x/crypto/blowfish
# golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
## explicit
golang.org/x/net/webdav
golang.org/x/net/webdav/internal/xml
# golang.org/x/sys v0.0.0-20201112073958-5cba982894dd
## explicit
golang.org/x/sys/internal/unsafeheader
golang.org/x/sys/unix
golang.org/x/sys/windows
# google.golang.org/protobuf v1.25.0
## explicit
google.golang.org/protobuf/encoding/prototext
google.golang.org/protobuf/encoding/protowire
google.golang.org/protobuf/internal/descfmt