kos/cmd/main.go

69 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-04-23 17:57:36 +08:00
package main
import (
"context"
"embed"
"flag"
2024-11-12 17:47:28 +08:00
"git.nobla.cn/golang/kos/entry/cli"
"git.nobla.cn/golang/kos/entry/http"
2023-08-25 09:53:45 +08:00
httpkg "net/http"
"time"
2023-06-11 11:19:01 +08:00
2024-11-12 17:47:28 +08:00
"git.nobla.cn/golang/kos"
2023-04-23 17:57:36 +08:00
)
//go:embed web
var webDir embed.FS
type subServer struct {
}
2024-01-18 17:11:44 +08:00
type users struct {
Name string `json:"name"`
Age int `json:"age"`
Tags []string `json:"tags"`
}
2023-04-23 17:57:36 +08:00
func (s *subServer) Start(ctx context.Context) (err error) {
2023-08-25 09:53:45 +08:00
kos.Http().Root("/web", httpkg.FS(webDir))
kos.Http().Handle(httpkg.MethodGet, "/hello", func(ctx *http.Context) (err error) {
return ctx.Success("Hello World")
})
2024-01-18 17:11:44 +08:00
kos.Command().Handle("/test", "test command", func(ctx *cli.Context) (err error) {
return ctx.Success([][]string{
[]string{"NAME", "AGE"},
[]string{"SSS", "aaa"},
})
})
2024-04-29 10:52:19 +08:00
2024-01-18 17:11:44 +08:00
kos.Command().Handle("/users", "test command", func(ctx *cli.Context) (err error) {
return ctx.Success([]*users{
{Name: "Zhan", Age: 10, Tags: []string{"a", "b"}},
{Name: "Lisi", Age: 15, Tags: []string{"c", "d"}},
})
})
kos.Command().Handle("/ctx", "context test", func(ctx *cli.Context) (err error) {
select {
case <-ctx.Context().Done():
case <-time.After(time.Second * 2):
}
return ctx.Success("OK")
})
2023-04-23 17:57:36 +08:00
return
}
func (s *subServer) Stop() (err error) {
return
}
func main() {
flag.Parse()
svr := kos.Init(
2024-11-12 17:47:28 +08:00
kos.WithName("git.nobla.cn/golang/test", "0.0.1"),
2023-04-23 17:57:36 +08:00
kos.WithServer(&subServer{}),
)
svr.Run()
}