146 lines
3.0 KiB
Go
146 lines
3.0 KiB
Go
package main
|
|
|
|
import "net/http"
|
|
|
|
func configHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Write(formatJson(parsecService))
|
|
}
|
|
|
|
func externalHandler(w http.ResponseWriter, r *http.Request) {
|
|
peerId := r.FormValue("peer")
|
|
addr := r.FormValue("addr")
|
|
if peerId == "" {
|
|
w.Write([]byte("Peer is empty"))
|
|
return
|
|
}
|
|
peer := parsecService.GetPeer(peerId)
|
|
if peer == nil {
|
|
w.Write([]byte("Peer not found"))
|
|
return
|
|
}
|
|
peer.External = addr
|
|
w.Write([]byte("Success"))
|
|
}
|
|
|
|
func addUserHandler(w http.ResponseWriter, r *http.Request) {
|
|
username := r.FormValue("user")
|
|
password := r.FormValue("pass")
|
|
|
|
if username == "" {
|
|
w.Write([]byte("user can not be empty"))
|
|
return
|
|
}
|
|
|
|
if password == "" {
|
|
w.Write([]byte("pass can not be empty"))
|
|
return
|
|
}
|
|
user := parsecService.GetUserByEmail(username)
|
|
if user != nil {
|
|
w.Write([]byte("user exist"))
|
|
return
|
|
}
|
|
|
|
parsecService.AddUser(username, password)
|
|
w.Write([]byte("Success"))
|
|
}
|
|
|
|
func editUserHandler(w http.ResponseWriter, r *http.Request) {
|
|
username := r.FormValue("user")
|
|
password := r.FormValue("pass")
|
|
|
|
if username == "" {
|
|
w.Write([]byte("user can not be empty"))
|
|
return
|
|
}
|
|
|
|
if password == "" {
|
|
w.Write([]byte("pass can not be empty"))
|
|
return
|
|
}
|
|
user := parsecService.GetUserByEmail(username)
|
|
if user == nil {
|
|
w.Write([]byte("user not exist"))
|
|
return
|
|
}
|
|
|
|
user.Password = password
|
|
w.Write([]byte("Success"))
|
|
}
|
|
|
|
func removeUserHandler(w http.ResponseWriter, r *http.Request) {
|
|
username := r.FormValue("user")
|
|
|
|
if username == "" {
|
|
w.Write([]byte("user can not be empty"))
|
|
return
|
|
}
|
|
|
|
user := parsecService.GetUserByEmail(username)
|
|
if user == nil {
|
|
w.Write([]byte("user not exist"))
|
|
return
|
|
}
|
|
|
|
parsecService.RemoveUser(username)
|
|
parsecService.RemovePeerByUser(username)
|
|
parsecService.RemoveSessionByUser(username)
|
|
parsecService.RemoveWsSessionByUser(username)
|
|
w.Write([]byte("Success"))
|
|
}
|
|
|
|
func publicHandler(w http.ResponseWriter, r *http.Request) {
|
|
peerId := r.FormValue("peer")
|
|
public := r.FormValue("public")
|
|
|
|
if peerId == "" {
|
|
w.Write([]byte("peer can not be empty"))
|
|
return
|
|
}
|
|
|
|
if public == "" {
|
|
w.Write([]byte("public can not be empty"))
|
|
return
|
|
}
|
|
|
|
peer := parsecService.GetPeer(peerId)
|
|
if peer == nil {
|
|
w.Write([]byte("peer not exist"))
|
|
return
|
|
}
|
|
peer.Public = public == "1"
|
|
w.Write([]byte("Success"))
|
|
}
|
|
|
|
func assignHandler(w http.ResponseWriter, r *http.Request) {
|
|
peer := r.FormValue("peer")
|
|
user := r.FormValue("user")
|
|
action := r.FormValue("action")
|
|
|
|
if peer == "" || action == "" {
|
|
w.Write([]byte("peer and action can not be empty"))
|
|
return
|
|
}
|
|
if action != "clear" && user == "" {
|
|
w.Write([]byte("user can not be empty"))
|
|
return
|
|
}
|
|
switch action {
|
|
case "add":
|
|
parsecService.AssignAdd(peer, user)
|
|
break
|
|
case "remove":
|
|
parsecService.AssignRemove(peer, user)
|
|
break
|
|
case "clear":
|
|
parsecService.AssignClear(peer)
|
|
break
|
|
}
|
|
w.Write([]byte("Success"))
|
|
}
|
|
|
|
func trimHandler(w http.ResponseWriter, r *http.Request) {
|
|
parsecService.RemoveOfflinePeers()
|
|
w.Write([]byte("Success"))
|
|
}
|