Files
go-socket/main.go
T

109 lines
6.1 KiB
Go

package main
import (
"context"
"log"
"net/http"
"strconv"
"go-socket/packages/config"
"go-socket/packages/httpRequest"
"go-socket/packages/minio"
"go-socket/packages/postgresql"
"go-socket/packages/wsServer"
)
func withCORS(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "token, Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS")
h(w, r)
}
}
func main() {
ctx := context.Background()
config.LoadConfFile()
postgresql.Init(ctx)
minio.Init(ctx)
http.HandleFunc("OPTIONS /", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "token, Content-Type")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS")
w.WriteHeader(http.StatusNoContent)
})
http.HandleFunc("POST /user", withCORS(httpRequest.HandleUserNew))
http.HandleFunc("DELETE /user", withCORS(httpRequest.HandleUserDelete))
http.HandleFunc("GET /user", withCORS(httpRequest.HandleUserGetUser))
http.HandleFunc("GET /users", withCORS(httpRequest.HandleUserGetUsers))
http.HandleFunc("PATCH /user/profile", withCORS(httpRequest.HandleUserModProfile))
http.HandleFunc("PATCH /user/avatar", withCORS(httpRequest.HandleSetUserAvatar))
http.HandleFunc("PATCH /user/profilebg", withCORS(httpRequest.HandleSetUserProfileBg))
http.HandleFunc("GET /user/avatar", withCORS(httpRequest.HandleGetUserAvatar))
http.HandleFunc("GET /user/profilebg", withCORS(httpRequest.HandleGetUserProfileBg))
http.HandleFunc("POST /token", withCORS(httpRequest.HandleUserNewToken))
http.HandleFunc("GET /connection", withCORS(httpRequest.HandleUserNewConnection))
http.HandleFunc("DELETE /connection", withCORS(httpRequest.HandleUserDeleteConnection))
http.HandleFunc("POST /connection/elevate", withCORS(httpRequest.HandleUserElevateConnection))
http.HandleFunc("POST /connection/deelevate", withCORS(httpRequest.HandleUserDeElevateConnection))
http.HandleFunc("GET /connections", withCORS(httpRequest.HandleUserGetConnections))
http.HandleFunc("GET /connection/messages", withCORS(httpRequest.HandleUserGetConnectionMessages))
http.HandleFunc("GET /connections/unreadmessages", withCORS(httpRequest.HandleUserGetConnectionsUnreadMessages))
http.HandleFunc("POST /file", withCORS(httpRequest.HandleAttachmentFileUpload))
http.HandleFunc("GET /file", withCORS(httpRequest.HandleAttachmentFileDownload))
http.HandleFunc("POST /hub", withCORS(httpRequest.HandleHubCreate))
http.HandleFunc("GET /hub", withCORS(httpRequest.GetHubData))
http.HandleFunc("POST /hub/channel/message", withCORS(httpRequest.HandleHubMessage))
http.HandleFunc("GET /hub/channel/messages", withCORS(httpRequest.HandleHubChannelGetMessages))
http.HandleFunc("GET /hub/channel", withCORS(httpRequest.GetChannelData))
http.HandleFunc("GET /hubs", withCORS(httpRequest.HandleGetHubs))
http.HandleFunc("GET /hubs/channels", withCORS(httpRequest.HandleGetChannels))
http.HandleFunc("GET /hubs/users", withCORS(httpRequest.HandleGetHubUsers))
http.HandleFunc("GET /hubs/roles", withCORS(httpRequest.GetRoles))
http.HandleFunc("PUT /hub/join", withCORS(httpRequest.HandleHubJoin))
http.HandleFunc("PATCH /hub/name", withCORS(httpRequest.HandleHubSetName))
http.HandleFunc("PATCH /hub/color", withCORS(httpRequest.HandleHubSetColor))
http.HandleFunc("PATCH /hub/icon", withCORS(httpRequest.HandleHubSetIcon))
http.HandleFunc("GET /hub/icon", withCORS(httpRequest.HandleGetHubIcon))
http.HandleFunc("PATCH /hub/bg", withCORS(httpRequest.HandleHubSetBg))
http.HandleFunc("PATCH /hub/joinrole", withCORS(httpRequest.HandleSetHubJoinRole))
http.HandleFunc("GET /hub/bg", withCORS(httpRequest.HandleGetHubBg))
http.HandleFunc("DELETE /hub", withCORS(httpRequest.HandleHubRemove))
http.HandleFunc("PATCH /hub/usercolorallowed", withCORS(httpRequest.HandleHubToggleUserColorAllowed))
http.HandleFunc("DELETE /hub/user", withCORS(httpRequest.HandleHubUserRemove))
http.HandleFunc("PATCH /hub/user/name", withCORS(httpRequest.HandleHubRenameUser))
http.HandleFunc("PATCH /hub/self/name", withCORS(httpRequest.HandleHubRenameSelf))
http.HandleFunc("PATCH /hub/user/mute", withCORS(httpRequest.HandleHubToggleMuteUser))
http.HandleFunc("PUT /hub/user/role", withCORS(httpRequest.HandleHubUserAddRole))
http.HandleFunc("DELETE /hub/user/role", withCORS(httpRequest.HandleHubUserRemoveRole))
http.HandleFunc("PUT /hub/role", httpRequest.HandleHubCreateRole)
http.HandleFunc("DELETE /hub/role", withCORS(httpRequest.HandleHubRemoveRole))
http.HandleFunc("PATCH /hub/role/name", withCORS(httpRequest.HandleRoleSetName))
http.HandleFunc("PATCH /hub/role/color", withCORS(httpRequest.HandleRoleSetColor))
http.HandleFunc("PATCH /hub/role/permissions", withCORS(httpRequest.HandleRoleSetPermissions))
http.HandleFunc("DELETE /hub/self/role", withCORS(httpRequest.HandleRoleSelfRemove))
http.HandleFunc("PUT /hub/channel", withCORS(httpRequest.HandleChannelCreate))
http.HandleFunc("DELETE /hub/channel", withCORS(httpRequest.HandleChannelRemove))
http.HandleFunc("PATCH /hub/channel/name", withCORS(httpRequest.HandleChannelSetName))
http.HandleFunc("PATCH /hub/channel/description", withCORS(httpRequest.HandleChannelSetDescription))
http.HandleFunc("PATCH /hub/channel/roles/view", withCORS(httpRequest.HandleChannelSetPermittedVisibleRole))
http.HandleFunc("PATCH /hub/channel/roles/send", withCORS(httpRequest.HandleChannelSetPermittedSendRole))
http.HandleFunc("PATCH /hub/channel/roles/history", withCORS(httpRequest.HandleChannelSetPermittedHistoryRole))
http.HandleFunc("PATCH /hub/channel/icon", withCORS(httpRequest.HandleChannelSetIcon))
http.HandleFunc("GET /hub/channel/icon", withCORS(httpRequest.HandleGetChannelIcon))
http.HandleFunc("POST /connection/message", withCORS(httpRequest.HandleDm))
http.HandleFunc("GET /ws", wsServer.ServeWsConnection)
http.Handle("GET /client/", http.StripPrefix("/client/", http.FileServer(http.Dir("test-client"))))
log.Println("beep boop; server server started")
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(int(config.Port)), nil))
}