Merge pull request '🏗️ Embed static files in binary' (#22) from ada/embed into main

Reviewed-on: #22
This commit is contained in:
Ada 2024-02-18 17:53:13 +00:00
commit 1ef6d3efaf
4 changed files with 33 additions and 19 deletions

View file

@ -1,12 +1,12 @@
package httpServer
import (
"embed"
"log"
"net/http"
"git.gnous.eu/gnouseu/plakken/internal/constant"
"git.gnous.eu/gnouseu/plakken/internal/web/plak"
"git.gnous.eu/gnouseu/plakken/internal/web/static"
"github.com/redis/go-redis/v9"
)
@ -14,6 +14,19 @@ type ServerConfig struct {
HTTPServer *http.Server
UrlLength uint8
DB *redis.Client
Static embed.FS
Templates embed.FS
}
func (config ServerConfig) home(w http.ResponseWriter, r *http.Request) {
index, err := config.Static.ReadFile("static/index.html")
if err != nil {
log.Println(err)
}
_, err = w.Write(index)
if err != nil {
log.Println(err)
}
}
// Configure HTTP router
@ -21,11 +34,13 @@ func (config ServerConfig) router() {
WebConfig := plak.WebConfig{
DB: config.DB,
UrlLength: config.UrlLength,
Templates: config.Templates,
}
staticFiles := http.FS(config.Static)
http.HandleFunc("GET /{$}", static.Home)
http.HandleFunc("GET /{$}", config.home)
http.Handle("GET /static/{file}", http.FileServer(staticFiles))
http.HandleFunc("GET /{key}/{settings...}", WebConfig.View)
http.HandleFunc("GET /static/{file}", static.ServeStatic)
http.HandleFunc("POST /{$}", WebConfig.Create)
http.HandleFunc("DELETE /{key}", WebConfig.Delete)
}

View file

@ -2,6 +2,7 @@ package plak
import (
"context"
"embed"
"io"
"log"
"net/http"
@ -19,6 +20,7 @@ var ctx = context.Background()
type WebConfig struct {
DB *redis.Client
UrlLength uint8
Templates embed.FS
}
// Plak "Object" for plak
@ -76,8 +78,9 @@ func (config WebConfig) View(w http.ResponseWriter, r *http.Request) {
log.Println(err)
}
} else {
t, err := template.ParseFiles("templates/paste.html")
t, err := template.ParseFS(config.Templates, "templates/paste.html")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println(err)
}
err = t.Execute(w, plak)

View file

@ -1,15 +0,0 @@
package static
import (
"net/http"
)
// ServeStatic Serve static file from static
func ServeStatic(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/"+r.PathValue("file")) // TODO: vérifier si c'est safe
}
// Home Serve index.html
func Home(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/index.html")
}

11
main.go
View file

@ -1,11 +1,20 @@
package main
import (
"embed"
"git.gnous.eu/gnouseu/plakken/internal/config"
"git.gnous.eu/gnouseu/plakken/internal/database"
"git.gnous.eu/gnouseu/plakken/internal/httpServer"
)
var (
//go:embed templates
templates embed.FS
//go:embed static
static embed.FS
)
func main() {
initConfig := config.GetConfig()
dbConfig := database.InitDB(initConfig.RedisAddress, initConfig.RedisUser, initConfig.RedisPassword, initConfig.RedisDB)
@ -15,6 +24,8 @@ func main() {
HTTPServer: httpServer.Config(initConfig.ListenAddress),
UrlLength: initConfig.UrlLength,
DB: db,
Static: static,
Templates: templates,
}
serverConfig.Server()