119 lines
2.2 KiB
Go
119 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"vultras.su/core/bond"
|
|
"vultras.su/core/bond/methods"
|
|
"vultras.su/core/bond/contents"
|
|
"vultras.su/core/bond/statuses"
|
|
)
|
|
|
|
type GetNotesOptions struct {
|
|
Id int `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
var root = bond.Root(bond.Path().
|
|
Case(
|
|
"",
|
|
bond.StaticFile("index.html"),
|
|
).Case(
|
|
"hello",
|
|
bond.Path().Case(
|
|
// Using the relative redirect to force us to the en.
|
|
"", bond.Redirect("en", statuses.Found),
|
|
).Case(
|
|
"en", bond.Func(func(c *bond.Context) {
|
|
c.Printf("Hello, World!")
|
|
}),
|
|
).Case(
|
|
"ru", bond.Func(func(c *bond.Context) {
|
|
c.Printf("Привет, Мир!")
|
|
}),
|
|
).Default(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.AbsPath(),
|
|
c.PathPrefix(),
|
|
c.Path(),
|
|
c.ContentType(),
|
|
)
|
|
c.Printf("Query:\n")
|
|
for k, vs := range c.Query() {
|
|
c.Printf("\t%q:\n", k)
|
|
for _, v := range vs {
|
|
c.Printf("\t\t%q\n", v)
|
|
}
|
|
}
|
|
})),
|
|
).Case(
|
|
"google", bond.Redirect("https://google.com", statuses.Found),
|
|
).Case(
|
|
"web", bond.StaticDir("./static"),
|
|
).Case(
|
|
"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.AbsPath(),
|
|
c.PathPrefix(),
|
|
c.Path(),
|
|
c.ContentType(),
|
|
)
|
|
c.Printf("Query:\n")
|
|
for k, vs := range c.Query() {
|
|
c.Printf("\t%q:\n", k)
|
|
for _, v := range vs {
|
|
c.Printf("\t\t%q\n", v)
|
|
}
|
|
}
|
|
}),
|
|
).Case(
|
|
"get-notes",
|
|
bond.Method().Case(
|
|
methods.Get,
|
|
bond.Func(func(c *bond.Context) {
|
|
opts := GetNotesOptions{}
|
|
c.Scan(&opts)
|
|
c.Printf("%v", opts)
|
|
}),
|
|
),
|
|
).Default(
|
|
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.AbsPath(),
|
|
c.PathPrefix(),
|
|
c.Path(),
|
|
c.ContentType(),
|
|
)
|
|
c.Printf("Query:\n")
|
|
for k, vs := range c.Query() {
|
|
c.Printf("\t%q:\n", k)
|
|
for _, v := range vs {
|
|
c.Printf("\t\t%q\n", v)
|
|
}
|
|
}
|
|
}),
|
|
))
|
|
|
|
func main() {
|
|
srv := bond.Server{
|
|
Addr: ":10800",
|
|
Handler: root,
|
|
}
|
|
err := srv.ListenAndServe()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|