From 51d5dde7b4e7ead147cf08f6e6a5a7cce26a4cb3 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 26 Apr 2023 15:15:43 -0400 Subject: [PATCH 1/4] Fix formattage de handlers html --- cmd/server.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/server.go b/cmd/server.go index db3bb32..8aaee8d 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -573,16 +573,20 @@ func handleActualiteArticle(c echo.Context) error { article := c.Param("article") return c.String(http.StatusOK, fmt.Sprintf("Article: %s", article)) } + func handleVieEtudiante(c echo.Context) error { return c.Render(http.StatusOK, "vie-etudiante-html", nil) } + func handleVieEtudianteOrganisme(c echo.Context) error { organisme := c.Param("organisme") return c.String(http.StatusOK, fmt.Sprintf("Organisme: %s", organisme)) } + func handleDocumentation(c echo.Context) error { return c.Render(http.StatusOK, "documentation-html", nil) } + func handleFormulaires(c echo.Context) error { return c.Render(http.StatusOK, "formulaires-html", nil) } -- 2.45.2 From 3dcb86d65fdc547a1c7af19bbebfa4d577886023 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 26 Apr 2023 16:51:13 -0400 Subject: [PATCH 2/4] =?UTF-8?q?Ajouter=20liste=20de=20documents=20=C3=A0?= =?UTF-8?q?=20/documentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 + api/api.go | 101 +++++++++++++++++++++++++++++++ cmd/server.go | 50 ++++++++++++++- public/html/documentation.gohtml | 13 +++- 4 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 api/api.go diff --git a/Dockerfile b/Dockerfile index 0e98d5c..5281e65 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/api/api.go b/api/api.go new file mode 100644 index 0000000..70334f9 --- /dev/null +++ b/api/api.go @@ -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)) +} diff --git a/cmd/server.go b/cmd/server.go index 8aaee8d..71d1817 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -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 { diff --git a/public/html/documentation.gohtml b/public/html/documentation.gohtml index 813d77e..50f8a00 100644 --- a/public/html/documentation.gohtml +++ b/public/html/documentation.gohtml @@ -8,7 +8,18 @@ {{ template "header-html" }} -

Documentation

+

Documentation

+

+ {{ range . }} +

{{ .Name }}

+ + + {{ end }} +

{{ end }} -- 2.45.2 From 25f8669c291a219356368a933be6eef856fb60e8 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 26 Apr 2023 18:27:58 -0400 Subject: [PATCH 3/4] Ajouter exposition publique de documents --- cmd/server.go | 32 ++++++++++++++++++++++++++++++++ public/html/documentation.gohtml | 5 +++-- 2 files changed, 35 insertions(+), 2 deletions(-) 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 @@

Documentation

{{ range . }} -

{{ .Name }}

+ {{ $bucket_name := .Name }} +

{{ $bucket_name }}

    {{ range .Documents }} -
  • {{ . }}
  • +
  • {{ . }}
  • {{ end}}
{{ end }} -- 2.45.2 From de33317a8e73ae49dd6db1fcb1fb61bdf9c6357d Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 26 Apr 2023 18:35:17 -0400 Subject: [PATCH 4/4] Unifier templates html --- public/html/documentation.gohtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/html/documentation.gohtml b/public/html/documentation.gohtml index e627bfc..27f2a06 100644 --- a/public/html/documentation.gohtml +++ b/public/html/documentation.gohtml @@ -8,11 +8,11 @@ {{ template "header-html" }} -

Documentation

+

Documentation

{{ range . }} {{ $bucket_name := .Name }} -

{{ $bucket_name }}

+

{{ $bucket_name }}

    {{ range .Documents }} -- 2.45.2