arnaucube
arnaucube/md-live-serverPublic Self-hosted repository
add page error when file don't exists
arnaucube arnaucube@gmail.com
Wed, 08 May 2019 21:46:25 +0200
commit

1eec2da1736f34d741cd39fa78519cdac8735733

parent

a0b3d071e55be62ff318d132cd7619d8964151cd

4 files changed, 27 insertions(+), 1 deletions(-)

jump to
M README.md → README.md

@@ -14,6 +14,5 @@ ## Features

- [x] server rendering .md files - [x] live reload when .md file changes - [x] directory files list on `/` endpoint -- [ ] on error return error page, instead of panic server - [ ] graphviz - [ ] colour `<code>` with syntax highlighting
M main.go → main.go

@@ -1,6 +1,7 @@

package main import ( + "fmt" "html/template" "log" "net/http"

@@ -59,6 +60,11 @@ vars := mux.Vars(r)

path := vars["path"] path = strings.Replace(path, "%", "/", -1) log.Println(path) + + if len(strings.Split(path, ".")) < 2 { + fmt.Fprintf(w, errTemplate) + return + } if strings.Split(path, ".")[1] != "md" { http.ServeFile(w, r, path)
M template.go → templates.go

@@ -74,3 +74,24 @@ </script>

</body> </html> ` + +const errTemplate = ` +<!DOCTYPE html> +<html> +<title>{{.Title}}</title> +<style> +body { + font-family: Arial, Helvetica, sans-serif; + background:#000000; + color:#cccccc; +} +</style> +<body> + +<h3>File doesn't exist</h3> +<br><br> +<a href="/">Back to dir menu</a> + +</body> +</html> +`