From cb2c3612d00ab7971c25c34cbfe5b35abdbf17f0 Mon Sep 17 00:00:00 2001 From: sp Date: Thu, 25 Feb 2021 12:20:27 +1100 Subject: [PATCH] api/v1 main entry point setup --- apiv1.go | 18 ++++++++++++++++++ debug.go | 14 ++++++++++++++ main.go | 17 +++-------------- 3 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 apiv1.go create mode 100644 debug.go diff --git a/apiv1.go b/apiv1.go new file mode 100644 index 0000000..ce942c4 --- /dev/null +++ b/apiv1.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "net/http" + "net/http/httputil" +) + +const apiV1Prefix = "/api/v1/" + +//apiV1Main version 1 main entry for all wechat callbacks +// +func apiV1Main(w http.ResponseWriter, r *http.Request) { + logRequestDebug(httputil.DumpRequest(r, true)) + + path := r.URL.Path[len(apiV1Prefix):] + fmt.Fprintf(w, "Protocol = %s, path= %s \n", r.Method, path) +} diff --git a/debug.go b/debug.go new file mode 100644 index 0000000..d9663fe --- /dev/null +++ b/debug.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + log "github.com/sirupsen/logrus" +) + +func logRequestDebug(data []byte, err error) { + if err == nil { + fmt.Printf("%s\n\n", string(data)) + } else { + log.Fatalf("%s\n\n", err) + } +} diff --git a/main.go b/main.go index 13b61dc..2f31032 100644 --- a/main.go +++ b/main.go @@ -32,26 +32,15 @@ func setupRootFileServer() { } func setupHTTPHandler() { - http.HandleFunc("/api", HelloHandler) - //http.HandleFunc("/upload", HelloHandler) - //http.HandleFunc("/crmfiles/", HelloHandler) - //http.HandleFunc("/dumprequest", HelloHandler) - //http.HandleFunc("/MP_verify_6JqVkftKr39GMakA.txt", HelloHandler) - //http.HandleFunc("/spa/redirect", HelloHandler) - //http.HandleFunc("/iapi/getAccessToken", HelloHandler) - //http.HandleFunc("/iapi/createWechatQr", HelloHandler) - //http.HandleFunc("/crmpixel.png", HelloHandler) //tracking pixel. - //http.HandleFunc("/crmcache", HelloHandler) - //http.HandleFunc("/spa/editprofile", HelloHandler) - //http.HandleFunc("/spa/livecast", HelloHandler) - //http.HandleFunc("/spa/editmeeting", HelloHandler) + http.HandleFunc(apiV1Prefix, apiV1Main) + http.HandleFunc("/dummy/", dummyHandler) log.Printf("Server started at %s:%s\n", config.Host, config.Port) log.Fatal(http.ListenAndServe(config.Host+":"+config.Port, nil)) } -func HelloHandler(w http.ResponseWriter, r *http.Request) { +func dummyHandler(w http.ResponseWriter, r *http.Request) { p := loan.People{} p.FakeNew() fmt.Fprintf(w, "Hello, there %s, %+v\n", loan.Version, p)