ss/cmd/test/main.go

80 lines
1.4 KiB
Go
Raw Normal View History

2023-12-07 12:29:57 +03:00
package main
import (
2024-01-09 07:40:32 +03:00
"vultras.su/core/bond"
"vultras.su/core/bond/methods"
"vultras.su/core/bond/contents"
"vultras.su/core/bond/statuses"
2023-12-07 12:29:57 +03:00
)
2024-01-03 21:23:03 +03:00
type GetNotesOptions struct {
2024-01-09 07:40:32 +03:00
Id int `json:"id"`
2024-01-03 21:23:03 +03:00
Name string `json:"name"`
}
var root = bond.Root(bond.Path().
Def(
"",
bond.StaticFile("index.html"),
).Def(
2023-12-07 12:29:57 +03:00
"hello",
bond.Path().Def(
// Using the relative redirect to force us to the en.
"", bond.Redirect("en", statuses.Found),
).Def(
"en", bond.Func(func(c *bond.Context) {
2024-01-03 21:23:03 +03:00
c.Printf("Hello, World!")
2023-12-07 12:29:57 +03:00
}),
).Def(
"ru", bond.Func(func(c *bond.Context) {
2024-01-03 21:23:03 +03:00
c.Printf("Привет, Мир!")
2023-12-07 12:29:57 +03:00
}),
),
2023-12-07 13:22:11 +03:00
).Def(
"google", bond.Redirect("https://google.com", statuses.Found),
).Def(
"web", bond.StaticDir("./static"),
2023-12-07 16:55:23 +03:00
).Def(
"test", bond.Func(func(c *bond.Context) {
2024-01-03 21:23:03 +03:00
c.SetContentType(contents.Plain)
2023-12-07 16:55:23 +03:00
c.Printf(
"AbsPath: %q\n" +
"Prefix: %q\n" +
2023-12-14 21:59:41 +03:00
"Path: %q\n"+
"Content-Type: %q\n",
c.AbsPath(),
c.PathPrefix(),
c.Path(),
c.ContentType(),
2023-12-07 16:55:23 +03:00
)
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)
}
}
}),
2024-01-03 21:23:03 +03:00
).Def(
"get-notes",
bond.Method().Def(
methods.Get,
2024-01-09 07:40:32 +03:00
bond.Func(func(c *bond.Context) {
2024-01-03 21:23:03 +03:00
opts := GetNotesOptions{}
c.Scan(&opts)
c.Printf("%v", opts)
}),
),
))
2023-12-07 12:29:57 +03:00
func main() {
srv := bond.Server{
2023-12-14 21:59:41 +03:00
Addr: ":10800",
2023-12-07 12:29:57 +03:00
Handler: root,
}
err := srv.ListenAndServe()
if err != nil {
panic(err)
}
}