Files
go-socket/main.go
T

72 lines
3.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("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("POST /hub/message", withCORS(httpRequest.HandleHubMessage))
http.HandleFunc("GET /hubs", withCORS(httpRequest.HandleGetHubs))
http.HandleFunc("PUT /hub/join", withCORS(httpRequest.HandleHubJoin))
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("machine-client"))))
log.Println("beep boop; server server started")
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(int(config.Port)), nil))
}