ss/cmd/test/main.go

103 lines
1.7 KiB
Go
Raw Normal View History

2023-12-07 12:29:57 +03:00
package main
import (
2024-05-19 20:48:06 +03:00
"surdeus.su/core/ss"
2023-12-07 12:29:57 +03:00
)
2024-05-19 20:48:06 +03:00
import "surdeus.su/core/ss/methods"
import "surdeus.su/core/ss/contents"
import "surdeus.su/core/ss/statuses"
2023-12-07 12:29:57 +03:00
2024-01-03 21:23:03 +03:00
2024-05-19 20:48:06 +03:00
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)
}
2024-05-19 20:48:06 +03:00
}
})
var helloHandler = ss.Path(
// Default fallback handler.
testHandler,
).Case(
2024-05-19 20:48:06 +03:00
// Using the relative redirect to force us to the en.
"", ss.Redirect("en", statuses.Found),
).Case(
2024-05-19 20:48:06 +03:00
"en", ss.Func(func(c *ss.Context) {
c.Printf("Hello, World!")
}),
).Case(
2024-05-19 20:48:06 +03:00
"ru", ss.Func(func(c *ss.Context) {
c.Printf("Привет, Мир!")
2023-12-07 16:55:23 +03:00
}),
2024-05-19 20:48:06 +03:00
)
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(
2024-05-19 20:48:06 +03:00
"note",
ss.Method(
// The default handler.
nil,
).Case(
2024-01-03 21:23:03 +03:00
methods.Get,
2024-05-19 20:48:06 +03:00
nil,
).Case(
methods.Post,
ss.Func(func(c *ss.Context) {
2024-01-03 21:23:03 +03:00
opts := GetNotesOptions{}
c.Scan(&opts)
c.Printf("%v", opts)
}),
),
2024-05-19 20:48:06 +03:00
)
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,
))
2023-12-07 12:29:57 +03:00
func main() {
2024-05-19 20:48:06 +03:00
srv := ss.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)
}
}