0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Better default template; other fixes

This commit is contained in:
Matthew Holt 2015-03-24 23:05:42 -06:00
parent 13d9bcc0c7
commit 63b39c78ee
2 changed files with 161 additions and 26 deletions

View file

@ -13,6 +13,7 @@ import (
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/mholt/caddy/middleware"
)
@ -55,6 +56,14 @@ type FileInfo struct {
Mode os.FileMode
}
func (fi FileInfo) HumanSize() string {
return humanize.Bytes(uint64(fi.Size))
}
func (fi FileInfo) HumanModTime(format string) string {
return fi.ModTime.Format(format)
}
var IndexPages = []string{
"index.html",
"index.htm",
@ -205,30 +214,17 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) {
for c.Next() {
var bc BrowseConfig
if !c.NextArg() {
// First argument is directory to allow browsing; default is site root
if c.NextArg() {
bc.PathScope = c.Val()
} else {
bc.PathScope = "/"
err := appendCfg(bc)
if err != nil {
return configs, err
}
continue
}
bc.PathScope = c.Val()
if !c.NextArg() {
err := appendCfg(bc)
if err != nil {
return configs, err
}
continue
}
tplFile := c.Val()
// Second argument would be the template file to use
var tplText string
if tplFile != "" {
tplBytes, err := ioutil.ReadFile(tplFile)
if c.NextArg() {
tplBytes, err := ioutil.ReadFile(c.Val())
if err != nil {
return configs, err
}
@ -237,12 +233,14 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) {
tplText = defaultTemplate
}
// Build the template
tpl, err := template.New("listing").Parse(tplText)
if err != nil {
return configs, err
}
bc.Template = tpl
// Save configuration
err = appendCfg(bc)
if err != nil {
return configs, err
@ -251,9 +249,3 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) {
return configs, nil
}
const defaultTemplate = `
{{range .}}
{{.Name}}<br>
{{end}}
`

View file

@ -0,0 +1,143 @@
package browse
// The default template to use when serving up directory listings
const defaultTemplate = `<!DOCTYPE html>
<html>
<head>
<title>{{.Name}}</title>
<meta charset="utf-8">
<style>
* { padding: 0; margin: 0; }
body {
padding: 1% 2%;
font: 16px sans-serif;
}
header {
font-size: 45px;
padding: 25px;
}
header a {
text-decoration: none;
color: inherit;
}
header .up {
display: inline-block;
height: 50px;
width: 50px;
text-align: center;
margin-right: 20px;
}
header a.up:hover {
background: #000;
color: #FFF;
}
h1 {
font-size: 30px;
display: inline;
}
table {
border: 0;
border-collapse: collapse;
max-width: 750px;
margin: 0 auto;
}
th,
td {
padding: 4px 20px;
vertical-align: middle;
line-height: 1.5em; /* emoji are kind of odd heights */
}
th {
text-align: left;
}
@media (max-width: 650px) {
.hideable {
display: none;
}
body {
padding: 0;
}
header,
header h1 {
font-size: 14px;
}
header {
position: fixed;
top: 0;
width: 100%;
background: #333;
color: #FFF;
padding: 10px;
text-align: center;
}
header .up {
height: auto;
width: auto;
display: none;
}
header a.up {
display: inline-block;
position: absolute;
left: 0;
top: 0;
width: 35px;
height: 28px;
font-size: 35px;
}
header h1 {
font-weight: normal;
}
main {
margin-top: 50px;
}
}
</style>
</head>
<body>
<header>
{{if .CanGoUp}}
<a href=".." class="up" title="Up one level">&#11025;</a>
{{else}}
<div class="up">&nbsp;</div>
{{end}}
<h1>{{.Path}}</h1>
</header>
<main>
<table>
<tr>
<th>Name</th>
<th>Size</th>
<th class="hideable">Modified</th>
</tr>
{{range .Items}}
<tr>
<td>
{{if .IsDir}}&#128194;{{else}}&#128196;{{end}}
<a href="{{.URL}}">{{.Name}}</a>
</td>
<td>{{.HumanSize}}</td>
<td class="hideable">{{.HumanModTime "01/02/2006 3:04:05 PM -0700"}}</td>
</tr>
{{end}}
</table>
</main>
</body>
</html>`