| 
									
										
										
										
											2025-09-21 00:10:17 +01:00
										 |  |  | package handlers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"log" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"go-api/utils" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ErrorResponse struct { | 
					
						
							|  |  |  | 	Message    string `json:"message"` | 
					
						
							|  |  |  | 	StatusCode int    `json:"statusCode"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type SuccessResponse struct { | 
					
						
							|  |  |  | 	Name string `json:"name"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func Handler(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 	log.Println("Running handler...") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-23 00:43:16 +01:00
										 |  |  | 	code := utils.GetStatusCode(r) | 
					
						
							| 
									
										
										
										
											2025-09-21 00:10:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	w.WriteHeader(code) | 
					
						
							|  |  |  | 	w.Header().Set("Content-Type", "application/json") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-21 00:19:23 +01:00
										 |  |  | 	if code != http.StatusOK { | 
					
						
							| 
									
										
										
										
											2025-09-21 00:10:17 +01:00
										 |  |  | 		json.NewEncoder(w).Encode(ErrorResponse{ | 
					
						
							| 
									
										
										
										
											2025-09-21 00:15:54 +01:00
										 |  |  | 			Message:    "There has been an error.", | 
					
						
							| 
									
										
										
										
											2025-09-21 00:11:22 +01:00
										 |  |  | 			StatusCode: code, | 
					
						
							| 
									
										
										
										
											2025-09-21 00:10:17 +01:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		json.NewEncoder(w).Encode(SuccessResponse{ | 
					
						
							|  |  |  | 			Name: "Oliver Davies", | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |