diff --git a/.env.dev b/.env.dev new file mode 100644 index 0000000..60223fd --- /dev/null +++ b/.env.dev @@ -0,0 +1,3 @@ + +MODE=development +HOST=localhost:8000 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6c88c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ + +# Environment files +.env + + diff --git a/README.md b/README.md index e8d8c66..f8c73d0 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ # Nuovo Sito Poisson -Documentazione principale è raccolta in [un altro repository](https://github.com/phc-dm/documentazione/blob/master/progetti/server-poisson/README.md). +Repo del server del nuovo sito per il PHC. ## Development -Usare il comando `MODE=development go run main.go` per lanciare il server su `localhost:8000`. +Copiare il file `.env.dev` in `.env` per dire al server di lavorare in modalità di development e su quale indirizzo servire il sito. Per avviare il server basta fare -## Materiale - -```css -@import url('https://fonts.googleapis.com/css2?family=Share:wght@700&family=Ubuntu+Mono&family=Ubuntu:wght@300;700&display=swap'); ``` +go run . +``` + diff --git a/config.go b/config.go new file mode 100644 index 0000000..2ca82bf --- /dev/null +++ b/config.go @@ -0,0 +1,47 @@ +package main + +import ( + "log" + "os" + "strings" + + "github.com/joho/godotenv" +) + +type Config struct { + Mode string + Host string + + GitUrl string + ForumUrl string +} + +func stringOrDefault(value, defaultValue string) string { + if len(strings.TrimSpace(value)) == 0 { + return defaultValue + } + + return value +} + +func LoadConfig() *Config { + godotenv.Load() + + log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) + + Mode := stringOrDefault(os.Getenv("MODE"), "production") + log.Printf("MODE = %v", Mode) + + Host := stringOrDefault(os.Getenv("HOST"), "localhost:8080") + log.Printf("HOST = %v", Host) + + GitUrl := stringOrDefault(os.Getenv("GIT_URL"), "git.phc.dm.unipi.it") + log.Printf("GIT_URL = %v", GitUrl) + + ForumUrl := stringOrDefault(os.Getenv("FORUM_URL"), "forum.phc.dm.unipi.it") + log.Printf("FORUM_URL = %v", ForumUrl) + + return &Config{ + Mode, Host, GitUrl, ForumUrl, + } +} diff --git a/go.mod b/go.mod index 0b2c3bb..4510bb2 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,7 @@ go 1.13 require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-chi/chi/v5 v5.0.7 // indirect + github.com/joho/godotenv v1.4.0 // indirect github.com/labstack/echo/v4 v4.1.16 ) diff --git a/go.sum b/go.sum index 7042419..be96117 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= +github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/labstack/echo/v4 v4.1.16 h1:8swiwjE5Jkai3RPfZoahp8kjVCRNq+y7Q0hPji2Kz0o= github.com/labstack/echo/v4 v4.1.16/go.mod h1:awO+5TzAjvL8XpibdsfXxPgHr+orhtXZJZIQCVjogKI= github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= diff --git a/main.go b/main.go index 5ee7ea9..8d0966f 100644 --- a/main.go +++ b/main.go @@ -3,58 +3,55 @@ package main import ( "log" "net/http" - "os" - "strconv" "git.phc.dm.xxxxx.xx/server-poisson/utils" - "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" ) type object map[string]interface{} -func init() { - - log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) - - log.Printf("MODE = %v (default: production)", os.Getenv("MODE")) - log.Printf("PORT = %v (default: 8000)", os.Getenv("PORT")) - -} - func main() { - // Echo instance - e := echo.New() + config := LoadConfig() - // Middleware - e.Use(middleware.Logger()) - e.Use(middleware.Recover()) + r := chi.NewRouter() - // Templates & Renderer - e.Renderer = utils.NewTemplateRenderer("base.html") + // Middleware + r.Use(middleware.Logger) + r.Use(middleware.Recoverer) // Static assets - e.Static("/assets", "./assets") - e.Static("/blog", "./blog/public") + r.Handle("/assets/*", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets")))) + r.Handle("/blog/*", http.StripPrefix("/blog", http.FileServer(http.Dir("./blog/public")))) + + // Templates & Renderer + renderer := utils.NewTemplateRenderer("base.html") // Routes - e.GET("/", func(c echo.Context) error { - return c.Render(http.StatusOK, "home.html", object{}) - }) - e.GET("/utenti", func(c echo.Context) error { - return c.Render(http.StatusOK, "utenti.html", object{}) - }) - e.GET("/login", func(c echo.Context) error { - return c.Render(http.StatusOK, "login.html", object{}) + r.Get("/", func(w http.ResponseWriter, r *http.Request) { + err := renderer.Render(w, "home.html", object{"Config": config}) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } }) - port, ok := strconv.Atoi(os.Getenv("PORT")) - if ok != nil { - port = 8000 - } + r.Get("/utenti", func(w http.ResponseWriter, r *http.Request) { + err := renderer.Render(w, "utenti.html", object{"Config": config}) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + }) - log.Print(port) + r.Get("/login", func(w http.ResponseWriter, r *http.Request) { + err := renderer.Render(w, "login.html", object{"Config": config}) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + }) - // Start server - e.Logger.Fatal(e.Start(":" + strconv.Itoa(port))) + log.Printf(`Starting server...`) + http.ListenAndServe(config.Host, r) } diff --git a/utils/template_renderer.go b/utils/template_renderer.go index bf16f0e..f0d187c 100644 --- a/utils/template_renderer.go +++ b/utils/template_renderer.go @@ -4,8 +4,6 @@ import ( "io" "os" "text/template" - - "github.com/labstack/echo/v4" ) // TemplateRenderer holds cached templates for rendering @@ -23,18 +21,14 @@ func NewTemplateRenderer(baseFile string) *TemplateRenderer { } // Render the template -func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error { +func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}) error { tmpl := t.templateMap[name] + if os.Getenv("MODE") == "development" || tmpl == nil { tmpl = template.Must(t.baseTemplate.Clone()) tmpl.ParseFiles("./views/" + name) t.templateMap[name] = tmpl } - // // Add global methods if data is a map - // if viewContext, isMap := data.(map[string]interface{}); isMap { - // viewContext["reverse"] = c.Echo().Reverse - // } - return tmpl.ExecuteTemplate(w, "base", data) } diff --git a/views/base.html b/views/base.html index 163e8e6..d2ad353 100644 --- a/views/base.html +++ b/views/base.html @@ -26,10 +26,10 @@ Notizie