add page error when file don't exists

This commit is contained in:
arnaucube
2019-05-08 21:46:25 +02:00
parent a0b3d071e5
commit 1eec2da173
4 changed files with 27 additions and 1 deletions

View File

@@ -14,6 +14,5 @@ And then go to the browser at `http://127.0.0.1:8080`
- [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

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"html/template"
"log"
"net/http"
@@ -60,6 +61,11 @@ func getPage(w http.ResponseWriter, r *http.Request) {
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)
}

Binary file not shown.

View File

@@ -74,3 +74,24 @@ h2:after{
</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>
`