Ajouter exposition publique de documents

This commit is contained in:
Victor Lacasse-Beaudoin 2023-04-26 18:27:58 -04:00
parent 3dcb86d65f
commit 25f8669c29
2 changed files with 35 additions and 2 deletions

View file

@ -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 {

View file

@ -11,11 +11,12 @@
<h2>Documentation</h2>
<p>
{{ range . }}
<h3>{{ .Name }}</h3>
{{ $bucket_name := .Name }}
<h3>{{ $bucket_name }}</h3>
<ul>
{{ range .Documents }}
<li>{{ . }}</li>
<li> <a href="/public/documentation/{{ $bucket_name }}/{{ . }}">{{ . }}</a></li>
{{ end}}
</ul>
{{ end }}