Embed fichiers statiques dans exécutable avec go embed #6
7 changed files with 48 additions and 20 deletions
10
Dockerfile
10
Dockerfile
|
@ -1,4 +1,4 @@
|
|||
FROM golang:1.20.2 as build
|
||||
FROM golang:1.21.4 as build
|
||||
|
||||
LABEL author="Victor Lacasse-Beaudoin <vlbeaudoin@agecem.org>"
|
||||
LABEL license="MIT"
|
||||
|
@ -15,11 +15,13 @@ ADD data/ data/
|
|||
|
||||
ADD handlers/ handlers/
|
||||
|
||||
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o babillard .
|
||||
ADD public/ public/
|
||||
|
||||
RUN CGO_ENABLED=0 go build -a -o babillard .
|
||||
|
||||
# Alpine
|
||||
|
||||
FROM alpine:3.17.2
|
||||
FROM alpine:3.18.4
|
||||
|
||||
RUN apk update && apk upgrade --no-cache
|
||||
|
||||
|
@ -27,8 +29,6 @@ WORKDIR /app
|
|||
|
||||
ADD contenu/ contenu/
|
||||
|
||||
ADD static/ static/
|
||||
|
||||
COPY --from=build /go/src/app/babillard /usr/bin/babillard
|
||||
|
||||
CMD ["babillard", "server"]
|
||||
|
|
|
@ -3,8 +3,10 @@ package cmd
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.agecem.com/agecem/babillard/handlers"
|
||||
"git.agecem.com/agecem/babillard/public"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -17,9 +19,9 @@ var serverCmd = &cobra.Command{
|
|||
Short: "Démarrer le serveur web",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
port := viper.GetInt("server.port")
|
||||
static_dir := viper.GetString("server.static_dir")
|
||||
//static_dir := viper.GetString("server.static_dir")
|
||||
|
||||
runServer(port, static_dir)
|
||||
runServer(port)
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -34,25 +36,33 @@ func declareFlags() {
|
|||
|
||||
serverCmd.Flags().String("contenu_dir", "contenu", "Répertoire du contenu à exposer (config: \"server.contenu_dir\")")
|
||||
viper.BindPFlag("server.contenu_dir", serverCmd.Flags().Lookup("contenu_dir"))
|
||||
|
||||
serverCmd.Flags().String("static_dir", "static", "Répertoire des fichiers statiques à exposer (config: \"server.static_dir\")")
|
||||
viper.BindPFlag("server.static_dir", serverCmd.Flags().Lookup("static_dir"))
|
||||
}
|
||||
|
||||
func runServer(port int, static_dir string) {
|
||||
func runServer(port int) {
|
||||
log.Print("[I] Starting webserver")
|
||||
|
||||
e := echo.New()
|
||||
|
||||
g := e.Group("")
|
||||
e.Pre(middleware.AddTrailingSlash())
|
||||
|
||||
e.Pre(middleware.RemoveTrailingSlash())
|
||||
e.Group("").Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||||
Root: "/html/",
|
||||
Filesystem: http.FS(public.HTMLFS()),
|
||||
}))
|
||||
e.Group("/public/css").Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||||
Root: "/css/",
|
||||
Filesystem: http.FS(public.CSSFS()),
|
||||
}))
|
||||
e.Group("/public/js").Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||||
Root: "/js/",
|
||||
Filesystem: http.FS(public.JSFS()),
|
||||
}))
|
||||
|
||||
g.Static("/", static_dir)
|
||||
groupAPI := e.Group("/api")
|
||||
|
||||
g.GET("/api", handlers.HandleAPIShow)
|
||||
g.GET("/api/contenu", handlers.HandleAPIContenuList)
|
||||
g.GET("/api/contenu/:filename", handlers.HandleAPIContenuFile)
|
||||
groupAPI.GET("/", handlers.HandleAPIShow)
|
||||
groupAPI.GET("/contenu/", handlers.HandleAPIContenuList)
|
||||
groupAPI.GET("/contenu/:filename/", handlers.HandleAPIContenuFile)
|
||||
|
||||
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", port)))
|
||||
}
|
||||
|
|
18
public/embed.go
Normal file
18
public/embed.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package public
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed html/*
|
||||
var htmlFS embed.FS
|
||||
|
||||
func HTMLFS() embed.FS { return htmlFS }
|
||||
|
||||
//go:embed css/*
|
||||
var cssFS embed.FS
|
||||
|
||||
func CSSFS() embed.FS { return cssFS }
|
||||
|
||||
//go:embed js/*
|
||||
var jsFS embed.FS
|
||||
|
||||
func JSFS() embed.FS { return jsFS }
|
|
@ -5,13 +5,13 @@
|
|||
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
|
||||
<script src="slider.js"></script>
|
||||
<script src="/public/js/slider.js"></script>
|
||||
|
||||
<!-- BROKEN, brise slider.js quand inclu
|
||||
<script src="text.js"></script>
|
||||
!-->
|
||||
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<link rel="stylesheet" href="/public/css/style.css">
|
||||
</head>
|
||||
|
||||
<body>
|
|
@ -6,7 +6,7 @@ function afficherIndex() {
|
|||
}
|
||||
|
||||
function afficherImage() {
|
||||
$('#image').attr('src', "api/contenu/"+images[indexImages]);
|
||||
$('#image').attr('src', "/api/contenu/"+images[indexImages]);
|
||||
}
|
||||
|
||||
function augmenterIndex() {
|
Loading…
Reference in a new issue