package main import ( "flag" "fmt" "io/ioutil" "net/http" "os" "time" "git.chuangxin1.com/csacred/toolkit" "git.chuangxin1.com/csacred/toolkit/example/cuser/service" khttp "git.chuangxin1.com/csacred/toolkit/http" "github.com/go-kit/kit/log" ) func main() { var addr = flag.String("listen", ":9122", "Address for HTTP server") logger := log.NewLogfmtLogger(os.Stderr) flag.Parse() readTimeout := 30 * time.Second writeTimeout := 30 * time.Second maxHeaderBytes := 1 << 20 router := khttp.NewRouter() khttp.AddGetHandle(router, "/ver", func(w http.ResponseWriter, r *http.Request) { ver := map[string]string{ "version": "0.2.0", "comments": "chuangxin1.com API", "author": "ls"} khttp.WriteJSON(w, toolkit.RowReplyData(ver)) }) khttp.AddPostHandle(router, "/upload", func(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() body, _ := ioutil.ReadAll(r.Body) fmt.Println(string(body)) khttp.WriteJSON(w, toolkit.OkReplyData()) }) dns := fmt.Sprintf( "root:Lsar-5211@tcp(127.0.0.1:3306)/community_user?charset=utf8mb4") dbConfig := toolkit.DbConfig{ Driver: `mysql`, DNS: dns, MaxOpenConns: 50, MaxIdle: 5, MaxLifetime: 0} toolkit.SetDbConfig(dbConfig) defer toolkit.FreeDB() srv := service.New() handers := service.GenServiceHandler(srv, logger) for i := range handers { fmt.Println(handers[i].Method, handers[i].Router) router.Handler(handers[i].Method, handers[i].Router, handers[i].Hander) } //serviceRouter(router, logger) //hs := make(HostSwitch) //hs["192.168.1.8:9122"] = router //toolkit.StartServer(addr, hs, readTimeout, writeTimeout, maxHeaderBytes, logger) khttp.ListenAndServe( *addr, router, readTimeout, writeTimeout, maxHeaderBytes, logger) }