Add auto reload
- reload markdown if changed - allow to monitor other files if wanted - disable HTTP cache to force reload - extend example - replace almost everything with layered file systems
This commit is contained in:
parent
8a977bad26
commit
bd27081aa6
20 changed files with 489 additions and 177 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -18,6 +18,9 @@
|
|||
.vscode/
|
||||
.history
|
||||
|
||||
# CI
|
||||
bin/
|
||||
|
||||
assets/reveal
|
||||
|
||||
pkged.go
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
project_name: goveal
|
||||
before:
|
||||
hooks:
|
||||
- task test
|
||||
builds:
|
||||
- main: ./cmd/goveal/main.go
|
||||
binary: goveal
|
||||
|
|
16
Taskfile.yml
16
Taskfile.yml
|
@ -30,6 +30,12 @@ tasks:
|
|||
- go mod download
|
||||
- go mod tidy
|
||||
|
||||
assets:
|
||||
sources:
|
||||
- ./assets/**/*
|
||||
cmds:
|
||||
- pkger
|
||||
|
||||
test:
|
||||
sources:
|
||||
- ./**/*.go
|
||||
|
@ -55,23 +61,26 @@ tasks:
|
|||
build:
|
||||
deps:
|
||||
- deps
|
||||
- assets
|
||||
sources:
|
||||
- ./**/*.go
|
||||
cmds:
|
||||
- pkger
|
||||
- go build {{ .GO_BUILD_ARGS }} -o {{ .BINARY_NAME }} {{ .CMD_PACKAGE }}
|
||||
|
||||
run:
|
||||
deps:
|
||||
- deps
|
||||
- assets
|
||||
cmds:
|
||||
- go run {{ .CMD_PACKAGE }} serve ./examples/slides.md
|
||||
- go run {{ .CMD_PACKAGE }} serve ./examples/slides.md --config ./examples/goveal.yaml
|
||||
|
||||
debug:
|
||||
cmds:
|
||||
- dlv debug --headless --listen=127.10.10.2:{{ .DEBUG_PORT }} --api-version=2 {{ .CMD_PACKAGE }} --build-flags="-tags debug" -- serve --config ./examples/goveal.yaml ./examples/slides.md
|
||||
|
||||
snapshot-release:
|
||||
deps:
|
||||
- assets
|
||||
cmds:
|
||||
- goreleaser --snapshot --skip-publish --rm-dist
|
||||
|
||||
|
@ -83,8 +92,9 @@ tasks:
|
|||
cmds:
|
||||
- rm -rf ./assets/reveal
|
||||
- mkdir -p ./assets/reveal
|
||||
- curl -sL https://github.com/hakimel/reveal.js/archive/{{ .REVEALJS_VERSION }}.tar.gz | tar -xvz --strip-components=1 -C ./assets/reveal --wildcards "*.js" --wildcards "*.css" --wildcards "*.html" --wildcards "*.woff" --wildcards "*.ttf" --exclude "test" --exclude "gruntfile.js" --exclude "examples/*.html"
|
||||
- curl -sL https://github.com/hakimel/reveal.js/archive/{{ .REVEALJS_VERSION }}.tar.gz | tar -xvz --strip-components=1 -C ./assets/reveal --wildcards "*.js" --wildcards "*.css" --wildcards "*.html" --wildcards "*.woff" --wildcards "*.ttf" --exclude "test" --exclude "gulpfile.js" --exclude "gruntfile.js" --exclude "demo.html" --exclude "index.html" --exclude "examples/*.html"
|
||||
- mkdir -p ./assets/reveal/plugin/menu ./assets/reveal/plugin/mouse-pointer
|
||||
- git clone https://github.com/denehyg/reveal.js-menu.git ./assets/reveal/plugin/menu
|
||||
- curl -L -o ./assets/reveal/plugin/mouse-pointer/mouse-pointer.js https://raw.githubusercontent.com/caiofcm/plugin-revealjs-mouse-pointer/master/mouse-pointer.js
|
||||
- rm -f ./assets/reveal/plugin/menu/{bower.json,CONTRIBUTING.md,LICENSE,package.json,README.md,.gitignore,gulpfile.js,package-lock.json}
|
||||
- curl -L https://github.com/highlightjs/highlight.js/archive/10.4.0.tar.gz | tar -xvz --strip-components=3 -C ./assets/reveal/plugin/highlight --wildcards "*.css" highlight.js-10.4.0/src/styles/
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
<script src="/plugin/highlight/highlight.js"></script>
|
||||
<script src="/plugin/notes/notes.js"></script>
|
||||
<script src="/plugin/menu/menu.js"></script>
|
||||
<script src="/js/reload.js"></script>
|
||||
|
||||
<script>
|
||||
Reveal.initialize({
|
||||
|
@ -87,6 +88,14 @@
|
|||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ if .Reveal.FilesToMonitor }}
|
||||
{{ range $idx, $file := .Reveal.FilesToMonitor }}
|
||||
subscribeForUpdates("/{{ trimPrefix "/" $file }}")
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
subscribeForUpdates("/content.md")
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
|
24
assets/web/js/reload.js
Normal file
24
assets/web/js/reload.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
let knownHashes = {}
|
||||
|
||||
function getLatestHash(path) {
|
||||
let request = new XMLHttpRequest()
|
||||
request.open("GET", `/hash/md5${path}`)
|
||||
|
||||
request.onload = () => {
|
||||
if(request.status === 200) {
|
||||
let hashResp = JSON.parse(request.responseText)
|
||||
if(path in knownHashes && knownHashes[path] !== hashResp["Hash"]) {
|
||||
window.location.reload()
|
||||
} else {
|
||||
knownHashes[path] = hashResp["Hash"]
|
||||
}
|
||||
}
|
||||
}
|
||||
request.send()
|
||||
}
|
||||
|
||||
function subscribeForUpdates(path) {
|
||||
setInterval(() => {
|
||||
getLatestHash(path)
|
||||
}, 1000)
|
||||
}
|
|
@ -5,3 +5,5 @@ vertical-separator: ---
|
|||
transition: fade
|
||||
stylesheets:
|
||||
- examples/custom.css
|
||||
filesToMonitor:
|
||||
- examples/**/*.css
|
10
go.mod
10
go.mod
|
@ -3,10 +3,20 @@ module github.com/baez90/go-reveal-slides
|
|||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/Masterminds/goutils v1.1.0 // indirect
|
||||
github.com/Masterminds/semver v1.5.0 // indirect
|
||||
github.com/Masterminds/sprig v2.22.0+incompatible
|
||||
github.com/bmatcuk/doublestar/v2 v2.0.4
|
||||
github.com/fsnotify/fsnotify v1.4.9
|
||||
github.com/gobuffalo/here v0.6.2 // indirect
|
||||
github.com/google/uuid v1.1.2 // indirect
|
||||
github.com/huandu/xstrings v1.3.2 // indirect
|
||||
github.com/imdario/mergo v0.3.11 // indirect
|
||||
github.com/markbates/pkger v0.17.1
|
||||
github.com/mitchellh/copystructure v1.0.0 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/sirupsen/logrus v1.7.0
|
||||
github.com/spf13/cobra v1.1.1
|
||||
github.com/spf13/viper v1.7.1
|
||||
go.uber.org/multierr v1.6.0
|
||||
)
|
||||
|
|
32
go.sum
32
go.sum
|
@ -14,6 +14,12 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
|
|||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
|
||||
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
|
||||
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
|
||||
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
|
||||
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
|
||||
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
|
||||
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
|
@ -24,6 +30,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
|
|||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
|
||||
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
|
||||
github.com/bmatcuk/doublestar/v2 v2.0.4 h1:6I6oUiT/sU27eE2OFcWqBhL1SwjyvQuOssxT4a1yidI=
|
||||
github.com/bmatcuk/doublestar/v2 v2.0.4/go.mod h1:QMmcs3H2AUQICWhfzLXz+IYln8lRQmTZRptLie8RgRw=
|
||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
|
||||
|
@ -50,6 +58,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
|
|||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
|
||||
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
|
||||
github.com/gobuffalo/here v0.6.2 h1:ZtCqC7F9ou3moLbYfHM1Tj+gwHGgWhjyRjVjsir9BE0=
|
||||
github.com/gobuffalo/here v0.6.2/go.mod h1:D75Sq0p2BVHdgQu3vCRsXbg85rx943V19urJpqAVWjI=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
|
@ -68,6 +78,8 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi
|
|||
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
|
||||
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
|
||||
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
||||
|
@ -78,10 +90,12 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
|
|||
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
|
||||
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
|
||||
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
|
||||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
|
||||
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
|
||||
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
|
||||
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
|
||||
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
||||
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
|
||||
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
|
||||
|
@ -97,6 +111,10 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
|
|||
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
|
||||
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
|
||||
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
|
||||
github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
|
||||
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
|
||||
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
|
||||
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||
|
@ -111,6 +129,7 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
|
|||
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
|
@ -123,6 +142,8 @@ github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
|
|||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
|
||||
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
|
||||
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
|
||||
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
|
@ -132,6 +153,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
|
|||
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
|
||||
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
|
||||
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
|
||||
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
|
@ -189,6 +212,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
|||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
|
||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
|
@ -197,12 +221,17 @@ go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
|||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
|
||||
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
|
||||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
|
@ -311,6 +340,7 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks
|
|||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
|
||||
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
|
@ -324,6 +354,8 @@ gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
|
|||
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
|
|
@ -18,7 +18,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/baez90/go-reveal-slides/internal/app/config"
|
||||
"github.com/baez90/go-reveal-slides/internal/app/rendering"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
|
@ -48,7 +48,7 @@ var (
|
|||
It is running a small web server that loads your markdown file, renders a complete HTML page and delivers it including all the reveal.js assets.
|
||||
It is not required to restart the server when you edit the markdown - a simple reload of the page is doing all the required magic.`,
|
||||
}
|
||||
params config.RevealParams
|
||||
params rendering.RevealParams
|
||||
)
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
|
|
|
@ -18,10 +18,10 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
|
||||
"github.com/baez90/go-reveal-slides/internal/app/rendering"
|
||||
"github.com/baez90/go-reveal-slides/internal/app/routing"
|
||||
"github.com/markbates/pkger"
|
||||
"github.com/baez90/go-reveal-slides/internal/app/server"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -30,49 +30,35 @@ import (
|
|||
var (
|
||||
host string
|
||||
port uint16
|
||||
openBrowser bool
|
||||
serveCmd = &cobra.Command{
|
||||
Use: "serve",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Short: "",
|
||||
Long: ``,
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
var srv *server.HTTPServer
|
||||
srv, err = server.NewHTTPServer(server.Config{
|
||||
Host: host,
|
||||
Port: port,
|
||||
MarkdownPath: args[0],
|
||||
RevealParams: ¶ms,
|
||||
})
|
||||
|
||||
router := &routing.RegexpRouter{}
|
||||
var tmplRenderer rendering.RevealRenderer
|
||||
if tmplRenderer, err = rendering.NewRevealRenderer(¶ms); err != nil {
|
||||
log.Errorf("Failed to initialize reveal renderer due to error: %v", err)
|
||||
if err != nil {
|
||||
log.Errorf("Error while setting up server: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Info("Setup template renderer")
|
||||
if err = router.AddRule(`^(/(index.html(l)?)?)?$`, tmplRenderer); err != nil {
|
||||
return
|
||||
listenUrl := fmt.Sprintf("http://%s/", srv.ListenAddress())
|
||||
log.Infof("Going to listen on %s", listenUrl)
|
||||
|
||||
if openBrowser {
|
||||
log.Info("Opening browser...")
|
||||
openBrowserInBackground(listenUrl)
|
||||
}
|
||||
|
||||
var markdownHandler rendering.MarkdownHandler
|
||||
if markdownHandler, err = rendering.NewMarkdownHandler(args[0]); err != nil {
|
||||
log.Errorf("Failed to initialize reveal renderer due to error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// single file handler that only delivers the single Markdown file containing the slides
|
||||
log.Info("Setup markdown handler for any *.md file...")
|
||||
if err = router.AddRule(".*\\.md$", markdownHandler); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
layeredHandler := &routing.LayeredHandler{}
|
||||
layeredHandler.AddHandlers(pkger.Dir("/assets/reveal"), http.Dir("."))
|
||||
|
||||
log.Info("Setup local file system and Reveal assets under / route")
|
||||
if err = router.AddRule("/.+", layeredHandler); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// start HTTP server
|
||||
hostPort := fmt.Sprintf("%s:%d", host, port)
|
||||
log.Infof("Running at addr http://%s/", hostPort)
|
||||
if err = http.ListenAndServe(hostPort, router); err != nil && errors.Is(err, http.ErrServerClosed) {
|
||||
if err = srv.Serve(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Errorf("Error while running serve command: %v", err)
|
||||
}
|
||||
return nil
|
||||
|
@ -85,4 +71,23 @@ func init() {
|
|||
|
||||
serveCmd.Flags().StringVar(&host, "host", "localhost", "host the CLI should listen on")
|
||||
serveCmd.Flags().Uint16Var(&port, "port", 2233, "port the CLI should listen on")
|
||||
serveCmd.Flags().BoolVar(&openBrowser, "open-browser", true, "if the browser should be opened at the URL")
|
||||
}
|
||||
|
||||
func openBrowserInBackground(url string) {
|
||||
var err error
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
err = exec.Command("xdg-open", url).Start()
|
||||
case "windows":
|
||||
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
|
||||
case "darwin":
|
||||
err = exec.Command("open", url).Start()
|
||||
default:
|
||||
err = fmt.Errorf("unsupported platform")
|
||||
}
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
// Copyright © 2019 Peter Kurfer peter.kurfer@googlemail.com
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type RevealParams struct {
|
||||
Theme string
|
||||
CodeTheme string
|
||||
Transition string
|
||||
NavigationMode string
|
||||
HorizontalSeparator string
|
||||
VerticalSeparator string
|
||||
SlideNumberVisibility string
|
||||
SlideNumberFormat string
|
||||
StyleSheets []string
|
||||
}
|
||||
|
||||
func (params *RevealParams) Load() {
|
||||
params.Theme = viper.GetString("theme")
|
||||
params.CodeTheme = viper.GetString("code-theme")
|
||||
params.Transition = viper.GetString("transition")
|
||||
params.NavigationMode = viper.GetString("navigationMode")
|
||||
params.HorizontalSeparator = viper.GetString("horizontal-separator")
|
||||
params.VerticalSeparator = viper.GetString("vertical-separator")
|
||||
params.StyleSheets = viper.GetStringSlice("stylesheets")
|
||||
params.SlideNumberVisibility = viper.GetString("slide-number-visibility")
|
||||
params.SlideNumberFormat = viper.GetString("slide-number-format")
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
// Copyright © 2019 Peter Kurfer peter.kurfer@googlemail.com
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package rendering
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
type MarkdownHandler interface {
|
||||
http.Handler
|
||||
}
|
||||
|
||||
func NewMarkdownHandler(markdownPath string) (handler MarkdownHandler, err error) {
|
||||
var info os.FileInfo
|
||||
info, err = os.Stat(markdownPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if info.IsDir() || path.Ext(info.Name()) != ".md" {
|
||||
err = fmt.Errorf("path %s did not pass sanity checks for markdown files", markdownPath)
|
||||
return
|
||||
}
|
||||
|
||||
handler = &markdownHandler{
|
||||
markdownPath: markdownPath,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type markdownHandler struct {
|
||||
markdownPath string
|
||||
}
|
||||
|
||||
func (handler *markdownHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
||||
http.ServeFile(response, request, handler.markdownPath)
|
||||
}
|
53
internal/app/rendering/reveal_params.go
Normal file
53
internal/app/rendering/reveal_params.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
// Copyright © 2019 Peter Kurfer peter.kurfer@googlemail.com
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package rendering
|
||||
|
||||
import (
|
||||
"github.com/bmatcuk/doublestar/v2"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type RevealParams struct {
|
||||
Theme string `mapstructure:"theme"`
|
||||
CodeTheme string `mapstructure:"code-theme"`
|
||||
Transition string `mapstructure:"transition"`
|
||||
NavigationMode string `mapstructure:"navigationMode"`
|
||||
HorizontalSeparator string `mapstructure:"horizontal-separator"`
|
||||
VerticalSeparator string `mapstructure:"vertical-separator"`
|
||||
SlideNumberVisibility string `mapstructure:"slide-number-visibility"`
|
||||
SlideNumberFormat string `mapstructure:"slide-number-format"`
|
||||
StyleSheets []string `mapstructure:"stylesheets"`
|
||||
FilesToMonitor []string `mapstructure:"filesToMonitor"`
|
||||
}
|
||||
|
||||
func (params *RevealParams) Load() {
|
||||
_ = viper.Unmarshal(params)
|
||||
expandGlobs(params)
|
||||
}
|
||||
|
||||
func expandGlobs(params *RevealParams) {
|
||||
var allFiles []string
|
||||
|
||||
for _, f := range params.FilesToMonitor {
|
||||
var err error
|
||||
|
||||
var matches []string
|
||||
if matches, err = doublestar.Glob(f); err != nil {
|
||||
continue
|
||||
}
|
||||
allFiles = append(allFiles, matches...)
|
||||
}
|
||||
params.FilesToMonitor = allFiles
|
||||
}
|
|
@ -15,12 +15,13 @@
|
|||
package rendering
|
||||
|
||||
import (
|
||||
"github.com/baez90/go-reveal-slides/internal/app/config"
|
||||
"github.com/markbates/pkger"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/Masterminds/sprig"
|
||||
"github.com/markbates/pkger"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type RevealRenderer interface {
|
||||
|
@ -28,7 +29,7 @@ type RevealRenderer interface {
|
|||
init() error
|
||||
}
|
||||
|
||||
func NewRevealRenderer(params *config.RevealParams) (renderer RevealRenderer, err error) {
|
||||
func NewRevealRenderer(params *RevealParams) (renderer RevealRenderer, err error) {
|
||||
renderer = &revealRenderer{
|
||||
params: params,
|
||||
}
|
||||
|
@ -40,7 +41,7 @@ func NewRevealRenderer(params *config.RevealParams) (renderer RevealRenderer, er
|
|||
type revealRenderer struct {
|
||||
template *template.Template
|
||||
renderedTemplate string
|
||||
params *config.RevealParams
|
||||
params *RevealParams
|
||||
}
|
||||
|
||||
func (renderer *revealRenderer) init() (err error) {
|
||||
|
@ -54,11 +55,11 @@ func (renderer *revealRenderer) init() (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
renderer.template, err = template.New("index").Parse(string(templateString))
|
||||
renderer.template, err = template.New("index").Funcs(sprig.FuncMap()).Parse(string(templateString))
|
||||
return
|
||||
}
|
||||
|
||||
func (renderer *revealRenderer) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
||||
func (renderer *revealRenderer) ServeHTTP(response http.ResponseWriter, _ *http.Request) {
|
||||
|
||||
if renderer.template == nil {
|
||||
writeErrorResponse(500, "rendering is not set - probably error during startup", response)
|
||||
|
@ -66,7 +67,7 @@ func (renderer *revealRenderer) ServeHTTP(response http.ResponseWriter, request
|
|||
}
|
||||
|
||||
err := renderer.template.Execute(response, struct {
|
||||
Reveal config.RevealParams
|
||||
Reveal RevealParams
|
||||
}{Reveal: *renderer.params})
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -3,39 +3,47 @@ package routing
|
|||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type LayeredHandler struct {
|
||||
var (
|
||||
ErrFileNotFound = errors.New("file not found in any layer")
|
||||
)
|
||||
|
||||
func NewLayeredFileSystem(layers ...http.FileSystem) http.FileSystem {
|
||||
return &layeredFileSystem{
|
||||
resolveCache: make(map[string]http.FileSystem),
|
||||
layers: layers,
|
||||
}
|
||||
}
|
||||
|
||||
type layeredFileSystem struct {
|
||||
layers []http.FileSystem
|
||||
resolveCache map[string]http.FileSystem
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (l *LayeredHandler) AddHandlers(layers ...http.FileSystem) {
|
||||
l.layers = append(l.layers, layers...)
|
||||
func (l *layeredFileSystem) Open(name string) (f http.File, err error) {
|
||||
if cachedLayer, isCached := l.resolveCache[name]; isCached {
|
||||
if cachedLayer != nil {
|
||||
return cachedLayer.Open(name)
|
||||
} else {
|
||||
return nil, ErrFileNotFound
|
||||
}
|
||||
}
|
||||
|
||||
func (l LayeredHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
_, fileName := filepath.Split(request.URL.Path)
|
||||
f, err := l.selectLayer(request.URL.Path)
|
||||
if err != nil {
|
||||
writer.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
stat, err := f.Stat()
|
||||
if err != nil {
|
||||
writer.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
http.ServeContent(writer, request, fileName, stat.ModTime(), f)
|
||||
}
|
||||
|
||||
func (l LayeredHandler) selectLayer(requestedFilePath string) (f http.File, err error) {
|
||||
for idx := range l.layers {
|
||||
layer := l.layers[idx]
|
||||
if f, err = layer.Open(requestedFilePath); err == nil {
|
||||
if f, err = layer.Open(name); err == nil {
|
||||
l.lock.Lock()
|
||||
l.resolveCache[name] = layer
|
||||
l.lock.Unlock()
|
||||
return
|
||||
}
|
||||
}
|
||||
return nil, errors.New("not found")
|
||||
|
||||
l.lock.Lock()
|
||||
l.resolveCache[name] = nil
|
||||
l.lock.Unlock()
|
||||
return nil, ErrFileNotFound
|
||||
}
|
||||
|
|
33
internal/app/routing/markdown_fs.go
Normal file
33
internal/app/routing/markdown_fs.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package routing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type markdownFs struct {
|
||||
destinationPath string
|
||||
}
|
||||
|
||||
func NewMarkdownFS(path string) (fs http.FileSystem, err error) {
|
||||
var info os.FileInfo
|
||||
info, err = os.Stat(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if info.IsDir() || filepath.Ext(info.Name()) != ".md" {
|
||||
err = fmt.Errorf("path %s did not pass sanity checks for markdown files", path)
|
||||
return
|
||||
}
|
||||
|
||||
return &markdownFs{
|
||||
destinationPath: path,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m markdownFs) Open(_ string) (http.File, error) {
|
||||
return os.Open(m.destinationPath)
|
||||
}
|
44
internal/app/routing/no_cache_handler.go
Normal file
44
internal/app/routing/no_cache_handler.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package routing
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var epoch = time.Unix(0, 0).Format(time.RFC1123)
|
||||
|
||||
var noCacheHeaders = map[string]string{
|
||||
"Expires": epoch,
|
||||
"Cache-Control": "no-cache, private, max-age=0",
|
||||
"Pragma": "no-cache",
|
||||
"X-Accel-Expires": "0",
|
||||
}
|
||||
|
||||
var etagHeaders = []string{
|
||||
"ETag",
|
||||
"If-Modified-Since",
|
||||
"If-Match",
|
||||
"If-None-Match",
|
||||
"If-Range",
|
||||
"If-Unmodified-Since",
|
||||
}
|
||||
|
||||
func NoCache(h http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
// Delete any ETag headers that may have been set
|
||||
for _, v := range etagHeaders {
|
||||
if r.Header.Get(v) != "" {
|
||||
r.Header.Del(v)
|
||||
}
|
||||
}
|
||||
|
||||
// Set our NoCache headers
|
||||
for k, v := range noCacheHeaders {
|
||||
w.Header().Set(k, v)
|
||||
}
|
||||
|
||||
h.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
102
internal/app/server/hash_handler.go
Normal file
102
internal/app/server/hash_handler.go
Normal file
|
@ -0,0 +1,102 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"hash"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.uber.org/multierr"
|
||||
)
|
||||
|
||||
var (
|
||||
pathMatcherRegexp = regexp.MustCompile(`(?i)^/hash/(md5|sha1|sha2)(/.*)`)
|
||||
hashes = map[string]crypto.Hash{
|
||||
"md5": crypto.MD5,
|
||||
"sha1": crypto.SHA1,
|
||||
"sha256": crypto.SHA256,
|
||||
}
|
||||
)
|
||||
|
||||
type hashResponse struct {
|
||||
FilePath string
|
||||
Hash string
|
||||
}
|
||||
|
||||
func NewHashHandler(fs http.FileSystem) http.Handler {
|
||||
return &hashHandler{
|
||||
fs: fs,
|
||||
bufferPool: &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, 4096)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type hashHandler struct {
|
||||
bufferPool *sync.Pool
|
||||
fs http.FileSystem
|
||||
}
|
||||
|
||||
func (h hashHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
components := pathMatcherRegexp.FindStringSubmatch(request.URL.Path)
|
||||
if len(components) != 3 {
|
||||
writer.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
|
||||
filePath := components[2]
|
||||
|
||||
var hashFound bool
|
||||
var hashInstance crypto.Hash
|
||||
if hashInstance, hashFound = hashes[strings.ToLower(components[1])]; !hashFound {
|
||||
writer.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
var f http.File
|
||||
var err error
|
||||
if f, err = h.fs.Open(filePath); err != nil {
|
||||
writer.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
|
||||
var encodedHash string
|
||||
if encodedHash, err = h.buildHash(hashInstance.New(), f); err != nil {
|
||||
log.Errorf("Failed to calculate hash %v", err)
|
||||
writer.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
|
||||
resp := hashResponse{
|
||||
FilePath: filePath,
|
||||
Hash: encodedHash,
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(writer)
|
||||
if err = encoder.Encode(resp); err != nil {
|
||||
writer.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *hashHandler) buildHash(hasher hash.Hash, src io.ReadCloser) (encodedHash string, err error) {
|
||||
defer func() {
|
||||
err = multierr.Append(err, src.Close())
|
||||
}()
|
||||
|
||||
buffer := h.bufferPool.Get().([]byte)
|
||||
for n, err := src.Read(buffer); n > 0 && err == nil; n, err = src.Read(buffer) {
|
||||
if _, err := hasher.Write(buffer[:n]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
encodedHash = hex.EncodeToString(hasher.Sum(nil))
|
||||
return
|
||||
}
|
74
internal/app/server/http_server.go
Normal file
74
internal/app/server/http_server.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/baez90/go-reveal-slides/internal/app/rendering"
|
||||
"github.com/baez90/go-reveal-slides/internal/app/routing"
|
||||
"github.com/markbates/pkger"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port uint16
|
||||
MarkdownPath string
|
||||
RevealParams *rendering.RevealParams
|
||||
}
|
||||
|
||||
type HTTPServer struct {
|
||||
listener net.Listener
|
||||
handler http.Handler
|
||||
}
|
||||
|
||||
func (srv HTTPServer) Serve() error {
|
||||
return http.Serve(srv.listener, srv.handler)
|
||||
}
|
||||
|
||||
func (srv HTTPServer) ListenAddress() string {
|
||||
return srv.listener.Addr().String()
|
||||
}
|
||||
|
||||
func NewHTTPServer(config Config) (srv *HTTPServer, err error) {
|
||||
router := &routing.RegexpRouter{}
|
||||
var tmplRenderer rendering.RevealRenderer
|
||||
if tmplRenderer, err = rendering.NewRevealRenderer(config.RevealParams); err != nil {
|
||||
err = fmt.Errorf("failed to initialize reveal renderer %w", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = router.AddRule(`^(\/(index.html(l)?)?)?$`, tmplRenderer); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var mdFS http.FileSystem
|
||||
if mdFS, err = routing.NewMarkdownFS(config.MarkdownPath); err != nil {
|
||||
err = fmt.Errorf("failed to initialize markdown file handler %w", err)
|
||||
return
|
||||
}
|
||||
fs := routing.NewLayeredFileSystem(pkger.Dir("/assets/reveal"), pkger.Dir("/assets/web"), http.Dir("."), mdFS)
|
||||
|
||||
//language=regexp
|
||||
if err = router.AddRule(`^(?i)/hash/(md5|sha1|sha2)/.*`, routing.NoCache(NewHashHandler(fs))); err != nil {
|
||||
return
|
||||
}
|
||||
if err = router.AddRule("^/.*\\.md$", routing.NoCache(http.FileServer(mdFS))); err != nil {
|
||||
return
|
||||
}
|
||||
if err = router.AddRule("/.+", routing.NoCache(http.FileServer(fs))); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
hostPort := fmt.Sprintf("%s:%d", config.Host, config.Port)
|
||||
|
||||
srv = &HTTPServer{
|
||||
handler: router,
|
||||
}
|
||||
|
||||
if srv.listener, err = net.Listen("tcp", hostPort); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
|
@ -3,5 +3,5 @@ package root
|
|||
import "github.com/markbates/pkger"
|
||||
|
||||
func init() {
|
||||
pkger.Include("/cmd/goveal")
|
||||
pkger.Include("/assets/reveal")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue