-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_api_single.go
41 lines (35 loc) · 1006 Bytes
/
rest_api_single.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"fmt"
"log"
"net/http"
"encoding/json"
)
//str.115
type Employee struct {
Title string `json:"title"`
Name string `json:"name"`
Position []string `json:"position"`
}
type Employees []Employee
func homePage(w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "Welcome in home..")
fmt.Println("Endpoint hit: homePage")
}
//simple requests - not mux
func handleRequests() {
http.HandleFunc("/", homePage)
http.HandleFunc("/emp", returnAllEmployees)
log.Fatal(http.ListenAndServe(":9999", nil))
}
func returnAllEmployees(w http.ResponseWriter, r *http.Request){
employees := Employees{
Employee{Title:"spec1", Name:"test1", Position: []string{"admin1", "dev1"}},
Employee{Title:"spec1", Name:"test2", Position: []string{"admin2", "ops1"}},
}
fmt.Println("Endpoint hit: returnAllEmployees")
json.NewEncoder(w).Encode(employees)
}
func main() {
handleRequests()
}