-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
117 lines (97 loc) · 2.77 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Test HTTP server
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"math/rand"
"strconv"
"strings"
"log"
"net/http"
"github.com/gorilla/mux"
)
// The person Type
type Person struct {
ID string `json:"id,omitempty"`
Firstname string `json:"firstname,omitempty"`
Lastname string `json:"lastname,omitempty"`
}
var people []Person
// Display all from the people var
func GetPeople(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(people)
}
// Display a single data
func GetPerson(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for _, item := range people {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(&Person{})
}
// Create a new item
func CreatePerson(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var person Person
_ = json.NewDecoder(r.Body).Decode(&person)
person.ID = strconv.Itoa(rand.Intn(1000000))
people = append(people, person)
json.NewEncoder(w).Encode(&person)
}
// Delete an item
func DeletePerson(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range people {
if item.ID == params["id"] {
people = append(people[:index], people[index+1:]...)
break
}
}
json.NewEncoder(w).Encode(people)
}
func homePage(w http.ResponseWriter, r *http.Request) {
if basicAuth(w, r) {
fmt.Fprintf(w, "Welcome to the HomePage!")
} else {
http.Error(w, "authorization failed", http.StatusUnauthorized)
return
}
}
func basicAuth(w http.ResponseWriter, r *http.Request) bool {
auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
return false
}
payload, _ := base64.StdEncoding.DecodeString(auth[1])
pair := strings.SplitN(string(payload), ":", 2)
if len(pair) != 2 || !validate(pair[0], pair[1]) {
return false
}
return true
}
func validate(username, password string) bool {
if username == "test" && password == "test" {
return true
}
return false
}
// main function to boot up everything
func main() {
router := mux.NewRouter()
people = append(people, Person{ID: "1", Firstname: "John", Lastname: "Doe"})
people = append(people, Person{ID: "2", Firstname: "Jane", Lastname: "Doe"})
router.HandleFunc("/", homePage)
router.HandleFunc("/people", GetPeople).Methods("GET")
router.HandleFunc("/people/{id}", GetPerson).Methods("GET")
router.HandleFunc("/people", CreatePerson).Methods("POST")
router.HandleFunc("/people/{id}", DeletePerson).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8080", router))
}