Ajouter lecture de document par route html #48

Merged
vlbeaudoin merged 4 commits from feature/html-document-read into main 2023-04-26 18:36:11 -04:00
4 changed files with 164 additions and 2 deletions
Showing only changes of commit 3dcb86d65f - Show all commits

View file

@ -10,6 +10,8 @@ ADD public/ public/
ADD cmd/ cmd/
ADD api/ api/
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org .
# Alpine

101
api/api.go Normal file
View file

@ -0,0 +1,101 @@
package api
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
)
type API struct {
Protocol string
Host string
Port int
Opts APIOptions
}
type APIOptions struct {
KeyAuth bool
Key string
}
func New(protocol, host string, port int, opts APIOptions) (*API, error) {
api := API{
Protocol: protocol,
Host: host,
Port: port,
Opts: opts,
}
return &api, nil
}
// Call returns a []byte representing a response body.
// Can be used for GET or DELETE methods
func (a *API) Call(method, route string) ([]byte, error) {
endpoint := fmt.Sprintf("%s://%s:%d",
a.Protocol,
a.Host,
a.Port,
)
request := fmt.Sprintf("%s%s", endpoint, route)
switch method {
case http.MethodGet:
// Create client
client := &http.Client{}
// Create request
request, err := http.NewRequest(http.MethodGet, request, nil)
if err != nil {
return nil, err
}
if a.Opts.KeyAuth {
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Opts.Key))
}
// Fetch Request
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return body, nil
case http.MethodDelete:
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest(http.MethodDelete, request, nil)
if err != nil {
return nil, err
}
if a.Opts.KeyAuth {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Opts.Key))
}
// Fetch Request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Read Response Body
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respBody, nil
}
return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method))
}

View file

@ -6,6 +6,7 @@ package cmd
import (
"context"
"crypto/subtle"
"encoding/json"
"fmt"
"log"
@ -20,6 +21,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"git.agecem.com/agecem/agecem-org/api"
"git.agecem.com/agecem/agecem-org/public"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
@ -584,7 +586,53 @@ func handleVieEtudianteOrganisme(c echo.Context) error {
}
func handleDocumentation(c echo.Context) error {
return c.Render(http.StatusOK, "documentation-html", nil)
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.Render(http.StatusInternalServerError, "documentation-html", nil)
}
result, err := client.Call(http.MethodGet, "/v1/bucket")
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var buckets []string
err = json.Unmarshal(result, &buckets)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
type Bucket struct {
Name string
Documents []string
}
var data []Bucket
for _, bucket := range buckets {
result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
var documents []string
err = json.Unmarshal(result, &documents)
if err != nil {
return c.Render(http.StatusInternalServerError, "documentation-html", nil)
}
data = append(data, Bucket{
Name: bucket,
Documents: documents,
})
}
return c.Render(http.StatusOK, "documentation-html", data)
}
func handleFormulaires(c echo.Context) error {

View file

@ -8,7 +8,18 @@
</head>
<body>
{{ template "header-html" }}
<h1>Documentation</h1>
<h2>Documentation</h2>
<p>
{{ range . }}
<h3>{{ .Name }}</h3>
<ul>
{{ range .Documents }}
<li>{{ . }}</li>
{{ end}}
</ul>
{{ end }}
</p>
</body>
</html>
{{ end }}