Browse Source

add page error when file don't exists

master
arnaucube 5 years ago
parent
commit
1eec2da173
4 changed files with 27 additions and 1 deletions
  1. +0
    -1
      README.md
  2. +6
    -0
      main.go
  3. BIN
      md-live-server
  4. +21
    -0
      templates.go

+ 0
- 1
README.md

@ -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

+ 6
- 0
main.go

@ -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)
}

BIN
md-live-server


template.go → templates.go

@ -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>
`

Loading…
Cancel
Save