WIP: ♻️ Restructure test #36
4 changed files with 33 additions and 19 deletions
|
@ -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,6 +14,19 @@ 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
|
||||||
|
@ -21,11 +34,13 @@ 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 /{$}", 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 /{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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package plak
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"embed"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -19,6 +20,7 @@ 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
|
||||||
|
@ -76,8 +78,9 @@ func (config WebConfig) View(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t, err := template.ParseFiles("templates/paste.html")
|
t, err := template.ParseFS(config.Templates, "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)
|
||||||
|
|
|
@ -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
11
main.go
|
@ -1,11 +1,20 @@
|
||||||
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)
|
||||||
|
@ -15,6 +24,8 @@ 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()
|
||||||
|
|
Loading…
Reference in a new issue