package main import ( "fmt" "git.diulo.com/mogfee/kit/rest" "net/http" "os" "os/signal" ) func main() { srv := rest.NewServer(rest.RestConf{ Host: "localhost", Port: 8998, }, rest.WithCors("*")) srv.Use(func(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { fmt.Println("middleware") next(w, r) } }) srv.AddRoute(rest.Route{ Method: http.MethodGet, Path: "/api/v2/user/:name", Handler: func(w http.ResponseWriter, r *http.Request) { //var param struct { // Name string `json:"name"` // Age int `json:"age"` //} //if err := httpx.Parse(r, ¶m); err != nil { // fmt.Println(err) // return //} //fmt.Println(param) w.WriteHeader(200) w.Write([]byte("OK")) }, }) errChan := make(chan error, 1) go func() { // 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间) signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, os.Interrupt) errChan <- fmt.Errorf("%v", <-signalChan) }() go func() { errChan <- srv.Start() }() fmt.Println("started") fmt.Println(<-errChan) fmt.Println("stoping....") }