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

View file

@ -6,15 +6,14 @@ LABEL author="Victor Lacasse-Beaudoin <vlbeaudoin@agecem.org>"
WORKDIR /go/src/app 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/ 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'" \
ADD templates/ templates/ ./cmd/agecemorg/
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'" .
# Alpine # Alpine

View file

@ -11,17 +11,17 @@ help: ## Show this help
.PHONY: build .PHONY: build
build: ## Build agecem-org executable build: ## Build agecem-org executable
CGO_ENABLED=0 go build -a -o agecem-org \ 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 .PHONY: install
install: ## Using `go install`, install the agecem-org executable at ~/go/bin/haul install: ## Using `go install`, install the agecem-org executable at ~/go/bin/haul
CGO_ENABLED=0 go install -a \ 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 .PHONY: run
run: ## Run agecem-org from source with `go run` run: ## Run agecem-org from source with `go run`
CGO_ENABLED=0 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 .PHONY: build-container
build-container: ## Build agecem-org container image build-container: ## Build agecem-org container image

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,4 +1,4 @@
package main package agecemorg
import ( import (
"context" "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. Le fichier request.go contient les types de requêtes pour les endpoints HTTP.
*/ */
package main package agecemorg
import ( import (
"bytes" "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. Le fichier response.go contient les types de réponses JSON et HTML pour les endpoints HTTP.
*/ */
package main package agecemorg
import ( import (
"codeberg.org/vlbeaudoin/voki/v3" "codeberg.org/vlbeaudoin/voki/v3"

View file

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

View file

@ -1,4 +1,4 @@
package version package agecemorg
// Filled by build flag // Filled by build flag
var version string 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: 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 { func Version() string {
if version == "" { 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