From 1c843876f701655147b89d3ba6817201ad885ece Mon Sep 17 00:00:00 2001 From: Kulwinder Date: Sun, 22 Oct 2023 22:26:06 +0530 Subject: [PATCH] refactored folder structure --- controllers/block-controllers.go | 25 +++++++++++++++++++++++++ main.go | 22 ++-------------------- routes/block-routes.go | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 controllers/block-controllers.go create mode 100644 routes/block-routes.go diff --git a/controllers/block-controllers.go b/controllers/block-controllers.go new file mode 100644 index 0000000..d687b00 --- /dev/null +++ b/controllers/block-controllers.go @@ -0,0 +1,25 @@ +package controllers + +import ( + "encoding/json" + "io" + "net/http" +) + +func HandleGetBlockchain(res http.ResponseWriter, req *http.Request) { + data, err := json.Marshal("hello") + if err != nil { + http.Error(res, err.Error(), http.StatusInternalServerError) + return + } + io.WriteString(res, string(data)) +} + +func HandleWriteBlock(res http.ResponseWriter, req *http.Request) { + data, err := json.Marshal("hello ji") + if err != nil { + http.Error(res, err.Error(), http.StatusInternalServerError) + return + } + io.WriteString(res, string(data)) +} diff --git a/main.go b/main.go index 0c02fab..fac45af 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,13 @@ package main import ( - "encoding/json" - "io" "log" "net/http" "os" "time" "github.com/KulwinderSingh07/POW-Blockchain/model" - "github.com/gorilla/mux" + "github.com/KulwinderSingh07/POW-Blockchain/routes" "github.com/joho/godotenv" ) @@ -26,7 +24,7 @@ func main() { } func run() error { - mux := makeMuxRouter() + mux := routes.CreateMuxRoutes() httpPort := os.Getenv("PORT") log.Println("Http server Listening on port :", httpPort) s := &http.Server{ @@ -41,19 +39,3 @@ func run() error { } return nil } - -func testing(res http.ResponseWriter, req *http.Request) { - data, err := json.Marshal("hello") - if err != nil { - http.Error(res, err.Error(), http.StatusInternalServerError) - return - } - io.WriteString(res, string(data)) -} - -func makeMuxRouter() http.Handler { - muxRouter := mux.NewRouter() - muxRouter.HandleFunc("/", testing).Methods("GET") - muxRouter.HandleFunc("/", testing).Methods("POST") - return muxRouter -} diff --git a/routes/block-routes.go b/routes/block-routes.go new file mode 100644 index 0000000..21b7ff0 --- /dev/null +++ b/routes/block-routes.go @@ -0,0 +1,15 @@ +package routes + +import ( + "net/http" + + "github.com/KulwinderSingh07/POW-Blockchain/controllers" + "github.com/gorilla/mux" +) + +func CreateMuxRoutes() http.Handler { + muxRouter := mux.NewRouter() + muxRouter.HandleFunc("/", controllers.HandleGetBlockchain).Methods("GET") + muxRouter.HandleFunc("/", controllers.HandleWriteBlock).Methods("POST") + return muxRouter +}