ss/cmd/test/main.go
2024-05-19 22:48:06 +05:00

102 lines
1.7 KiB
Go

package main
import (
"surdeus.su/core/ss"
)
import "surdeus.su/core/ss/methods"
import "surdeus.su/core/ss/contents"
import "surdeus.su/core/ss/statuses"
var testHandler = ss.Func(func(c *ss.Context){
c.SetContentType(contents.Plain, contents.Utf8)
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)
}
}
})
var helloHandler = ss.Path(
// Default fallback handler.
testHandler,
).Case(
// Using the relative redirect to force us to the en.
"", ss.Redirect("en", statuses.Found),
).Case(
"en", ss.Func(func(c *ss.Context) {
c.Printf("Hello, World!")
}),
).Case(
"ru", ss.Func(func(c *ss.Context) {
c.Printf("Привет, Мир!")
}),
)
type GetNotesOptions struct {
Id int `json:"id"`
Author string `json:"author"`
Title string `json:"title"`
Text string `json:"text"`
}
var apiHandler = ss.Path(
nil,
).Case(
"note",
ss.Method(
// The default handler.
nil,
).Case(
methods.Get,
nil,
).Case(
methods.Post,
ss.Func(func(c *ss.Context) {
opts := GetNotesOptions{}
c.Scan(&opts)
c.Printf("%v", opts)
}),
),
)
var root = ss.Root(ss.Path(
testHandler,
).Case(
// Static file serving example.
"",
ss.StaticFile("index.html"),
).Case(
"hello",
helloHandler,
).Case(
"google", ss.Redirect("https://google.com", statuses.Found),
).Case(
"web", ss.StaticDir("./static"),
).Case(
"api",
apiHandler,
))
func main() {
srv := ss.Server{
Addr: ":10800",
Handler: root,
}
err := srv.ListenAndServe()
if err != nil {
panic(err)
}
}