-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started with serve command, yet incomplete
- Loading branch information
Thomas von Dein
committed
Dec 20, 2024
1 parent
37ca653
commit 0af53e4
Showing
8 changed files
with
305 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ type Config struct { | |
DB *app.DB | ||
File string | ||
Tags []string | ||
Listen string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
Copyright © 2023 Thomas von Dein | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package rest | ||
|
||
import ( | ||
//"github.com/alecthomas/repr" | ||
|
||
"github.com/gofiber/fiber/v3" | ||
"github.com/tlinden/anydb/app" | ||
"github.com/tlinden/anydb/cfg" | ||
) | ||
|
||
type SetContext struct { | ||
Query string `json:"query" form:"query"` | ||
} | ||
|
||
type ListResponse struct { | ||
Success bool | ||
Code int | ||
Entries app.DbEntries | ||
} | ||
|
||
type SingleResponse struct { | ||
Success bool | ||
Code int | ||
Entry *app.DbEntry | ||
} | ||
|
||
func RestList(c fiber.Ctx, conf *cfg.Config) error { | ||
// FIXME: Check for tags and filter | ||
// FIXME: https://github.com/gofiber/fiber/blob/main/docs/api/bind.md#body | ||
/* | ||
setcontext := new(SetContext) | ||
if err := c.Bind().Body(setcontext); err != nil { | ||
return JsonStatus(c, fiber.StatusForbidden, | ||
"Unable to parse body: "+err.Error()) | ||
} | ||
*/ | ||
|
||
// get list | ||
entries, err := conf.DB.List(&app.DbAttr{}) | ||
if err != nil { | ||
return JsonStatus(c, fiber.StatusForbidden, | ||
"Unable to list keys: "+err.Error()) | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON( | ||
ListResponse{ | ||
Success: true, | ||
Code: fiber.StatusOK, | ||
Entries: entries, | ||
}, | ||
) | ||
} | ||
|
||
func RestGet(c fiber.Ctx, conf *cfg.Config) error { | ||
if c.Params("key") == "" { | ||
return JsonStatus(c, fiber.StatusForbidden, | ||
"key not provided") | ||
} | ||
|
||
// get list | ||
entry, err := conf.DB.Get(&app.DbAttr{Key: c.Params("key")}) | ||
if err != nil { | ||
return JsonStatus(c, fiber.StatusForbidden, | ||
"Unable to get key: "+err.Error()) | ||
} | ||
if entry.Key == "" { | ||
return JsonStatus(c, fiber.StatusForbidden, | ||
"Key does not exist") | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON( | ||
SingleResponse{ | ||
Success: true, | ||
Code: fiber.StatusOK, | ||
Entry: entry, | ||
}, | ||
) | ||
} | ||
|
||
func RestDelete(c fiber.Ctx, conf *cfg.Config) error { | ||
if c.Params("key") == "" { | ||
return JsonStatus(c, fiber.StatusForbidden, | ||
"key not provided") | ||
} | ||
|
||
// get list | ||
err := conf.DB.Del(&app.DbAttr{Key: c.Params("key")}) | ||
if err != nil { | ||
return JsonStatus(c, fiber.StatusForbidden, | ||
"Unable to delete key: "+err.Error()) | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON( | ||
Result{ | ||
Success: true, | ||
Code: fiber.StatusOK, | ||
Message: "key deleted", | ||
}, | ||
) | ||
} | ||
|
||
// FIXME: add RestSet() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
Copyright © 2024 Thomas von Dein | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package rest | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v3" | ||
"github.com/gofiber/fiber/v3/middleware/compress" | ||
"github.com/gofiber/fiber/v3/middleware/cors" | ||
"github.com/gofiber/fiber/v3/middleware/logger" | ||
"github.com/gofiber/fiber/v3/middleware/requestid" | ||
"github.com/tlinden/anydb/cfg" | ||
) | ||
|
||
// used to return to the api client | ||
type Result struct { | ||
Success bool `json:"success"` | ||
Message string `json:"message"` | ||
Code int `json:"code"` | ||
} | ||
|
||
func Runserver(conf *cfg.Config, args []string) error { | ||
// setup api server | ||
router := SetupServer(conf) | ||
|
||
// public rest api routes | ||
api := router.Group("/anydb/v1") | ||
{ | ||
api.Get("/", func(c fiber.Ctx) error { | ||
return RestList(c, conf) | ||
}) | ||
|
||
api.Get("/:key", func(c fiber.Ctx) error { | ||
return RestGet(c, conf) | ||
}) | ||
|
||
api.Delete("/:key", func(c fiber.Ctx) error { | ||
return RestDelete(c, conf) | ||
}) | ||
/* | ||
api.Put("/", nil, func(c *fiber.Ctx) error { | ||
return RestSet(c, conf) | ||
}) | ||
*/ | ||
} | ||
|
||
// public routes | ||
{ | ||
router.Get("/", func(c fiber.Ctx) error { | ||
return c.Send([]byte("Use the REST API")) | ||
}) | ||
} | ||
|
||
return router.Listen(conf.Listen) | ||
} | ||
|
||
func SetupServer(conf *cfg.Config) *fiber.App { | ||
router := fiber.New(fiber.Config{ | ||
CaseSensitive: true, | ||
StrictRouting: true, | ||
Immutable: true, | ||
ServerHeader: "anydb serve", | ||
AppName: "anydb", | ||
}) | ||
|
||
router.Use(requestid.New()) | ||
|
||
router.Use(logger.New(logger.Config{ | ||
Format: "${pid} ${locals:requestid} ${status} - ${method} ${path}\n", | ||
})) | ||
|
||
router.Use(cors.New(cors.Config{ | ||
AllowMethods: []string{"GET", "PUT", "POST", "DELETE"}, | ||
ExposeHeaders: []string{"Content-Type", "Accept"}, | ||
})) | ||
|
||
router.Use(compress.New(compress.Config{ | ||
Level: compress.LevelBestSpeed, | ||
})) | ||
|
||
return router | ||
} | ||
|
||
/* | ||
Wrapper to respond with proper json status, message and code, | ||
shall be prepared and called by the handlers directly. | ||
*/ | ||
func JsonStatus(c fiber.Ctx, code int, msg string) error { | ||
success := true | ||
|
||
if code != fiber.StatusOK { | ||
success = false | ||
} | ||
|
||
return c.Status(code).JSON(Result{ | ||
Code: code, | ||
Message: msg, | ||
Success: success, | ||
}) | ||
} |