Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
3ec870cb
Commit
3ec870cb
authored
Apr 18, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Templates middleware with "include" functionality
parent
cd0421ce
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
132 additions
and
0 deletions
+132
-0
config/middleware.go
config/middleware.go
+2
-0
middleware/templates/context.go
middleware/templates/context.go
+24
-0
middleware/templates/templates.go
middleware/templates/templates.go
+106
-0
No files found.
config/middleware.go
View file @
3ec870cb
...
@@ -13,6 +13,7 @@ import (
...
@@ -13,6 +13,7 @@ import (
"github.com/mholt/caddy/middleware/proxy"
"github.com/mholt/caddy/middleware/proxy"
"github.com/mholt/caddy/middleware/redirect"
"github.com/mholt/caddy/middleware/redirect"
"github.com/mholt/caddy/middleware/rewrite"
"github.com/mholt/caddy/middleware/rewrite"
"github.com/mholt/caddy/middleware/templates"
"github.com/mholt/caddy/middleware/websockets"
"github.com/mholt/caddy/middleware/websockets"
)
)
...
@@ -48,6 +49,7 @@ func init() {
...
@@ -48,6 +49,7 @@ func init() {
register
(
"fastcgi"
,
fastcgi
.
New
)
register
(
"fastcgi"
,
fastcgi
.
New
)
register
(
"websocket"
,
websockets
.
New
)
register
(
"websocket"
,
websockets
.
New
)
register
(
"markdown"
,
markdown
.
New
)
register
(
"markdown"
,
markdown
.
New
)
register
(
"templates"
,
templates
.
New
)
register
(
"browse"
,
browse
.
New
)
register
(
"browse"
,
browse
.
New
)
}
}
...
...
middleware/templates/context.go
0 → 100644
View file @
3ec870cb
package
templates
import
(
"io/ioutil"
"net/http"
)
// This file contains the context and functions available for
// use in the templates.
// context is the context with which templates are executed.
type
context
struct
{
root
http
.
FileSystem
}
// Include returns the contents of filename relative to the site root
func
(
c
context
)
Include
(
filename
string
)
(
string
,
error
)
{
file
,
err
:=
c
.
root
.
Open
(
filename
)
if
err
!=
nil
{
return
""
,
err
}
body
,
err
:=
ioutil
.
ReadAll
(
file
)
return
string
(
body
),
err
}
middleware/templates/templates.go
0 → 100644
View file @
3ec870cb
package
templates
import
(
"html/template"
"net/http"
"path"
"github.com/mholt/caddy/middleware"
)
// New constructs a new Templates middleware instance.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
rules
,
err
:=
parse
(
c
)
if
err
!=
nil
{
return
nil
,
err
}
tmpls
:=
Templates
{
Root
:
c
.
Root
(),
Rules
:
rules
,
}
return
func
(
next
middleware
.
Handler
)
middleware
.
Handler
{
tmpls
.
Next
=
next
return
tmpls
},
nil
}
// ServeHTTP implements the middleware.Handler interface.
func
(
t
Templates
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
rule
:=
range
t
.
Rules
{
if
!
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
Path
)
{
continue
}
reqExt
:=
path
.
Ext
(
r
.
URL
.
Path
)
for
_
,
ext
:=
range
rule
.
Extensions
{
if
reqExt
==
ext
{
// Create execution context
ctx
:=
context
{
root
:
http
.
Dir
(
t
.
Root
)}
// Build the template
tpl
,
err
:=
template
.
ParseFiles
(
t
.
Root
+
r
.
URL
.
Path
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
// Execute it
err
=
tpl
.
Execute
(
w
,
ctx
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
return
http
.
StatusOK
,
nil
}
}
}
return
t
.
Next
.
ServeHTTP
(
w
,
r
)
}
func
parse
(
c
middleware
.
Controller
)
([]
Rule
,
error
)
{
var
rules
[]
Rule
for
c
.
Next
()
{
var
rule
Rule
if
c
.
NextArg
()
{
// First argument would be the path
rule
.
Path
=
c
.
Val
()
// Any remaining arguments are extensions
rule
.
Extensions
=
c
.
RemainingArgs
()
if
len
(
rule
.
Extensions
)
==
0
{
rule
.
Extensions
=
defaultExtensions
}
}
else
{
rule
.
Path
=
defaultPath
rule
.
Extensions
=
defaultExtensions
}
rules
=
append
(
rules
,
rule
)
}
return
rules
,
nil
}
// Templates is middleware to render templated files as the HTTP response.
type
Templates
struct
{
Next
middleware
.
Handler
Root
string
Rules
[]
Rule
}
// Rule represents a template rule. A template will only execute
// with this rule if the request path matches the Path specified
// and requests a resource with one of the extensions specified.
type
Rule
struct
{
Path
string
Extensions
[]
string
}
const
defaultPath
=
"/"
var
defaultExtensions
=
[]
string
{
".html"
,
".htm"
,
".txt"
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment