parent
218ded6960
commit
95fb5f714e
@ -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)
|
||||
}
|
@ -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)
|
||||
}
|
@ -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
|
||||
}
|
Loading…
Reference in new issue