diff --git a/cmd/server.go b/cmd/server.go index 71d1817..71f5a54 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -150,6 +150,10 @@ func RunServer() { e.GET("/formulaires", handleFormulaires) + // Public Routes + + e.GET("/public/documentation/:bucket/:document", handlePublicDocumentation) + e.Logger.Fatal(e.Start( fmt.Sprintf(":%d", viper.GetInt("server.port")))) } @@ -639,6 +643,34 @@ func handleFormulaires(c echo.Context) error { return c.Render(http.StatusOK, "formulaires-html", nil) } +func handlePublicDocumentation(c echo.Context) error { + client, err := api.New("http", "localhost", viper.GetInt("server.port"), api.APIOptions{ + KeyAuth: viper.GetBool("server.api.auth"), + Key: viper.GetString("server.api.key"), + }) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + } + + bucket := c.Param("bucket") + document := c.Param("document") + + result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document)) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + } + + // Check if result can fit inside a map containing a message + var result_map map[string]string + + err = json.Unmarshal(result, &result_map) + if err == nil { + return c.JSON(http.StatusBadRequest, result_map) + } + + return c.Blob(http.StatusOK, "application/octet-stream", result) +} + // CSS Handlers func handleStaticCSSIndex(c echo.Context) error { diff --git a/public/html/documentation.gohtml b/public/html/documentation.gohtml index 50f8a00..e627bfc 100644 --- a/public/html/documentation.gohtml +++ b/public/html/documentation.gohtml @@ -11,11 +11,12 @@
{{ range . }} -