refactor: déplacer fichiers go source vers pkg/ et cmd/ #217

Merged
vlbeaudoin merged 1 commit from vlbeaudoin/refactor/standard-pkg-cmd into main 2024-09-27 20:28:22 -04:00 AGit
107 changed files with 68 additions and 107 deletions
Showing only changes of commit 7d4a747774 - Show all commits

View file

@ -6,15 +6,14 @@ LABEL author="Victor Lacasse-Beaudoin <vlbeaudoin@agecem.org>"
WORKDIR /go/src/app
COPY go.mod go.sum main.go client.go cmd.go config.go entity.go handler.go media.go request.go response.go routes.go ./
COPY go.mod go.sum LICENSE ./
ADD cmd/ cmd/
ADD pkg/ pkg/
ADD ui/ ui/
ADD public/ public/
ADD templates/ templates/
ADD version/ version/
RUN CGO_ENABLED=0 go build -a -o agecem-org -ldflags="-X 'git.agecem.com/agecem/agecem-org/version.version=$agecem_org_version'" .
RUN CGO_ENABLED=0 go build -a -o agecem-org \
-ldflags="-X 'git.agecem.com/agecem/agecem-org/pkg/agecemorg/version.version=$agecem_org_version'" \
./cmd/agecemorg/
# Alpine

View file

@ -11,17 +11,17 @@ help: ## Show this help
.PHONY: build
build: ## Build agecem-org executable
CGO_ENABLED=0 go build -a -o agecem-org \
-ldflags="-X 'git.agecem.com/agecem/agecem-org/version.version=`git describe`'" .
-ldflags="-X 'git.agecem.com/agecem/agecem-org/pkg/agecemorg/version.version=`git describe`'" ./cmd/agecemorg/
.PHONY: install
install: ## Using `go install`, install the agecem-org executable at ~/go/bin/haul
CGO_ENABLED=0 go install -a \
-ldflags="-X 'git.agecem.com/agecem/agecem-org/version.version=`git describe`'" .
-ldflags="-X 'git.agecem.com/agecem/agecem-org/pkg/agecemorg/version.version=`git describe`'" ./cmd/agecemorg/
.PHONY: run
run: ## Run agecem-org from source with `go run`
CGO_ENABLED=0 go run \
-ldflags="-X 'git.agecem.com/agecem/agecem-org/version.version=`git describe`'" .
-ldflags="-X 'git.agecem.com/agecem/agecem-org/pkg/agecemorg/version.version=`git describe`'" ./cmd/agecemorg/
.PHONY: build-container
build-container: ## Build agecem-org container image

View file

