add footer text support + move last-line addition to footer text

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
This commit is contained in:
kim (grufwub) 2020-04-30 13:58:29 +01:00
parent 74cb7dde76
commit 5cb9a6cb9e
6 changed files with 50 additions and 8 deletions

View File

@ -16,6 +16,7 @@ type ServerConfig struct {
RootDir string
/* Content settings */
FooterText []byte
PageWidth int
RestrictedFiles []*regexp.Regexp

View File

@ -67,12 +67,13 @@ type GophermapContents struct {
}
func (gc *GophermapContents) Render(request *FileSystemRequest) []byte {
returnContents := make([]byte, 0)
/* We don't just want to read the contents, each section
* in the sections slice needs a call to render() to
* perform their own required actions in producing a
* sendable byte slice.
*/
returnContents := make([]byte, 0)
for _, line := range gc.sections {
content, gophorErr := line.Render(request)
if gophorErr != nil {
@ -81,8 +82,7 @@ func (gc *GophermapContents) Render(request *FileSystemRequest) []byte {
returnContents = append(returnContents, content...)
}
/* Finally we end render with last line */
returnContents = append(returnContents, []byte(LastLine)...)
/* The footer added later contains last line, don't need to worry */
return returnContents
}

View File

@ -74,16 +74,29 @@ func (fs *FileSystem) HandleRequest(requestPath string, host *ConnHost) ([]byte,
switch fileType {
/* Directory */
case FileTypeDir:
/* Check Gophermap exists */
gophermapPath := path.Join(requestPath, GophermapFileStr)
_, err := os.Stat(gophermapPath)
var output []byte
var gophorErr *GophorError
if err == nil {
/* Gophermap exists, serve this! */
return fs.FetchFile(&FileSystemRequest{ gophermapPath, host })
output, gophorErr = fs.FetchFile(&FileSystemRequest{ gophermapPath, host })
} else {
/* No gophermap, serve directory listing */
return listDir(&FileSystemRequest{ requestPath, host }, map[string]bool{})
output, gophorErr = listDir(&FileSystemRequest{ requestPath, host }, map[string]bool{})
}
if gophorErr != nil {
/* Fail out! */
return nil, gophorErr
}
/* Append footer text (contains last line) and return */
output = append(output, Config.FooterText...)
return output, nil
/* Regular file */
case FileTypeRegular:
return fs.FetchFile(&FileSystemRequest{ requestPath, host })

View File

@ -200,9 +200,6 @@ func _listDirBase(request *FileSystemRequest, iterFunc func(dirContents *[]byte,
/* Walk through files :D */
for _, file := range files { iterFunc(&dirContents, file) }
/* Append last line */
dirContents = append(dirContents, []byte(LastLine)...)
return dirContents, nil
}

View File

@ -141,6 +141,31 @@ func getItemType(name string) ItemType {
}
}
/* Build a line separator of supplied width */
func buildLineSeparator(count int) string {
ret := ""
for i := 0; i < count; i += 1 {
ret += "_"
}
return ret
}
/* Formats an info-text footer from string. Add last line as we use the footer to contain last line (regardless if empty) */
func formatGophermapFooter(text string, useSeparator bool) []byte {
ret := make([]byte, 0)
if text != "" {
ret = append(ret, buildInfoLine("")...)
if useSeparator {
ret = append(ret, buildInfoLine(buildLineSeparator(Config.PageWidth))...)
}
for _, line := range strings.Split(text, "\n") {
ret = append(ret, buildInfoLine(line)...)
}
}
ret = append(ret, []byte(LastLine)...)
return ret
}
/* Parse line type from contents */
func parseLineType(line string) ItemType {
lineLen := len(line)

View File

@ -82,6 +82,9 @@ func setupServer() []*GophorListener {
serverGeoloc := flag.String("geoloc", "", "Change server gelocation string in auto-generated caps.txt.")
/* Content settings */
footerText := flag.String("footer", "Running Gophor, a Gopher server in Go.", "Change gophermap footer text (Unix new-line separated lines).")
footerSeparator := flag.Bool("no-footer-separator", false, "Disable footer text line separator.")
pageWidth := flag.Int("page-width", 80, "Change page width used when formatting output.")
restrictedFiles := flag.String("restrict-files", "", "New-line separated list of regex statements restricting files from showing in directory listings.")
@ -110,6 +113,9 @@ func setupServer() []*GophorListener {
Config.RootDir = *serverRoot
Config.PageWidth = *pageWidth
/* Have to be set AFTER page width variable set */
Config.FooterText = formatGophermapFooter(*footerText, !*footerSeparator)
/* Setup Gophor logging system */
Config.SystemLogger, Config.AccessLogger = setupLogging(*logType, *systemLogPath, *accessLogPath)