Vari fix e riorganizzazione del codice
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
// "github.com/phc-dm/auth-poisson/user"
|
|
||||||
|
|
||||||
// AuthService rappresenta un servizio di autenticazione
|
// AuthService rappresenta un servizio di autenticazione
|
||||||
// type AuthService interface {
|
// type AuthService interface {
|
||||||
// GetUsers() []User
|
// GetUsers() []User
|
||||||
|
|||||||
+11
-1
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
"github.com/phc-dm/server-poisson/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Mode string
|
var Mode string
|
||||||
@@ -22,7 +23,7 @@ func stringOrDefault(value, defaultValue string) string {
|
|||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig() {
|
func Load() {
|
||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
|
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||||
@@ -39,3 +40,12 @@ func LoadConfig() {
|
|||||||
ForumUrl = stringOrDefault(os.Getenv("FORUM_URL"), "forum.phc.dm.unipi.it")
|
ForumUrl = stringOrDefault(os.Getenv("FORUM_URL"), "forum.phc.dm.unipi.it")
|
||||||
log.Printf("FORUM_URL = %v", ForumUrl)
|
log.Printf("FORUM_URL = %v", ForumUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Object() util.H {
|
||||||
|
return util.H{
|
||||||
|
"Mode": Mode,
|
||||||
|
"Host": Host,
|
||||||
|
"GitUrl": GitUrl,
|
||||||
|
"ForumUrl": ForumUrl,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,13 +6,12 @@ import (
|
|||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
"github.com/phc-dm/server-poisson/utils"
|
"github.com/phc-dm/server-poisson/config"
|
||||||
|
"github.com/phc-dm/server-poisson/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type object map[string]interface{}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config.LoadConfig()
|
config.Load()
|
||||||
|
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
@@ -25,11 +24,11 @@ func main() {
|
|||||||
r.Handle("/blog/*", http.StripPrefix("/blog", http.FileServer(http.Dir("./blog/public"))))
|
r.Handle("/blog/*", http.StripPrefix("/blog", http.FileServer(http.Dir("./blog/public"))))
|
||||||
|
|
||||||
// Templates & Renderer
|
// Templates & Renderer
|
||||||
renderer := utils.NewTemplateRenderer("base.html")
|
renderer := NewTemplateRenderer("base.html")
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := renderer.Render(w, "home.html", object{})
|
err := renderer.Render(w, "home.html", util.H{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -37,7 +36,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
r.Get("/utenti", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/utenti", func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := renderer.Render(w, "utenti.html", object{})
|
err := renderer.Render(w, "utenti.html", util.H{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -45,13 +44,13 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
r.Get("/login", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := renderer.Render(w, "login.html", object{})
|
err := renderer.Render(w, "login.html", util.H{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Printf(`Starting server...`)
|
log.Printf(`Starting server on %v...`, config.Host)
|
||||||
http.ListenAndServe(Config.Host, r)
|
http.ListenAndServe(config.Host, r)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package utils
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/phc-dm/server-poisson/config"
|
"github.com/phc-dm/server-poisson/config"
|
||||||
|
"github.com/phc-dm/server-poisson/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TemplateRenderer holds cached templates for rendering
|
// TemplateRenderer holds cached templates for rendering
|
||||||
@@ -24,12 +25,10 @@ func NewTemplateRenderer(baseFile string) *TemplateRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render the template
|
// Render the template
|
||||||
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}) error {
|
func (t *TemplateRenderer) Render(w io.Writer, name string, data util.H) error {
|
||||||
tmpl := t.templateMap[name]
|
tmpl := t.templateMap[name]
|
||||||
|
|
||||||
if config.Mode == "development" || tmpl == nil {
|
if config.Mode == "development" || tmpl == nil {
|
||||||
tmpl = template.Must(t.baseTemplate.Clone())
|
|
||||||
|
|
||||||
if config.Mode == "development" {
|
if config.Mode == "development" {
|
||||||
tmpl = template.Must(template.ParseFiles("./views/" + t.baseFile))
|
tmpl = template.Must(template.ParseFiles("./views/" + t.baseFile))
|
||||||
} else {
|
} else {
|
||||||
@@ -40,5 +39,9 @@ func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}) er
|
|||||||
t.templateMap[name] = tmpl
|
t.templateMap[name] = tmpl
|
||||||
}
|
}
|
||||||
|
|
||||||
return tmpl.ExecuteTemplate(w, "base", data)
|
newData := util.H{}
|
||||||
|
newData.Apply(data)
|
||||||
|
newData["Config"] = config.Object()
|
||||||
|
|
||||||
|
return tmpl.ExecuteTemplate(w, "base", newData)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
type H map[string]interface{}
|
||||||
|
|
||||||
|
func (target H) Apply(sources ...H) H {
|
||||||
|
for _, source := range sources {
|
||||||
|
for k, v := range source {
|
||||||
|
target[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return target
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user