initial refactor in go
This commit is contained in:
3
users/go.mod
Normal file
3
users/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module acooldomain.co/backend/users
|
||||
|
||||
go 1.22.0
|
79
users/users.go
Normal file
79
users/users.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type Permission int
|
||||
|
||||
const (
|
||||
Start Permission = 1 << iota
|
||||
Stop
|
||||
Browse
|
||||
Create
|
||||
Delete
|
||||
RunCommand
|
||||
Admin
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
MaxOwnedServers int `json:"maxed_owned_servers"`
|
||||
Permissions []string `json:"permissions"`
|
||||
}
|
||||
|
||||
type Connection struct {
|
||||
connection *mongo.Client
|
||||
}
|
||||
|
||||
func TranslatePermission(permission Permission, permissions_list *list.List) {
|
||||
if Start&permission == Start {
|
||||
permissions_list.PushFront("start")
|
||||
}
|
||||
if Stop&permission == Stop {
|
||||
permissions_list.PushFront("stop")
|
||||
}
|
||||
if Browse&permission == Browse {
|
||||
permissions_list.PushFront("browse")
|
||||
}
|
||||
if Create&permission == Create {
|
||||
permissions_list.PushFront("create")
|
||||
}
|
||||
if Delete&permission == Delete {
|
||||
permissions_list.PushFront("delete")
|
||||
}
|
||||
if RunCommand&permission == RunCommand {
|
||||
permissions_list.PushFront("runcommand")
|
||||
}
|
||||
if Admin&permission == Admin {
|
||||
permissions_list.PushFront("admin")
|
||||
}
|
||||
}
|
||||
|
||||
func (con Connection) GetUsers(c *gin.Context) {
|
||||
users, err := con.connection.Database("Backend").Collection("Users").Find(context.TODO(), bson.D{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var response []User
|
||||
|
||||
err = users.All(context.TODO(), &response)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func LoadGroup(group *gin.RouterGroup, client *mongo.Client) {
|
||||
connection := Connection{connection: client}
|
||||
group.GET("/", connection.GetUsers)
|
||||
}
|
Reference in New Issue
Block a user