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).
|
// The wrapper for the standard http.Handler(s).
|
||||||
type Wrap struct {
|
type Wrap struct {
|
||||||
|
UseRelUrl bool
|
||||||
HttpHandler
|
HttpHandler
|
||||||
}
|
}
|
||||||
func (w Wrap) Handle(c *Context) {
|
func (w Wrap) Handle(c *Context) {
|
||||||
|
if w.UseRelUrl {
|
||||||
|
c.R.URL = c.RelUrl
|
||||||
|
}
|
||||||
w.ServeHTTP(c.W, c.R)
|
w.ServeHTTP(c.W, c.R)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,14 +15,12 @@ type GetNotesOptions struct {
|
||||||
var root = bond.Root(bond.Path().
|
var root = bond.Root(bond.Path().
|
||||||
Def(
|
Def(
|
||||||
"",
|
"",
|
||||||
bond.Func(func(c *bond.Context) {
|
bond.StaticFile("index.html"),
|
||||||
c.W.Write([]byte("This is the index page"))
|
|
||||||
}),
|
|
||||||
).Def(
|
).Def(
|
||||||
"hello",
|
"hello",
|
||||||
bond.Path().Def(
|
bond.Path().Def(
|
||||||
// Using the relative redirect to force us to the en.
|
// Using the relative redirect to force us to the en.
|
||||||
"", bond.Redirect("/hello/en", statuses.SeeOther),
|
"", bond.Redirect("en", statuses.Found),
|
||||||
).Def(
|
).Def(
|
||||||
"en", bond.Func(func(c *bond.Context) {
|
"en", bond.Func(func(c *bond.Context) {
|
||||||
c.Printf("Hello, World!")
|
c.Printf("Hello, World!")
|
||||||
|
@ -33,14 +31,21 @@ Def(
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
).Def(
|
).Def(
|
||||||
"web", bond.Static("./static"),
|
"google", bond.Redirect("https://google.com", statuses.Found),
|
||||||
|
).Def(
|
||||||
|
"web", bond.StaticDir("./static"),
|
||||||
).Def(
|
).Def(
|
||||||
"test", bond.Func(func(c *bond.Context) {
|
"test", bond.Func(func(c *bond.Context) {
|
||||||
c.SetContentType(contents.Plain)
|
c.SetContentType(contents.Plain)
|
||||||
c.Printf(
|
c.Printf(
|
||||||
|
"AbsPath: %q\n" +
|
||||||
|
"Prefix: %q\n" +
|
||||||
"Path: %q\n"+
|
"Path: %q\n"+
|
||||||
"Content-Type: %q\n",
|
"Content-Type: %q\n",
|
||||||
c.Path(), c.ContentType(),
|
c.AbsPath(),
|
||||||
|
c.PathPrefix(),
|
||||||
|
c.Path(),
|
||||||
|
c.ContentType(),
|
||||||
)
|
)
|
||||||
c.Printf("Query:\n")
|
c.Printf("Query:\n")
|
||||||
for k, vs := range c.Query() {
|
for k, vs := range c.Query() {
|
||||||
|
|
18
context.go
18
context.go
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
R *Request
|
R *Request
|
||||||
|
RelUrl *url.URL
|
||||||
W ResponseWriter
|
W ResponseWriter
|
||||||
// Custom data to store stuff.
|
// Custom data to store stuff.
|
||||||
Data any
|
Data any
|
||||||
|
@ -87,10 +88,20 @@ func (c *Context) ScanErr() error {
|
||||||
return c.scanErr
|
return c.scanErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) Path() string {
|
func (c *Context) AbsPath() string {
|
||||||
return c.R.URL.Path
|
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() {
|
func (c *Context) NotFound() {
|
||||||
http.NotFound(c.W, c.R)
|
http.NotFound(c.W, c.R)
|
||||||
}
|
}
|
||||||
|
@ -104,5 +115,10 @@ func (c *Context) Query() url.Values {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) Redirect(u string, status Status) {
|
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))
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.R.URL.Path = rest
|
c.RelUrl.Path = rest
|
||||||
handler.Handle(c)
|
handler.Handle(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
root.go
2
root.go
|
@ -23,5 +23,7 @@ func (router *RootRouter) ServeHTTP(w ResponseWriter, r *Request) {
|
||||||
ctx := Context{}
|
ctx := Context{}
|
||||||
ctx.W = w
|
ctx.W = w
|
||||||
ctx.R = r
|
ctx.R = r
|
||||||
|
u := *r.URL
|
||||||
|
ctx.RelUrl = &u
|
||||||
router.handler.Handle(&ctx)
|
router.handler.Handle(&ctx)
|
||||||
}
|
}
|
||||||
|
|
2
short.go
2
short.go
|
@ -1,6 +1,6 @@
|
||||||
package bond
|
package bond
|
||||||
|
|
||||||
type RedirectRouter string
|
// Returns the handler that redirects to the specified URL (u)
|
||||||
func Redirect(u string, status Status) Handler {
|
func Redirect(u string, status Status) Handler {
|
||||||
return Func(func(c *Context){
|
return Func(func(c *Context){
|
||||||
c.Redirect(u, status)
|
c.Redirect(u, status)
|
||||||
|
|
14
static.go
14
static.go
|
@ -4,7 +4,17 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Static(pth string) Handler {
|
func StaticDir(pth string) Handler {
|
||||||
return Wrap{http.FileServer(http.Dir(pth))}
|
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