From 95fb5f714e5c466860c8e529043a9e2809b3ac15 Mon Sep 17 00:00:00 2001 From: masterhc Date: Wed, 17 Jul 2024 11:26:42 +0100 Subject: [PATCH] separation of concerns attempt --- controllers/controller.go | 53 +++++++++++++++++++++++++++++++++++++++ main.go | 50 ++++++++---------------------------- routes/router.go | 18 +++++++++++++ 3 files changed, 82 insertions(+), 39 deletions(-) create mode 100644 controllers/controller.go create mode 100644 routes/router.go diff --git a/controllers/controller.go b/controllers/controller.go new file mode 100644 index 0000000..49230d9 --- /dev/null +++ b/controllers/controller.go @@ -0,0 +1,53 @@ +package controllers + +import ( + "encoding/json" + "fmt" + "net/http" + "github.com/gorilla/mux" +) + +// User represents a user in the system +type User struct { + ID int `json:"id"` + Name string `json:"name"` +} + +// GetUsers handles the GET /users route +func GetUsers(w http.ResponseWriter, r *http.Request) { + users := []User{ + {ID: 1, Name: "John Doe"}, + {ID: 2, Name: "Jane Doe"}, + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(users) +} + +// GetUser handles the GET /users/{id} route +func GetUser(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + id := vars["id"] + + user := User{ID: 1, Name: "John Doe"} // Placeholder user + + if id == "1" { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(user) + } else { + http.Error(w, "User not found", http.StatusNotFound) + } +} + +// CreateUser handles the POST /users route +func CreateUser(w http.ResponseWriter, r *http.Request) { + var user User + + err := json.NewDecoder(r.Body).Decode(&user) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + fmt.Fprintf(w, "Received user: %+v", user) +} diff --git a/main.go b/main.go index a6aa13f..6efbba5 100644 --- a/main.go +++ b/main.go @@ -1,47 +1,19 @@ package main import ( - "encoding/json" - "net/http" - "github.com/gorilla/mux" - "log" + "log" + "net/http" + "yaagobackend/routes" // Import local package (assuming router.go is in a subdirectory named 'routes') + "github.com/gorilla/mux" ) -type User struct -{ - ID int `json:"id"` - Name string `json:"name"` -} - -func GetUsers(w http.ResponseWriter, r *http.Request){ - //Place holder values - users := []User{ - {ID:1, Name:"John Doe"}, - {ID:2, Name:"John Doe"}, - } - - w.Header().Set("Content-Type", "application/json"); - json.NewEncoder(w).Encode(users); -} - +func main() { + r := mux.NewRouter() -func GetUser(w http.ResponseWriter, r *http.Request){ - vars := mux.Vars(r) //Grabs var from the route variable - id := vars["id"] - user := User{ID:1, Name:"John Doe"} //Placeholder + // Initialize routes from router.go + routes.InitializeRoutes(r) - if(id=="1"){ - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(user) - }else{ - http.Error(w, "User not found", http.StatusNotFound) - } + // Start the server + log.Println("Server listening on port 8000") + log.Fatal(http.ListenAndServe(":8000", r)) } - func main(){ - r := mux.NewRouter() - r.HandleFunc("/users", GetUsers).Methods("GET") - r.HandleFunc("/users/{id}", GetUsers).Methods("GET") - - log.Println("Server listening on port 8000"); - http.ListenAndServe(":8000", r) - } \ No newline at end of file diff --git a/routes/router.go b/routes/router.go new file mode 100644 index 0000000..2f5046b --- /dev/null +++ b/routes/router.go @@ -0,0 +1,18 @@ +package routes + +import ( + "github.com/gorilla/mux" + "yaagobackend/controllers" // Import local package (assuming router.go is in a subdirectory named 'routes') +) + +// InitializeRouter sets up the router with all the routes +func InitializeRouter() *mux.Router { + r := mux.NewRouter() + + // Define routes and their handlers + r.HandleFunc("/users", GetUsers).Methods("GET") + r.HandleFunc("/users/{id}", GetUser).Methods("GET") + r.HandleFunc("/users", CreateUser).Methods("POST") + + return r +}