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 200 additions and 1 deletions

View file

@ -10,6 +10,8 @@ ADD public/ public/
ADD cmd/ cmd/ ADD cmd/ cmd/
ADD api/ api/
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org . RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org .
# Alpine # 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 ( import (
"context" "context"
"crypto/subtle" "crypto/subtle"
"encoding/json"
"fmt" "fmt"
"log" "log"
@ -20,6 +21,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"git.agecem.com/agecem/agecem-org/api"
"git.agecem.com/agecem/agecem-org/public" "git.agecem.com/agecem/agecem-org/public"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
@ -148,6 +150,10 @@ func RunServer() {
e.GET("/formulaires", handleFormulaires) e.GET("/formulaires", handleFormulaires)
// Public Routes
e.GET("/public/documentation/:bucket/:document", handlePublicDocumentation)
e.Logger.Fatal(e.Start( e.Logger.Fatal(e.Start(
fmt.Sprintf(":%d", viper.GetInt("server.port")))) fmt.Sprintf(":%d", viper.GetInt("server.port"))))
} }
@ -573,20 +579,98 @@ func handleActualiteArticle(c echo.Context) error {
article := c.Param("article") article := c.Param("article")
return c.String(http.StatusOK, fmt.Sprintf("Article: %s", article)) return c.String(http.StatusOK, fmt.Sprintf("Article: %s", article))
} }
func handleVieEtudiante(c echo.Context) error { func handleVieEtudiante(c echo.Context) error {
return c.Render(http.StatusOK, "vie-etudiante-html", nil) return c.Render(http.StatusOK, "vie-etudiante-html", nil)
} }
func handleVieEtudianteOrganisme(c echo.Context) error { func handleVieEtudianteOrganisme(c echo.Context) error {
organisme := c.Param("organisme") organisme := c.Param("organisme")
return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme)) return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme))
} }
func handleDocumentation(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 { func handleFormulaires(c echo.Context) error {
return c.Render(http.StatusOK, "formulaires-html", nil) 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 // CSS Handlers
func handleStaticCSSIndex(c echo.Context) error { func handleStaticCSSIndex(c echo.Context) error {

View file

@ -9,6 +9,18 @@
<body> <body>
{{ template "header-html" }} {{ template "header-html" }}
<h1>Documentation</h1> <h1>Documentation</h1>
<p>
{{ range . }}
{{ $bucket_name := .Name }}
<h2>{{ $bucket_name }}</h2>
<ul>
{{ range .Documents }}
<li> <a href="/public/documentation/{{ $bucket_name }}/{{ . }}">{{ . }}</a></li>
{{ end}}
</ul>
{{ end }}
</p>
</body> </body>
</html> </html>
{{ end }} {{ end }}