|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "cth.release/common/types" |
| 5 | + "cth.release/common/utils" |
| 6 | + "github.com/gofiber/fiber/v2" |
| 7 | + "github.com/syndtr/goleveldb/leveldb" |
| 8 | +) |
| 9 | + |
| 10 | +func GetKey(ctx *fiber.Ctx) error { |
| 11 | + channel := ctx.Params("channel") |
| 12 | + key := ctx.Params("key") |
| 13 | + |
| 14 | + type GetKeyRequest struct { |
| 15 | + Username string `json:"username"` |
| 16 | + Password string `json:"password"` |
| 17 | + } |
| 18 | + |
| 19 | + var request GetKeyRequest |
| 20 | + if err := ctx.BodyParser(&request); err != nil { |
| 21 | + return types.Fail(ctx, 400, "Invalid Request") |
| 22 | + } |
| 23 | + |
| 24 | + userDb, err := leveldb.OpenFile("data/users.db", nil) |
| 25 | + if err != nil { |
| 26 | + return types.Fail(ctx, 500, "Error Opening User Database") |
| 27 | + } |
| 28 | + defer userDb.Close() |
| 29 | + |
| 30 | + userData, err := userDb.Get([]byte(request.Username), nil) |
| 31 | + if err != nil { |
| 32 | + return types.Fail(ctx, 404, "User not found") |
| 33 | + } |
| 34 | + |
| 35 | + if string(userData) != utils.Sha512Hex(request.Password) { |
| 36 | + return types.Fail(ctx, 401, "Invalid Password") |
| 37 | + } |
| 38 | + |
| 39 | + db, err := leveldb.OpenFile("data/data."+channel+".db", nil) |
| 40 | + if err != nil { |
| 41 | + return types.Fail(ctx, 500, "Error Opening Database") |
| 42 | + } |
| 43 | + defer db.Close() |
| 44 | + |
| 45 | + data, err := db.Get([]byte(key), nil) |
| 46 | + |
| 47 | + if err != nil { |
| 48 | + return types.Fail(ctx, 404, "Key not found") |
| 49 | + } |
| 50 | + |
| 51 | + return types.Success(ctx, 200, string(data)) |
| 52 | +} |
| 53 | + |
| 54 | +func PutKey(ctx *fiber.Ctx) error { |
| 55 | + channel := ctx.Params("channel") |
| 56 | + key := ctx.Params("key") |
| 57 | + |
| 58 | + // 구조체 { value: string } |
| 59 | + type PutKeyRequest struct { |
| 60 | + Username string `json:"username"` |
| 61 | + Password string `json:"password"` |
| 62 | + Value string `json:"value"` |
| 63 | + } |
| 64 | + |
| 65 | + var request PutKeyRequest |
| 66 | + if err := ctx.BodyParser(&request); err != nil { |
| 67 | + return types.Fail(ctx, 400, "Invalid Request") |
| 68 | + } |
| 69 | + |
| 70 | + userDb, err := leveldb.OpenFile("data/users.db", nil) |
| 71 | + if err != nil { |
| 72 | + return types.Fail(ctx, 500, "Error Opening User Database") |
| 73 | + } |
| 74 | + defer userDb.Close() |
| 75 | + |
| 76 | + userData, err := userDb.Get([]byte(request.Username), nil) |
| 77 | + if err != nil { |
| 78 | + return types.Fail(ctx, 404, "User not found") |
| 79 | + } |
| 80 | + |
| 81 | + if string(userData) != utils.Sha512Hex(request.Password) { |
| 82 | + return types.Fail(ctx, 401, "Invalid Password") |
| 83 | + } |
| 84 | + |
| 85 | + db, err := leveldb.OpenFile("data/data."+channel+".db", nil) |
| 86 | + if err != nil { |
| 87 | + return types.Fail(ctx, 500, "Error Opening Database") |
| 88 | + } |
| 89 | + |
| 90 | + defer db.Close() |
| 91 | + |
| 92 | + err = db.Put([]byte(key), []byte(request.Value), nil) |
| 93 | + |
| 94 | + if err != nil { |
| 95 | + return types.Fail(ctx, 500, "Error Putting Key") |
| 96 | + } |
| 97 | + |
| 98 | + return types.Success(ctx, 200, "Key put successfully") |
| 99 | +} |
| 100 | + |
| 101 | +func DeleteKey(ctx *fiber.Ctx) error { |
| 102 | + channel := ctx.Params("channel") |
| 103 | + key := ctx.Params("key") |
| 104 | + |
| 105 | + type DeleteKeyRequest struct { |
| 106 | + Username string `json:"username"` |
| 107 | + Password string `json:"password"` |
| 108 | + } |
| 109 | + |
| 110 | + var request DeleteKeyRequest |
| 111 | + if err := ctx.BodyParser(&request); err != nil { |
| 112 | + return types.Fail(ctx, 400, "Invalid Request") |
| 113 | + } |
| 114 | + |
| 115 | + userDb, err := leveldb.OpenFile("data/users.db", nil) |
| 116 | + if err != nil { |
| 117 | + return types.Fail(ctx, 500, "Error Opening User Database") |
| 118 | + } |
| 119 | + defer userDb.Close() |
| 120 | + |
| 121 | + userData, err := userDb.Get([]byte(request.Username), nil) |
| 122 | + if err != nil { |
| 123 | + return types.Fail(ctx, 404, "User not found") |
| 124 | + } |
| 125 | + |
| 126 | + if string(userData) != utils.Sha512Hex(request.Password) { |
| 127 | + return types.Fail(ctx, 401, "Invalid Password") |
| 128 | + } |
| 129 | + |
| 130 | + db, err := leveldb.OpenFile("data/data."+channel+".db", nil) |
| 131 | + if err != nil { |
| 132 | + return types.Fail(ctx, 500, "Error Opening Database") |
| 133 | + } |
| 134 | + |
| 135 | + defer db.Close() |
| 136 | + |
| 137 | + err = db.Delete([]byte(key), nil) |
| 138 | + |
| 139 | + if err != nil { |
| 140 | + return types.Fail(ctx, 500, "Error Deleting Key") |
| 141 | + } |
| 142 | + |
| 143 | + return types.Success(ctx, 200, "Key deleted successfully") |
| 144 | +} |
0 commit comments