implement basic localdb function

This commit is contained in:
2025-09-08 19:18:24 +00:00
parent f8b6384db7
commit fd94106ef1
6 changed files with 123 additions and 29 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
go.sum
localdb.json

37
go.mod
View File

@@ -1,35 +1,34 @@
module user-manager-api
go 1.23.2
go 1.24
require github.com/gin-gonic/gin v1.10.0
require github.com/gin-gonic/gin v1.10.1
require (
github.com/bytedance/sonic v1.12.8 // indirect
github.com/bytedance/sonic/loader v0.2.3 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gin-contrib/sse v1.0.0 // indirect
github.com/bytedance/gopkg v0.1.3 // indirect
github.com/bytedance/sonic v1.14.1 // indirect
github.com/bytedance/sonic/loader v0.3.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/gabriel-vasile/mimetype v1.4.10 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.24.0 // indirect
github.com/go-playground/validator/v10 v10.27.0 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/knz/go-libedit v1.10.1 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.14.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
github.com/ugorji/go/codec v1.3.0 // indirect
golang.org/x/arch v0.20.0 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/net v0.43.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/text v0.28.0 // indirect
google.golang.org/protobuf v1.36.8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

20
main.go
View File

@@ -16,17 +16,19 @@ func main() {
})
})
users := GetLocaldb("localdb.json")
router.GET("/users", func(c *gin.Context) {})
router.POST("/users/:userid", func(c *gin.Context) {
var user User
if err := c.ShouldBind(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
router.POST("/users/:userid", func(c *gin.Context) {})
router.GET("/users/:userid", func(c *gin.Context) {
userid := c.Param("userid")
user, ok := users[userid]
if ok {
c.JSON(http.StatusOK, user)
} else {
c.JSON(http.StatusNotFound, nil)
}
user.Id = c.Param("userid")
c.JSON(http.StatusOK, user)
})
router.GET("/users/:userid", func(c *gin.Context) {})
router.DELETE("/users/:userid", func(c *gin.Context) {})
router.GET("/groups", func(c *gin.Context) {})
@@ -37,5 +39,5 @@ func main() {
router.POST("/groups/:groupid/members/:userid", func(c *gin.Context) {})
router.DELETE("/groups/:groupid/members/:userid", func(c *gin.Context) {})
router.Run("0.0.0.0:80") // listen and serve on 0.0.0.0:8080
router.Run("0.0.0.0:8083") // listen and serve on 0.0.0.0:8080
}

View File

@@ -1,5 +1,6 @@
package main
/*
type User struct {
Id string
Password string `form:"userpassword" binding:"required"`
@@ -9,3 +10,4 @@ type User struct {
Cluster struct{}
Templates struct{}
}
*/

70
types.go Normal file
View File

@@ -0,0 +1,70 @@
package main
type User struct {
DN string `json:"dn"`
UID string `json:"uid"`
CN string `json:"cn"`
SN string `json:"sn"`
Mail string `json:"mail"`
MemberOf []string `json:"memberOf"`
Resources map[string]any `json:"resources"`
Cluster Cluster `json:"cluster"`
Templates Templates `json:"templates"`
}
type Cluster struct {
Admin bool `json:"admin"`
Nodes map[string]bool `json:"nodes"`
VMID VMID `json:"vmid"`
Pools map[string]bool `json:"pools"`
Backups Backups `json:"backups"`
}
type VMID struct {
Min int `json:"min"`
MAx int `json:"max"`
}
type Backups struct {
Max int `json:"max"`
}
type Templates struct {
Instances struct {
LXC map[string]ResourceTemplate `json:"lxc"`
QEMU map[string]ResourceTemplate `json:"qemu"`
} `json:"instances"`
}
type SimpleResource struct {
Limits struct {
Global SimpleLimit `json:"global"`
Nodes map[string]SimpleLimit `json:"nodes"`
} `json:"limits"`
}
type SimpleLimit struct {
Max int `json:"max"`
}
type MatchResource struct {
Limits struct {
Global []MatchLimit `json:"global"`
Nodes map[string][]MatchLimit `json:"nodes"`
} `json:"limits"`
}
type MatchLimit struct {
Match string `json:"match"`
Name string `json:"name"`
Max int `json:"max"`
}
type ResourceTemplate struct {
Value string `json:"value"`
Resource struct {
Enabled bool `json:"enabled"`
Name string `json:"name"`
Amount int `json:"amount"`
} `json:"resource"`
}

20
utils.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import (
"encoding/json"
"log"
"os"
)
func GetLocaldb(dbPath string) map[string]User {
users := map[string]User{}
content, err := os.ReadFile(dbPath)
if err != nil {
log.Fatal("Error when opening file: ", err)
}
err = json.Unmarshal(content, &users)
if err != nil {
log.Fatal("Error during Unmarshal(): ", err)
}
return users
}