-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
71 lines (56 loc) · 1.92 KB
/
response.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bytes"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"path"
"strings"
"text/template"
)
func (server *GopherServer) createListing(reqPath string) (string, error) {
log.Info("Creating listing for request: ", reqPath)
var listing string
files, err := server.ServerRoot.GetServerDir(reqPath)
if err != nil {
return "", err
}
for _, f := range files {
link := ""
link = server.createLink(server.ServerRoot.Type(path.Join(reqPath, f)), f, path.Join(reqPath, f))
log.Info("Adding item: " + replaceCRLF(link))
listing += link
}
return strings.TrimRight(listing, "\r\n"), nil
}
func (server *GopherServer) createLink(itemType, text, path string) string {
return fmt.Sprintf("%s%s\t%s\t%s\t%s\r\n", itemTypes[itemType], text, path, server.Domain, server.Port)
}
func (server *GopherServer) parseTemplate(templ string, data map[string]string) (string, error) {
outHeader := bytes.NewBufferString("")
outFile := bytes.NewBufferString("")
outFooter := bytes.NewBufferString("")
// Parse header template
if headerTmpl, err := template.New("header").Parse(server.ServerRoot.HeaderTemplate()); err == nil {
if err := headerTmpl.Execute(outHeader, data); err != nil {
return "", errors.New("Could not parse header Template")
}
}
// Parse footer template
if footerTmpl, err := template.New("footer").Parse(server.ServerRoot.FooterTemplate()); err == nil {
if err := footerTmpl.Execute(outFooter, data); err != nil {
return "", errors.New("Could not parse footer template")
}
}
// Parse request file as template
if fileTmpl, err := template.New("request").Parse(templ); err == nil {
if err := fileTmpl.Execute(outFile, data); err != nil {
return "", errors.New("Could not parse file template")
}
}
return strings.TrimRight(outHeader.String()+outFile.String()+outFooter.String(), "\n"), nil
}
func (server *GopherServer) isTemplate(path string) bool {
//TODO implement
return true
}