cleanup
This commit is contained in:
parent
ef882c9156
commit
51f08bd6e2
32
server.go
32
server.go
@ -173,24 +173,28 @@ func (s *Server) ParseRequest(req string) Response {
|
|||||||
return Response{STATUS_TEMPORARY_FAILURE, "Unable to access file", ""}
|
return Response{STATUS_TEMPORARY_FAILURE, "Unable to access file", ""}
|
||||||
case fi.IsDir():
|
case fi.IsDir():
|
||||||
if strings.HasSuffix(u.Path, "/") {
|
if strings.HasSuffix(u.Path, "/") {
|
||||||
return generateDirectoryListing(selector)
|
return generateDirectory(selector)
|
||||||
} else {
|
} else {
|
||||||
return Response{STATUS_REDIRECT_PERMANENT, "gemini://" + s.Hostname + u.Path + "/", ""}
|
return Response{STATUS_REDIRECT_PERMANENT, "gemini://" + s.Hostname + u.Path + "/", ""}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
// it's a file
|
// it's a file
|
||||||
meta := mime.TypeByExtension(filepath.Ext(selector))
|
return generateFile(selector)
|
||||||
file, err := os.Open(selector)
|
|
||||||
if err != nil {
|
|
||||||
panic("Failed to read file")
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
buf, err := ioutil.ReadAll(file)
|
|
||||||
return Response{STATUS_SUCCESS, meta, string(buf)}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateDirectoryListing(path string) Response {
|
func generateFile(selector string) Response {
|
||||||
|
meta := mime.TypeByExtension(filepath.Ext(selector))
|
||||||
|
file, err := os.Open(selector)
|
||||||
|
if err != nil {
|
||||||
|
panic("Failed to read file")
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
buf, err := ioutil.ReadAll(file)
|
||||||
|
return Response{STATUS_SUCCESS, meta, string(buf)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateDirectory(path string) Response {
|
||||||
var listing string
|
var listing string
|
||||||
files, err := ioutil.ReadDir(path)
|
files, err := ioutil.ReadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -209,13 +213,7 @@ func generateDirectoryListing(path string) Response {
|
|||||||
}
|
}
|
||||||
if file.Name() == "index.gmi" || file.Name() == "index.gemini" {
|
if file.Name() == "index.gmi" || file.Name() == "index.gemini" {
|
||||||
//Found an index file, return that instead
|
//Found an index file, return that instead
|
||||||
read_file, err := os.Open(path + file.Name())
|
return generateFile(path + file.Name())
|
||||||
if err != nil {
|
|
||||||
return Response{STATUS_TEMPORARY_FAILURE, "Unable to show directory index file", ""}
|
|
||||||
}
|
|
||||||
defer read_file.Close()
|
|
||||||
buf, err := ioutil.ReadAll(read_file)
|
|
||||||
return Response{STATUS_SUCCESS, "text/gemini", string(buf)}
|
|
||||||
} else {
|
} else {
|
||||||
listing += fmt.Sprintf("=> %s %s\r\n", file.Name(), file.Name())
|
listing += fmt.Sprintf("=> %s %s\r\n", file.Name(), file.Name())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user