Refactor: Rimpiazzato il framework echo con go-chi
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
|
||||
# Environment files
|
||||
.env
|
||||
|
||||
|
||||
@@ -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 .
|
||||
```
|
||||
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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()
|
||||
|
||||
r := chi.NewRouter()
|
||||
|
||||
// Middleware
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
|
||||
// Templates & Renderer
|
||||
e.Renderer = utils.NewTemplateRenderer("base.html")
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
+2
-2
@@ -26,10 +26,10 @@
|
||||
<a href="/blog">Notizie</a>
|
||||
</div>
|
||||
<div class="nav-item">
|
||||
<a href="https://git.phc.dm.unipi.it">Git</a>
|
||||
<a href="https://{{ .Config.GitUrl }}">Git</a>
|
||||
</div>
|
||||
<div class="nav-item">
|
||||
<a href="https://forum.phc.dm.unipi.it">Forum</a>
|
||||
<a href="https://{{ .Config.ForumUrl }}">Forum</a>
|
||||
</div>
|
||||
<!-- Spacing -->
|
||||
<div class="hfill"></div>
|
||||
|
||||
Reference in New Issue
Block a user