From 04fb03b8756a2080dffd01d85db48d47c7d8e992 Mon Sep 17 00:00:00 2001 From: sp Date: Wed, 24 Feb 2021 23:50:54 +1100 Subject: [PATCH] initial workable go project --- .idea/SFM_Loan_RestApi.iml | 1 + config.go | 29 ++++++++++++++++++++++++++ config.json | 5 +++++ go.mod | 14 +++++++++++++ main.go | 42 +++++++++++++++++++++++++++++++++++--- 5 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 config.go create mode 100644 config.json create mode 100644 go.mod diff --git a/.idea/SFM_Loan_RestApi.iml b/.idea/SFM_Loan_RestApi.iml index 5e764c4..0dc9279 100644 --- a/.idea/SFM_Loan_RestApi.iml +++ b/.idea/SFM_Loan_RestApi.iml @@ -2,6 +2,7 @@ + diff --git a/config.go b/config.go new file mode 100644 index 0000000..1d1c62f --- /dev/null +++ b/config.go @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/json" + log "github.com/sirupsen/logrus" + "io/ioutil" +) + +type configuration struct { + Host string + Port string + DocRoot string +} + +var configFile = "config.json" +var config = configuration{} + +func (m *configuration) readConfig() (e error) { + log.Printf("read Path config from %s\r\n", configFile) + body, e := ioutil.ReadFile(configFile) + if e != nil { + log.Fatal("Cannot read config from " + configFile) + return + } + e = json.Unmarshal(body, m) + + //TODO: check config before proceed further + return +} diff --git a/config.json b/config.json new file mode 100644 index 0000000..0f106ce --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "Host":"127.0.0.1", + "Port":"8080", + "DocRoot": "./html/" +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..37788cd --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module SFM_Loan_RestApi + +go 1.15 + +replace biukop.com/sfm/loan => /home/sp/GolandProjects/SFM-loan + +require ( + biukop.com/sfm/loan v0.0.0-00010101000000-000000000000 + github.com/VividCortex/mysqlerr v0.0.0-20201215173831-4c396ae82aac + github.com/brianvoe/gofakeit/v6 v6.0.1 + github.com/go-sql-driver/mysql v1.5.0 + github.com/sirupsen/logrus v1.7.0 + github.com/stretchr/testify v1.2.2 +) diff --git a/main.go b/main.go index be62989..c1dcb8e 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "biukop.com/sfm/loan" "fmt" "log" "net/http" @@ -8,11 +9,46 @@ import ( func main() { + err := config.readConfig() //wechat API config + if err != nil { + log.Println(err) + log.Fatalf("unable to read %s, program quit\n", configFile) + return + } + + setupRootFileServer() + //always the last one + setupHTTPHandler() + +} + +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("/", HelloHandler) - fmt.Println("Server started at port 8080") - log.Fatal(http.ListenAndServe(":8080", nil)) + fmt.Printf("Server started at port %s\n", config.Port) + log.Fatal(http.ListenAndServe(config.Host+":"+config.Port, nil)) + } func HelloHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello, there\n") + p := loan.People{} + p.FakeNew() + fmt.Fprintf(w, "Hello, there %s, %+v\n", loan.Version, p) }