From 9f8538863768258b015edb60491a6e4b13eb5a8d Mon Sep 17 00:00:00 2001 From: Ali Helmy Date: Sun, 7 Jan 2024 15:12:52 +0200 Subject: [PATCH] Add url to server ui --- README.md | 7 +-- server/server.go | 33 +++++++++++++ server/server_test.go | 37 +++++++++++++++ server/ui/index.html | 11 +++++ server/ui/partials/navbar.html | 3 ++ server/ui/url.html | 84 ++++++++++++++++++++++++++++++++++ 6 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 server/ui/url.html diff --git a/README.md b/README.md index ff3ac16..5436bb2 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ Usage: xdev [command] Available Commands: - base64decoder Decode base64 string. Alias: b64d +base64decoder Decode base64 string. Alias: b64d base64encoder Encode string to base64. Alias: b64e completion Generate the autocompletion script for the specified shell help Help about any command @@ -109,10 +109,11 @@ Available Commands: json2yaml Convert JSON to YAML. Alias: j2y jwt Decode or encode a JWT string. password Password generator. Alias: pwd - uuid Generate a uuid string + server Start a web server, Alias: s ulid Generate a ulid string + url URL encode/decode. + uuid Generate a uuid string yaml2json Convert YAML to JSON. Alias: y2j - server Start webserver default port 8000. Alias: s Flags: -h, --help help for xdev diff --git a/server/server.go b/server/server.go index 2d906af..a80b8f5 100644 --- a/server/server.go +++ b/server/server.go @@ -25,6 +25,7 @@ const ( ULIDPath = "/ulid" PasswordPath = "/password" Base64Path = "/base64" + URLPath = "/url" ) //go:embed ui/* @@ -62,6 +63,7 @@ func StartServer(port int32, isVerbose bool) { yamlPage(app) jwtPage(app) base64Page(app) + urlPage(app) log.Fatal(app.Listen(":"+strconv.FormatInt(int64(port), 10), fiber.ListenConfig{EnablePrefork: true})) } @@ -246,6 +248,37 @@ func base64Page(app *fiber.App) { }) } +func urlPage(app *fiber.App) { + app.Get(URLPath, func(c fiber.Ctx) error { + action := c.FormValue("action") + + decoded := "" + encoded := "" + errorStr := "" + + if action == "encode" { + encoded = internal.EncodeURL(c.FormValue("decoded")) + decoded = c.FormValue("decoded") + } else if action == "decode" { + _decoded, err := internal.DecodeURL(c.FormValue("encoded")) + if err != nil { + errorStr = err.Error() + } else { + decoded = _decoded + } + encoded = c.FormValue("encoded") + } + + // Render index within layouts/main + return c.Render(Prefix+"url", fiber.Map{ + "Title": "URL encode/decode", + "Decoded": decoded, + "Encoded": encoded, + "Error": errorStr, + }, MainLayout) + }) +} + func defineResources(app *fiber.App) { app.Use("/css", filesystem.New(filesystem.Config{ Root: fs.FS(cssFS), diff --git a/server/server_test.go b/server/server_test.go index 40fe3c7..6f2c002 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -290,3 +290,40 @@ func TestDefineResources(t *testing.T) { // TODO: Add assertions for the response body or other expectations }) } + +func TestURLPage(t *testing.T) { + app := newApp() + urlPage(app) + + t.Run("Test URL Page - Encode", func(t *testing.T) { + // Create a test request to the "/url" route with action=encode and decoded=... + req := httptest.NewRequest(http.MethodGet, "/url?action=encode&decoded=...", nil) + resp, err := app.Test(req) + if err != nil { + t.Fatalf("Failed to send test request: %v", err) + } + + // Check the response status code + if resp.StatusCode != http.StatusOK { + t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode) + } + + // TODO: Add assertions for the response body or other expectations + }) + + t.Run("Test URL Page - Decode", func(t *testing.T) { + // Create a test request to the "/url" route with action=decode and encoded=... + req := httptest.NewRequest(http.MethodGet, "/url?action=decode&encoded=...", nil) + resp, err := app.Test(req) + if err != nil { + t.Fatalf("Failed to send test request: %v", err) + } + + // Check the response status code + if resp.StatusCode != http.StatusOK { + t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode) + } + + // TODO: Add assertions for the response body or other expectations + }) +} \ No newline at end of file diff --git a/server/ui/index.html b/server/ui/index.html index a18b466..8958254 100644 --- a/server/ui/index.html +++ b/server/ui/index.html @@ -62,6 +62,17 @@
Base64
+
+ +
+
+
URL
+

+ Encode or decode a URL string.

+
+
+
+
diff --git a/server/ui/partials/navbar.html b/server/ui/partials/navbar.html index bf879e5..8908ed5 100644 --- a/server/ui/partials/navbar.html +++ b/server/ui/partials/navbar.html @@ -20,6 +20,9 @@ + diff --git a/server/ui/url.html b/server/ui/url.html new file mode 100644 index 0000000..8d3915d --- /dev/null +++ b/server/ui/url.html @@ -0,0 +1,84 @@ +{{template "ui/partials/navbar" .}} + +
+
+ {{.Error}} +
+
+
+
+ Encoded URL +
+
+ +
+
+
+
+ +
+
+
+
 
+
+
+
+
+
+ Decoded URL string +
+
+ +
+
+
+
+ +
+
+ +
+
+ + + \ No newline at end of file