feat: added way to serve single files and fixed the redirecting relative path problems.
This commit is contained in:
parent
893a53c482
commit
e86beb23d0
8 changed files with 58 additions and 11 deletions
4
api.go
4
api.go
|
@ -16,9 +16,13 @@ func (fn Func) Handle(c *Context) {
|
|||
|
||||
// The wrapper for the standard http.Handler(s).
|
||||
type Wrap struct {
|
||||
UseRelUrl bool
|
||||
HttpHandler
|
||||
}
|
||||
func (w Wrap) Handle(c *Context) {
|
||||
if w.UseRelUrl {
|
||||
c.R.URL = c.RelUrl
|
||||
}
|
||||
w.ServeHTTP(c.W, c.R)
|
||||
}
|
||||
|
||||
|
|
|
@ -15,14 +15,12 @@ type GetNotesOptions struct {
|
|||
var root = bond.Root(bond.Path().
|
||||
Def(
|
||||
"",
|
||||
bond.Func(func(c *bond.Context) {
|
||||
c.W.Write([]byte("This is the index page"))
|
||||
}),
|
||||
bond.StaticFile("index.html"),
|
||||
).Def(
|
||||
"hello",
|
||||
bond.Path().Def(
|
||||
// Using the relative redirect to force us to the en.
|
||||
"", bond.Redirect("/hello/en", statuses.SeeOther),
|
||||
"", bond.Redirect("en", statuses.Found),
|
||||
).Def(
|
||||
"en", bond.Func(func(c *bond.Context) {
|
||||
c.Printf("Hello, World!")
|
||||
|
@ -33,14 +31,21 @@ Def(
|
|||
}),
|
||||
),
|
||||
).Def(
|
||||
"web", bond.Static("./static"),
|
||||
"google", bond.Redirect("https://google.com", statuses.Found),
|
||||
).Def(
|
||||
"web", bond.StaticDir("./static"),
|
||||
).Def(
|
||||
"test", bond.Func(func(c *bond.Context) {
|
||||
c.SetContentType(contents.Plain)
|
||||
c.Printf(
|
||||
"AbsPath: %q\n" +
|
||||
"Prefix: %q\n" +
|
||||
"Path: %q\n"+
|
||||
"Content-Type: %q\n",
|
||||
c.Path(), c.ContentType(),
|
||||
c.AbsPath(),
|
||||
c.PathPrefix(),
|
||||
c.Path(),
|
||||
c.ContentType(),
|
||||
)
|
||||
c.Printf("Query:\n")
|
||||
for k, vs := range c.Query() {
|
||||
|
|
18
context.go
18
context.go
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
type Context struct {
|
||||
R *Request
|
||||
RelUrl *url.URL
|
||||
W ResponseWriter
|
||||
// Custom data to store stuff.
|
||||
Data any
|
||||
|
@ -87,10 +88,20 @@ func (c *Context) ScanErr() error {
|
|||
return c.scanErr
|
||||
}
|
||||
|
||||
func (c *Context) Path() string {
|
||||
func (c *Context) AbsPath() string {
|
||||
return c.R.URL.Path
|
||||
}
|
||||
|
||||
func (c *Context) PathPrefix() string {
|
||||
pth := c.AbsPath()
|
||||
rpth := c.Path()
|
||||
return pth[:len(pth)-len(rpth)]
|
||||
}
|
||||
|
||||
func (c *Context) Path() string {
|
||||
return c.RelUrl.Path
|
||||
}
|
||||
|
||||
func (c *Context) NotFound() {
|
||||
http.NotFound(c.W, c.R)
|
||||
}
|
||||
|
@ -104,5 +115,10 @@ func (c *Context) Query() url.Values {
|
|||
}
|
||||
|
||||
func (c *Context) Redirect(u string, status Status) {
|
||||
pth := c.AbsPath()
|
||||
if len(pth) > 0 && pth[len(pth)-1] != '/' {
|
||||
pth += "/"
|
||||
}
|
||||
c.R.URL.Path = pth
|
||||
http.Redirect(c.W, c.R, u, int(status))
|
||||
}
|
||||
|
|
10
index.html
Normal file
10
index.html
Normal file
|
@ -0,0 +1,10 @@
|
|||
<!doctype html>
|
||||
<html><head>
|
||||
</head><body>
|
||||
<p>
|
||||
This is the index page!
|
||||
</p>
|
||||
<div><a href="hello/en">English Hello</a></div>
|
||||
<div><a href="hello/ru">Русский Привет</a></div>
|
||||
<div><a href="web/">Static files directory</a></div>
|
||||
</body></html>
|
2
path.go
2
path.go
|
@ -59,7 +59,7 @@ func (router *PathRouter) Handle(c *Context) {
|
|||
return
|
||||
}
|
||||
|
||||
c.R.URL.Path = rest
|
||||
c.RelUrl.Path = rest
|
||||
handler.Handle(c)
|
||||
}
|
||||
|
||||
|
|
2
root.go
2
root.go
|
@ -23,5 +23,7 @@ func (router *RootRouter) ServeHTTP(w ResponseWriter, r *Request) {
|
|||
ctx := Context{}
|
||||
ctx.W = w
|
||||
ctx.R = r
|
||||
u := *r.URL
|
||||
ctx.RelUrl = &u
|
||||
router.handler.Handle(&ctx)
|
||||
}
|
||||
|
|
2
short.go
2
short.go
|
@ -1,6 +1,6 @@
|
|||
package bond
|
||||
|
||||
type RedirectRouter string
|
||||
// Returns the handler that redirects to the specified URL (u)
|
||||
func Redirect(u string, status Status) Handler {
|
||||
return Func(func(c *Context){
|
||||
c.Redirect(u, status)
|
||||
|
|
14
static.go
14
static.go
|
@ -4,7 +4,17 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
func Static(pth string) Handler {
|
||||
return Wrap{http.FileServer(http.Dir(pth))}
|
||||
func StaticDir(pth string) Handler {
|
||||
return Wrap{
|
||||
UseRelUrl: true,
|
||||
HttpHandler: http.FileServer(http.Dir(pth)),
|
||||
}
|
||||
}
|
||||
|
||||
func StaticFile(pth string) Handler {
|
||||
return Wrap{
|
||||
HttpHandler: HttpHandlerFunc(func(w ResponseWriter, r *Request){
|
||||
http.ServeFile(w, r, pth)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue