Files
api-proxy/opencatd/opencat.go
Sakurasan 2ba63e226d up
2023-03-28 22:46:22 +08:00

173 lines
3.4 KiB
Go

package main
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type User struct {
IsDelete bool `json:"IsDelete"`
ID int `json:"id"`
UpdatedAt string `json:"updatedAt"`
Name string `json:"name"`
Token string `json:"token"`
CreatedAt string `json:"createdAt"`
}
type Key struct {
ID int `json:"id"`
Key string `json:"key"`
UpdatedAt string `json:"updatedAt"`
Name string `json:"name"`
CreatedAt string `json:"createdAt"`
}
var users = []User{
{false, 1, "2023-03-28T12:36:03.907590825Z", "root", "4930f144-7ffc-469b-8f2d-71a267358d87", "2023-03-28T11:52:45.593940257Z"},
}
var keys = []Key{
{1, "1234567890", "2023-03-28T12:53:41.124319079Z", "key1", "2023-03-28T12:53:41.124318928Z"},
}
func main() {
router := gin.Default()
// 初始化用户
router.POST("/1/users/init", func(c *gin.Context) {
resJSON := User{
false,
1,
"2023-03-28T14:13:54.969089197Z",
"root",
"3260dd53-463f-45d0-afa4-57acd04ee036",
"2023-03-28T14:13:54.969089107Z",
}
c.JSON(http.StatusOK, resJSON)
})
// 获取当前用户信息
router.GET("/1/me", func(c *gin.Context) {
resJSON := users[0]
c.JSON(http.StatusOK, resJSON)
})
// 获取所有Key
router.GET("/1/keys", func(c *gin.Context) {
c.JSON(http.StatusOK, keys)
})
// 获取所有用户信息
router.GET("/1/users", func(c *gin.Context) {
c.JSON(http.StatusOK, users)
})
// 添加Key
router.POST("/1/keys", func(c *gin.Context) {
var body Key
if err := c.BindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
keys = append(keys, body)
c.JSON(http.StatusCreated, body)
})
// 删除Key
router.DELETE("/1/keys/:id", func(c *gin.Context) {
id := c.Param("id")
var keyIdx int = -1
for idx, key := range keys {
if strconv.Itoa(key.ID) == id {
keyIdx = idx
break
}
}
if keyIdx == -1 {
c.JSON(http.StatusNotFound, gin.H{"error": "Key not found"})
return
}
keys = append(keys[:keyIdx], keys[keyIdx+1:]...)
c.JSON(http.StatusOK, gin.H{"message": "ok"})
})
// 添加用户
router.POST("/1/users", func(c *gin.Context) {
var body User
if err := c.BindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
newUser := User{
false,
len(users) + 1,
"2023-03-28T13:05:02.103135082Z",
body.Name,
GenerateToken(),
"2023-03-28T12:38:57.513105431Z",
}
users = append(users, newUser)
c.JSON(http.StatusCreated, newUser)
})
// 删除用户
router.DELETE("/1/users/:id", func(c *gin.Context) {
id := c.Param("id")
var userIdx int = -1
for idx, user := range users {
if strconv.Itoa(user.ID) == id {
userIdx = idx
break
}
}
if userIdx == -1 {
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
return
}
users = append(users[:userIdx], users[userIdx+1:]...)
c.JSON(http.StatusOK, gin.H{"message": "ok"})
})
// 重置用户Token
router.POST("/1/users/:id/reset", func(c *gin.Context) {
id := c.Param("id")
var userIdx int = -1
for idx, user := range users {
if strconv.Itoa(user.ID) == id {
userIdx = idx
break
}
}
if userIdx == -1 {
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
return
}
users[userIdx].Token = GenerateToken()
c.JSON(http.StatusOK, users[userIdx])
})
router.Run(":8080")
}
func GenerateToken() string {
token := uuid.New()
return token.String()
}