-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitServer.go
63 lines (50 loc) · 1.63 KB
/
initServer.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type dummyResponse struct {
Message string `json:"message"`
From string `json:"from"`
}
func secretHandler(w http.ResponseWriter, r *http.Request) {
if _,err := checkCookie(w, r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
fmt.Println(err)
return
}
dummyData := dummyResponse{"We are so excited to meet you", "invictus"}
// Status OK
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(dummyData)
// success
fmt.Println("Secret content Delievered to someone") // modify later to get name
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
// Valid Methods
if r.Method != "GET" {
http.Error(w, "Method is not Supported.", http.StatusMethodNotAllowed)
fmt.Println("Invlid Method")
// Error method does not necessarly close the request. Need to return.
return
}
w.WriteHeader(200)
fmt.Fprintf(w, "Hii there, Move to either login or signup")
}
func startServer() {
// Handle incoming requests
http.HandleFunc("/", homeHandler) // anyone
http.HandleFunc("/login", loginHandler) // anyone
http.HandleFunc("/signup", signupHandler) // anyone
http.HandleFunc("/secret", secretHandler) // anyone
http.HandleFunc("/award", awardHandler) // admin only
http.HandleFunc("/transfer", transferHandler) // only sender can request
http.HandleFunc("/balance", balanceHandler) // anyone
// Start the server
fmt.Println("Starting Server at http://localhost:8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalln("Listen and server error:", err)
}
}