update todo, delete uneccessary body http respons
This commit is contained in:
@@ -125,6 +125,15 @@ func DbSaveGroupWithoutClients(ctx context.Context, group *Group) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DbDeleteGroup deletes given group by id
|
||||||
|
// return: error if not successful
|
||||||
|
func DbDeleteGroup(ctx context.Context, group *Group) error {
|
||||||
|
_, err := dbConn.Exec(ctx, `
|
||||||
|
DELETE FROM chat_groups WHERE id = $1
|
||||||
|
`, group.Id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// DbSetGroupByIdWithoutClients sets all fields of given struct with database's data using id, populates Clients map with member ids but not their data
|
// DbSetGroupByIdWithoutClients sets all fields of given struct with database's data using id, populates Clients map with member ids but not their data
|
||||||
// return: error if not successful
|
// return: error if not successful
|
||||||
func DbSetGroupByIdWithoutClients(ctx context.Context, group *Group) error {
|
func DbSetGroupByIdWithoutClients(ctx context.Context, group *Group) error {
|
||||||
|
|||||||
@@ -6,10 +6,3 @@ const (
|
|||||||
BadMessage WsServerResponse = iota
|
BadMessage WsServerResponse = iota
|
||||||
InvalidCredentials
|
InvalidCredentials
|
||||||
)
|
)
|
||||||
|
|
||||||
var Colors = map[string][3]uint8{
|
|
||||||
"red": {255, 0, 0},
|
|
||||||
"green": {0, 255, 0},
|
|
||||||
"blue": {0, 0, 255},
|
|
||||||
"default": {255, 255, 255},
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ func HttpHandleNewToken(response http.ResponseWriter, request *http.Request) {
|
|||||||
response.Write([]byte(token))
|
response.Write([]byte(token))
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) {
|
func HttpHandeGroupCreate(response http.ResponseWriter, request *http.Request) {
|
||||||
if !isMethodAllowed(&response, request) {
|
if !isMethodAllowed(&response, request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -207,13 +207,6 @@ func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) {
|
|||||||
|
|
||||||
colorString := request.FormValue("color")
|
colorString := request.FormValue("color")
|
||||||
color, err := ConvertStringToRgb(colorString)
|
color, err := ConvertStringToRgb(colorString)
|
||||||
if err != nil {
|
|
||||||
var ok bool
|
|
||||||
color, ok = Colors[colorString]
|
|
||||||
if !ok {
|
|
||||||
color = Colors["default"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
group := Group{
|
group := Group{
|
||||||
Name: name,
|
Name: name,
|
||||||
@@ -238,6 +231,26 @@ func HttpHandeNewGroup(response http.ResponseWriter, request *http.Request) {
|
|||||||
fmt.Fprintf(response, "%d", group.Id)
|
fmt.Fprintf(response, "%d", group.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HttpHandleGroupRemove(response http.ResponseWriter, request *http.Request) {
|
||||||
|
if !isMethodAllowed(&response, request) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := request.Context()
|
||||||
|
_, group, err := getIfOwnerClientAndGroup(ctx, &response, request)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = DbDeleteGroup(ctx, group)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response.WriteHeader(http.StatusAccepted)
|
||||||
|
}
|
||||||
|
|
||||||
func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Request) {
|
func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Request) {
|
||||||
if !isMethodAllowed(&response, request) {
|
if !isMethodAllowed(&response, request) {
|
||||||
return
|
return
|
||||||
@@ -292,7 +305,6 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques
|
|||||||
}
|
}
|
||||||
|
|
||||||
response.WriteHeader(http.StatusAccepted)
|
response.WriteHeader(http.StatusAccepted)
|
||||||
response.Write([]byte("ok"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpHandleGroupRemoveClient(response http.ResponseWriter, request *http.Request) {
|
func HttpHandleGroupRemoveClient(response http.ResponseWriter, request *http.Request) {
|
||||||
@@ -410,7 +422,6 @@ func HttpHandleGroupChangeOwner(response http.ResponseWriter, request *http.Requ
|
|||||||
}
|
}
|
||||||
|
|
||||||
response.WriteHeader(http.StatusAccepted)
|
response.WriteHeader(http.StatusAccepted)
|
||||||
response.Write([]byte("changed"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
||||||
@@ -448,7 +459,6 @@ func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
response.WriteHeader(http.StatusAccepted)
|
response.WriteHeader(http.StatusAccepted)
|
||||||
response.Write([]byte("sent"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpHandleGroupsGetWithoutMembers(response http.ResponseWriter, request *http.Request) {
|
func HttpHandleGroupsGetWithoutMembers(response http.ResponseWriter, request *http.Request) {
|
||||||
|
|||||||
@@ -76,6 +76,7 @@
|
|||||||
<button onclick="showForm('new-message')">POST /new/message</button>
|
<button onclick="showForm('new-message')">POST /new/message</button>
|
||||||
<button onclick="showForm('get-groups')">POST /get/groups</button>
|
<button onclick="showForm('get-groups')">POST /get/groups</button>
|
||||||
<button onclick="showForm('get-members')">POST /get/group/members</button>
|
<button onclick="showForm('get-members')">POST /get/group/members</button>
|
||||||
|
<button onclick="showForm('del-group')" class="warn">POST /del/group</button>
|
||||||
<button onclick="showForm('websocket')">WS /ws</button>
|
<button onclick="showForm('websocket')">WS /ws</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -176,6 +177,14 @@
|
|||||||
],
|
],
|
||||||
submit: () => httpPost('/get/group/members', { token:'gm-token', group:'gm-group' })
|
submit: () => httpPost('/get/group/members', { token:'gm-token', group:'gm-group' })
|
||||||
},
|
},
|
||||||
|
'del-group': {
|
||||||
|
title: 'POST /del/group — delete group (owner only)',
|
||||||
|
fields: [
|
||||||
|
{ id: 'dg-token', label: 'token', ph: '' },
|
||||||
|
{ id: 'dg-groupid', label: 'groupid', ph: 'uint32' },
|
||||||
|
],
|
||||||
|
submit: () => httpPost('/del/group', { token:'dg-token', groupid:'dg-groupid' })
|
||||||
|
},
|
||||||
'websocket': {
|
'websocket': {
|
||||||
title: 'WS /ws — WebSocket connection',
|
title: 'WS /ws — WebSocket connection',
|
||||||
renderCustom: () => {
|
renderCustom: () => {
|
||||||
|
|||||||
@@ -19,14 +19,15 @@ func main() {
|
|||||||
|
|
||||||
http.HandleFunc("/new/client", withCORS(HttpHandleNewClient))
|
http.HandleFunc("/new/client", withCORS(HttpHandleNewClient))
|
||||||
http.HandleFunc("/new/token", withCORS(HttpHandleNewToken))
|
http.HandleFunc("/new/token", withCORS(HttpHandleNewToken))
|
||||||
http.HandleFunc("/new/group", withCORS(HttpHandeNewGroup))
|
http.HandleFunc("/new/group", withCORS(HttpHandeGroupCreate))
|
||||||
|
http.HandleFunc("/new/message", withCORS(HttpHandleNewMessage))
|
||||||
http.HandleFunc("/mod/group/addclients", withCORS(HttpHandleGroupAddClient))
|
http.HandleFunc("/mod/group/addclients", withCORS(HttpHandleGroupAddClient))
|
||||||
http.HandleFunc("/mod/group/removeclients", withCORS(HttpHandleGroupRemoveClient))
|
http.HandleFunc("/mod/group/removeclients", withCORS(HttpHandleGroupRemoveClient))
|
||||||
http.HandleFunc("/mod/group/color", withCORS(HttpHandleGroupChangeColor))
|
http.HandleFunc("/mod/group/color", withCORS(HttpHandleGroupChangeColor))
|
||||||
http.HandleFunc("/mod/group/owner", withCORS(HttpHandleGroupChangeOwner))
|
http.HandleFunc("/mod/group/owner", withCORS(HttpHandleGroupChangeOwner))
|
||||||
http.HandleFunc("/new/message", withCORS(HttpHandleNewMessage))
|
|
||||||
http.HandleFunc("/get/groups", withCORS(HttpHandleGroupsGetWithoutMembers))
|
http.HandleFunc("/get/groups", withCORS(HttpHandleGroupsGetWithoutMembers))
|
||||||
http.HandleFunc("/get/group/members", withCORS(HttpHandleGroupMembersGet))
|
http.HandleFunc("/get/group/members", withCORS(HttpHandleGroupMembersGet))
|
||||||
|
http.HandleFunc("/del/group", withCORS(HttpHandleGroupRemove))
|
||||||
http.HandleFunc("/ws", ServeWsConnection)
|
http.HandleFunc("/ws", ServeWsConnection)
|
||||||
|
|
||||||
log.Println("listening on :8080")
|
log.Println("listening on :8080")
|
||||||
|
|||||||
Reference in New Issue
Block a user