diff --git a/assets/style.css b/assets/style.css index 2282db8..cc8227f 100644 --- a/assets/style.css +++ b/assets/style.css @@ -5,13 +5,16 @@ box-sizing: border-box; } +html { + position: relative; +} + html, body { margin: 0; min-height: 100vh; display: flex; flex-direction: column; - position: relative; font-family: 'Sen'; } @@ -22,7 +25,8 @@ h1, h2, h3, h4 { } body { - max-width: 50rem; + max-width: 100%; + width: 50rem; margin: 0 auto; padding: 0 0.5em; } @@ -63,6 +67,7 @@ body > div:last-of-type { footer { position: absolute; bottom: 0; + left: 0; width: 100%; height: 3em; display: flex; diff --git a/auth.go b/auth.go new file mode 100644 index 0000000..b612989 --- /dev/null +++ b/auth.go @@ -0,0 +1,53 @@ +package main + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "log" + "net/http" +) + +// AuthenticationService è l'interfaccia del servizio di autenticazione sul server principale +type AuthenticationService struct { + URL string +} + +// NewAuthenticationService crea un nuovo servizio di autenticazione e controlla se è attivo +func NewAuthenticationService(url string) (*AuthenticationService, error) { + service := new(AuthenticationService) + service.URL = url + + res, err := service.Get("status") + + if err != nil { + return nil, err + } + + status, _ := ioutil.ReadAll(res.Body) + + if string(status) != "online" { + log.Fatal("Authentication service isn't online, status: '%s'", status) + } + + return service, nil +} + +// Get ... +func (service *AuthenticationService) Get(url string) (*http.Response, error) { + return http.Get(service.URL + "/" + url) +} + +// Authenticate ... +func (service *AuthenticationService) Authenticate(username, password string) bool { + + json, _ := json.Marshal(struct { + Username, Password string + }{username, password}) + + res, _ := http.Post(service.URL+"/auth", "application/json", bytes.NewReader(json)) + + result, _ := ioutil.ReadAll(res.Body) + + return string(result) == "true" +} diff --git a/go.mod b/go.mod index 1674a62..fb6a158 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ -module phc.dm.unipi.it/aziis98/nuovo-sito-poisson +module phc.dm.unipi.it/server-poisson go 1.13 + +require github.com/gorilla/mux v1.7.4 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..abb0613 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= +github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= diff --git a/main.go b/main.go index 66bc39c..41561c3 100644 --- a/main.go +++ b/main.go @@ -1,26 +1,45 @@ package main import ( + "html/template" "log" "net/http" ) -func serveHome(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "./views/home.html") +// func serveHome(w http.ResponseWriter, r *http.Request) { +// http.ServeFile(w, r, "./views/home.html") +// } + +var templates = template.Must(template.ParseFiles("views/base.html")) + +func newTemplateHandler(file string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + t, _ := (template.Must(templates.Clone()).ParseFiles("views/" + file)) + t.ExecuteTemplate(w, "base", nil) + }) } func main() { - // Serve assets files - fs := http.FileServer(http.Dir("./assets")) - http.Handle("/assets/", http.StripPrefix("/assets/", fs)) + mux := http.NewServeMux() // Serve homepage - http.HandleFunc("/", serveHome) + homeTmpl := newTemplateHandler("home.html") + mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + if req.URL.Path == "/" { + homeTmpl.ServeHTTP(w, req) + } else { + http.NotFound(w, req) + } + }) + + mux.Handle("/utenti", newTemplateHandler("utenti.html")) + mux.Handle("/login", newTemplateHandler("login.html")) + + // Serve assets files + fs := http.FileServer(http.Dir("./assets")) + mux.Handle("/assets/", http.StripPrefix("/assets", fs)) log.Println("Listening on :8000...") - err := http.ListenAndServe(":8000", nil) - if err != nil { - log.Fatal(err) - } + log.Fatal(http.ListenAndServe(":8000", mux)) } diff --git a/views/base.html b/views/base.html new file mode 100644 index 0000000..12f36ce --- /dev/null +++ b/views/base.html @@ -0,0 +1,56 @@ +{{define "base"}} + + + +
+ + +