-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
41 lines (38 loc) · 1.03 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"html/template"
"log"
"net/http"
"strings"
)
// executeTemplate creates a new template and then embeds the data within the TemplateData struct into the html
func executeTemplate(w http.ResponseWriter, fileContent string, templateData *TemplateData) {
tmpl, err := template.New("demo").Parse(fileContent)
if err != nil {
log.Printf("Error parsing file content %v\n", err)
return
}
if err = tmpl.Execute(w, templateData); err != nil {
log.Printf("Error executing template %v\n", err)
}
}
// corpusHandler to serve local documents
func corpusHandler(w http.ResponseWriter, r *http.Request) {
urlPath := r.URL.Path
if strings.HasSuffix(urlPath, "/") {
urlPath += "index.html"
}
filePath := "." + urlPath
fileContent, err := openAndReadFile(filePath)
if err != nil {
_, err = w.Write([]byte("404 No Page found!"))
if err != nil {
log.Printf("Could not serve 404 page %v\n", err)
}
return
}
_, err = w.Write(fileContent)
if err != nil {
log.Printf("Error writing response: %v", err.Error())
}
}