Skip to content

Commit

Permalink
refactored folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
KulwinderSingh07 committed Oct 22, 2023
1 parent 3a21e4e commit 1c84387
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
25 changes: 25 additions & 0 deletions controllers/block-controllers.go
Original file line number Diff line number Diff line change
@@ -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))
}
22 changes: 2 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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{
Expand All @@ -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
}
15 changes: 15 additions & 0 deletions routes/block-routes.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1c84387

Please sign in to comment.