@ -4,20 +4,14 @@ Copyright © 2023-2024 AGECEM
package main
import (
"embed"
"encoding/json"
"fmt"
"io"
"log"
"os"
"strings"
"text/template"
"codeberg.org/vlbeaudoin/serpents"
"git.agecem.com/agecem/agecem-org/public"
"git.agecem.com/agecem/agecem-org/templates"
"git.agecem.com/agecem/agecem-org/version"
"github.com/labstack/echo/v4"
"git.agecem.com/agecem/agecem-org/v3/pkg/agecemorg"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -27,7 +21,7 @@ var configCmd = &cobra.Command{
Use: "config",
Short: "Print the config to stdout in indented JSON format",
Run: func(cmd *cobra.Command, args []string) {
var cfg Config
var cfg agecemorg.Config
if err := viper.Unmarshal(&cfg); err != nil {
log.Fatal(err)
@ -41,7 +35,7 @@ func init() {
rootCmd.AddCommand(configCmd)
}
func printConfig(config Config) error {
func printConfig(config agecemorg.Config) error {
buf, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
@ -102,27 +96,17 @@ func initConfig() {
}
}
type Template struct {
templates *template.Template
}
var cfg Config
var (
publicFS embed.FS
templatesFS embed.FS
)
// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
Short: "Démarrer le serveur web",
Run: func(cmd *cobra.Command, args []string) {
var cfg agecemorg.Config
if err := viper.Unmarshal(&cfg); err != nil {
log.Fatal(err)
}
mediaClient, err := NewMediaClientFromViper()
mediaClient, err := agecemorg.NewMediaClientFromViper()
switch err != nil {
case true:
log.Printf("NewMediaClientFromViper error: %s", err)
@ -135,14 +119,12 @@ var serverCmd = &cobra.Command{
}
}
RunServer()
agecemorg.RunServer(cfg)
},
}
func init() {
rootCmd.AddCommand(serverCmd)
publicFS = public.GetPublicFS()
templatesFS = templates.GetTemplatesFS()
serpents.Int(serverCmd.Flags(),
"server.port", "server-port", 8080,
@ -212,16 +194,12 @@ func init() {
"Password for basic authentication for /admin routes")
}
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print version registered at build time",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("agecem-org", version.Version())
fmt.Println("agecem-org", agecemorg.Version())
},
}

2
go.mod
View file

@ -1,4 +1,4 @@
module git.agecem.com/agecem/agecem-org
module git.agecem.com/agecem/agecem-org/v3
go 1.23.0

View file

@ -1,4 +1,4 @@
package main
package agecemorg
import (
"bytes"

View file

@ -1,4 +1,4 @@
package main
package agecemorg
/*
Permet de contenir la configuration obtenue par cobra/viper

View file

@ -1,4 +1,4 @@
package main
package agecemorg
type Bucket struct {
Name string

View file

@ -1,4 +1,4 @@
package main
package agecemorg
import (
"context"
@ -10,7 +10,6 @@ import (
"codeberg.org/vlbeaudoin/pave/v2"
"codeberg.org/vlbeaudoin/voki/v3"
"git.agecem.com/agecem/agecem-org/version"
"github.com/labstack/echo/v4"
"github.com/minio/minio-go/v7"
)
@ -550,7 +549,7 @@ func (h *V1Handler) ReadSpec(c echo.Context) error {
return c.JSON(response.StatusCode(), response)
}
response.Data.Spec = fmt.Sprintf("# pave spec for agecem-org %s\n", version.Version())
response.Data.Spec = fmt.Sprintf("# pave spec for agecem-org %s\n", Version())
for _, route := range h.Pave.SortedRouteStrings() {
response.Data.Spec = fmt.Sprintf("%s%s", response.Data.Spec, route)

View file

@ -1,4 +1,4 @@
package main
package agecemorg
import (
"context"

View file

@ -3,7 +3,7 @@ File request.go contains the JSON request types for HTTP endpoints.
Le fichier request.go contient les types de requêtes pour les endpoints HTTP.
*/
package main
package agecemorg
import (
"bytes"

View file

@ -3,7 +3,7 @@ File response.go contains the JSON and HTML response types for HTTP endpoints.
Le fichier response.go contient les types de réponses JSON et HTML pour les endpoints HTTP.
*/
package main
package agecemorg
import (
"codeberg.org/vlbeaudoin/voki/v3"

View file

@ -1,32 +1,29 @@
package main
package agecemorg
import (
"crypto/subtle"
"fmt"
"log"
"net/http"
"text/template"
"codeberg.org/vlbeaudoin/pave/v2"
"git.agecem.com/agecem/agecem-org/v3/ui"
"git.agecem.com/agecem/agecem-org/v3/ui/public"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func RunServer() {
func RunServer(cfg Config) {
e := echo.New()
t := &Template{
templates: template.Must(template.ParseFS(templatesFS, "html/*.html")),
}
e.Renderer = t
e.Renderer = ui.NewRenderer()
e.Pre(middleware.RemoveTrailingSlash())
groupStatic := e.Group("/public/*")
groupStatic.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "/",
Filesystem: http.FS(publicFS),
Filesystem: http.FS(public.GetPublicFS()),
//TODO
//Browse: true,
}))

View file

@ -1,4 +1,4 @@
package version
package agecemorg
// Filled by build flag
var version string
@ -8,7 +8,7 @@ Version returns the application version
Example command to use the output of `git describe` for version number at build time:
$ go build -ldflags="-X 'git.agecem.com/agecem/agecem-org/version.version=`git describe`'" .
$ go build -ldflags="-X 'git.agecem.com/agecem/agecem-org/pkg/agecemorg/version.version=`git describe`'" .
*/
func Version() string {
if version == "" {

View file

@ -1,14 +0,0 @@
{{ define "a-propos-html" }}
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>AGECEM | À propos</title>
{{ template "general-html" }}
</head>
<body>
{{ template "header-html" }}
<h1 class="heading1">À propos</h1>
</body>
</html>
{{ end }}

View file

@ -1,14 +0,0 @@
{{ define "actualite-html" }}
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>AGECEM | Actualité</title>
{{ template "general-html" }}
</head>
<body>
{{ template "header-html" }}
<h1 class="heading1">Actualité</h1>
</body>
</html>
{{ end }}

View file

@ -1,18 +0,0 @@
/*
Package templates contient les fichiers html à templater par l'application.
Le contenu sera embedded dans le fichier binaire, dans le but de bundle les
dépendances avec l'application, simplifiant son déploiement.
Une copie du contenu peut être obtenue par un appel de GetTemplatesFS().
*/
package templates
import "embed"
//go:embed html/*.html
var templatesFS embed.FS
func GetTemplatesFS() embed.FS {
return templatesFS
}

View file

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View file

Before

Width:  |  Height:  |  Size: 818 B

After

Width:  |  Height:  |  Size: 818 B

View file

Before

Width:  |  Height:  |  Size: 965 B

After

Width:  |  Height:  |  Size: 965 B

View file

Before

Width:  |  Height:  |  Size: 396 B

After

Width:  |  Height:  |  Size: 396 B

View file

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

Some files were not shown because too many files have changed in this diff Show more