-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpHandler.go
53 lines (43 loc) · 1.12 KB
/
httpHandler.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func httpHandler(writer http.ResponseWriter, request *http.Request) {
reqBody, err := ioutil.ReadAll(request.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", reqBody)
var reqData LoginRequest
if err := json.Unmarshal(reqBody, &reqData); err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", reqData)
fmt.Printf("Data: %s\n", reqData.UserName)
result := Login(reqData)
if !result.Success {
fmt.Errorf("login failed on server with user %v", reqData.UserName)
}
writer.Header().Set("Content-Type", "application/json")
/*var userR User
userR.Uid = result.Uid
userR.Uname = reqData.UserName
var chatRoomIds []ChatRoomId
for _, chatRoomItem := range result.JoinedChatRoom {
var cid ChatRoomId
cid = chatRoomItem.Cid
chatRoomIds = append(chatRoomIds, cid)
}
userR.JoinedChatRoom = chatRoomIds*/
json.NewEncoder(writer).Encode(result)
fmt.Println("welcome to kiki's chat service")
}
func main() {
http.HandleFunc("/login", httpHandler)
http.ListenAndServe(":8080", nil)
//log.Fatal(http.ListenAndServe(":8080", nil))
}