|  | 
|  | 1 | +package main | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"encoding/json" | 
|  | 5 | +	"fmt" | 
|  | 6 | +	"net/http" | 
|  | 7 | +) | 
|  | 8 | + | 
|  | 9 | +func apiControllerTest(w http.ResponseWriter, r *http.Request) { | 
|  | 10 | +	fmt.Fprintf(w, "Server is working and you've requested %s\n", r.URL.Path) | 
|  | 11 | + | 
|  | 12 | +} | 
|  | 13 | + | 
|  | 14 | +func getNoOfUser(w http.ResponseWriter, r *http.Request) { | 
|  | 15 | +	data := map[string]string{ | 
|  | 16 | +		"name": "gaurav", | 
|  | 17 | +		"age": "20", | 
|  | 18 | +	} | 
|  | 19 | +	// Also, data is a map, so you can’t directly print it without formatting. | 
|  | 20 | +	fmt.Printf("%s", r.URL.Host) | 
|  | 21 | +	// fmt.Fprintf(w, "%v", data) | 
|  | 22 | + | 
|  | 23 | +	/* to set content-type header */ | 
|  | 24 | +	w.Header().Set("content/type", "application/json") | 
|  | 25 | + | 
|  | 26 | +	/* encoding the data into json*/ | 
|  | 27 | +	json.NewEncoder(w).Encode(data) | 
|  | 28 | +} | 
|  | 29 | + | 
|  | 30 | +func main() { | 
|  | 31 | + | 
|  | 32 | +	fmt.Println("/////////////Starting your server wait for a while\\\\\\\\\\\\\\") | 
|  | 33 | +	/* function to handle new request */ | 
|  | 34 | +	http.HandleFunc("/api", apiControllerTest) | 
|  | 35 | +	http.HandleFunc("/api/user/count", getNoOfUser) | 
|  | 36 | + | 
|  | 37 | +	// running the server at certain port address | 
|  | 38 | +	http.ListenAndServe(":8080", nil) | 
|  | 39 | + | 
|  | 40 | +} | 
0 commit comments