Compare commits

..

No commits in common. "1ef6d3efaf6ff4fb5e4465d5049e8d84a005e9a6" and "eeabb76fdb571f6ce693c7e993f854a5ed5fb6e7" have entirely different histories.

4 changed files with 19 additions and 33 deletions

View file

@ -1,12 +1,12 @@
package httpServer package httpServer
import ( import (
"embed"
"log" "log"
"net/http" "net/http"
"git.gnous.eu/gnouseu/plakken/internal/constant" "git.gnous.eu/gnouseu/plakken/internal/constant"
"git.gnous.eu/gnouseu/plakken/internal/web/plak" "git.gnous.eu/gnouseu/plakken/internal/web/plak"
"git.gnous.eu/gnouseu/plakken/internal/web/static"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
) )
@ -14,19 +14,6 @@ type ServerConfig struct {
HTTPServer *http.Server HTTPServer *http.Server
UrlLength uint8 UrlLength uint8
DB *redis.Client 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 // Configure HTTP router
@ -34,13 +21,11 @@ func (config ServerConfig) router() {
WebConfig := plak.WebConfig{ WebConfig := plak.WebConfig{
DB: config.DB, DB: config.DB,
UrlLength: config.UrlLength, UrlLength: config.UrlLength,
Templates: config.Templates,
} }
staticFiles := http.FS(config.Static)
http.HandleFunc("GET /{$}", config.home) http.HandleFunc("GET /{$}", static.Home)
http.Handle("GET /static/{file}", http.FileServer(staticFiles))
http.HandleFunc("GET /{key}/{settings...}", WebConfig.View) http.HandleFunc("GET /{key}/{settings...}", WebConfig.View)
http.HandleFunc("GET /static/{file}", static.ServeStatic)
http.HandleFunc("POST /{$}", WebConfig.Create) http.HandleFunc("POST /{$}", WebConfig.Create)
http.HandleFunc("DELETE /{key}", WebConfig.Delete) http.HandleFunc("DELETE /{key}", WebConfig.Delete)
} }

View file

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

View file

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