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
f5d0ed5b
Commit
f5d0ed5b
authored
9 years ago
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More template love
parent
55801b48
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
1 deletion
+43
-1
middleware/templates/context.go
middleware/templates/context.go
+42
-0
middleware/templates/templates.go
middleware/templates/templates.go
+1
-1
No files found.
middleware/templates/context.go
View file @
f5d0ed5b
...
@@ -2,8 +2,12 @@ package templates
...
@@ -2,8 +2,12 @@ package templates
import
(
import
(
"io/ioutil"
"io/ioutil"
"net"
"net/http"
"net/http"
"net/url"
"time"
"time"
"github.com/mholt/caddy/middleware"
)
)
// This file contains the context and functions available for
// This file contains the context and functions available for
...
@@ -13,6 +17,7 @@ import (
...
@@ -13,6 +17,7 @@ import (
type
context
struct
{
type
context
struct
{
root
http
.
FileSystem
root
http
.
FileSystem
req
*
http
.
Request
req
*
http
.
Request
URL
*
url
.
URL
}
}
// Include returns the contents of filename relative to the site root
// Include returns the contents of filename relative to the site root
...
@@ -50,3 +55,40 @@ func (c context) Header(name string) string {
...
@@ -50,3 +55,40 @@ func (c context) Header(name string) string {
func
(
c
context
)
RemoteAddr
()
string
{
func
(
c
context
)
RemoteAddr
()
string
{
return
c
.
req
.
RemoteAddr
return
c
.
req
.
RemoteAddr
}
}
// URI returns the raw, unprocessed request URI (including query
// string and hash) obtained directly from the Request-Line of
// the HTTP request.
func
(
c
context
)
URI
()
string
{
return
c
.
req
.
RequestURI
}
// Host returns the hostname portion of the Host header
// from the HTTP request.
func
(
c
context
)
Host
()
(
string
,
error
)
{
host
,
_
,
err
:=
net
.
SplitHostPort
(
c
.
req
.
Host
)
if
err
!=
nil
{
return
""
,
err
}
return
host
,
nil
}
// Port returns the port portion of the Host header if specified.
func
(
c
context
)
Port
()
(
string
,
error
)
{
_
,
port
,
err
:=
net
.
SplitHostPort
(
c
.
req
.
Host
)
if
err
!=
nil
{
return
""
,
err
}
return
port
,
nil
}
// Method returns the method (GET, POST, etc.) of the request.
func
(
c
context
)
Method
()
string
{
return
c
.
req
.
Method
}
// PathMatches returns true if the path portion of the request
// URL matches pattern.
func
(
c
context
)
PathMatches
(
pattern
string
)
bool
{
return
middleware
.
Path
(
c
.
req
.
URL
.
Path
)
.
Matches
(
pattern
)
}
This diff is collapsed.
Click to expand it.
middleware/templates/templates.go
View file @
f5d0ed5b
...
@@ -38,7 +38,7 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
...
@@ -38,7 +38,7 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
for
_
,
ext
:=
range
rule
.
Extensions
{
for
_
,
ext
:=
range
rule
.
Extensions
{
if
reqExt
==
ext
{
if
reqExt
==
ext
{
// Create execution context
// Create execution context
ctx
:=
context
{
root
:
http
.
Dir
(
t
.
Root
),
req
:
r
}
ctx
:=
context
{
root
:
http
.
Dir
(
t
.
Root
),
req
:
r
,
URL
:
r
.
URL
}
// Build the template
// Build the template
tpl
,
err
:=
template
.
ParseFiles
(
t
.
Root
+
r
.
URL
.
Path
)
tpl
,
err
:=
template
.
ParseFiles
(
t
.
Root
+
r
.
URL
.
Path
)
...
...
This diff is collapsed.
Click to expand it.
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