Implémenter client web de base

Déplacer tous les flags vers rootCmd.PersistentFlags()

Ajouter config struct types à models/

Ajouter data/apiclient.go#ApiClient.GetHealth()

Ajouter webCmd avec viper.Unmarshal() pour valeurs de config

Ajouter package web depuis agecem/bottin
This commit is contained in:
Victor Lacasse-Beaudoin 2023-06-09 01:09:02 -04:00
parent 7773b3cf72
commit c850b221a1
8 changed files with 431 additions and 56 deletions

10
web/embed.go Normal file
View file

@ -0,0 +1,10 @@
package web
import "embed"
//go:embed templates/*
var templatesFS embed.FS
func GetTemplates() embed.FS {
return templatesFS
}

117
web/templates/index.html Normal file
View file

@ -0,0 +1,117 @@
{{ define "index-html" }}
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>
AGECEM | Agenda
</title>
<style>
body {
/* Center the form on the page */
margin: 0 auto;
width: 400px;
/* Form outline */
padding: 1em;
border: 1px solid #ccc;
border-radius: 1em;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
form li + li {
margin-top: 1em;
}
label {
/* Uniform size & alignment */
display: inline-block;
width: 90px;
text-align: right;
}
input,
textarea {
/* To make sure that all text fields have the same font settings
By default, textareas have a monospace font */
font: 1em sans-serif;
/* Uniform text field size */
width: 300px;
box-sizing: border-box;
/* Match form field borders */
border: 1px solid #999;
}
input:focus,
textarea:focus {
/* Additional highlight for focused elements */
border-color: #000;
}
textarea {
/* Align multiline text fields with their labels */
vertical-align: top;
/* Provide space to type some text */
height: 5em;
}
.button {
/* Align buttons with the text fields */
padding-left: 90px; /* same size as the label elements */
}
button {
/* This extra margin represent roughly the same space as the space
between the labels and their text fields */
margin-left: 0.5em;
}
</style>
</head>
<body>
<h2>
Distribution d'agendas aux membres de l'AGECEM
</h2>
<p>
1) Si l'agenda demandé est perpétuel, cochez 'Perpétuel?:'<br>
<br>
2) Sélectionnez le champs 'Numéro étudiant:'<br>
<br>
3) Scannez la carte étudiante d'unE membre<br>
-ou-<br>
Entrez manuellement le code à 7 chiffres<br>
<br>
4) Si aucune erreur ne survient, la personne est libre de partir avec son agenda
</p>
<form action="/transaction" method="post">
<ul>
<li>
<label for="is_perpetual">Perpétuel?:</label>
<input type="checkbox" id="is_perpetual" name="is_perpetual"/>
</li>
<li>
<label for="membre_id">Numéro étudiant:</label>
<input type="text" id="membre_id" name="membre_id" autofocus/>
</li>
<li class="button">
<button type="submit">Scan</button>
</li>
</ul>
</form>
<p class="result">{{ .Result }}</p>
</body>
</html>
{{ end }}

View file

@ -0,0 +1,43 @@
package webhandlers
import (
"fmt"
"net/http"
"git.agecem.com/agecem/bottin/v5/data"
"github.com/labstack/echo/v4"
)
func GetIndex(c echo.Context) error {
return c.Render(http.StatusOK, "index-html", nil)
}
func GetMembre(c echo.Context) error {
apiClient := data.NewApiClientFromViper()
membreID := c.QueryParam("membre_id")
membre, err := apiClient.GetMembre(membreID)
if err != nil {
return c.Render(http.StatusBadRequest, "index-html", struct {
Result string
}{
Result: fmt.Sprintln("👎", err.Error()),
})
}
membreResult := fmt.Sprintf(`👍
Membre trouvéE: [%s]`, membre.ID)
if membre.PreferedName != "" {
membreResult = fmt.Sprintf("%s -> %s", membreResult, membre.PreferedName)
} else {
membreResult = fmt.Sprintf("%s -> %s, %s", membreResult, membre.LastName, membre.FirstName)
}
return c.Render(http.StatusOK, "index-html", struct {
Result string
}{
Result: membreResult,
})
}