Struttura di base, templating e interfaccia per il servizio di autenticazione

main-old
Antonio De Lucreziis 5 years ago
parent bffe3056f7
commit aa21c64880

@ -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;

@ -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"
}

@ -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

@ -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=

@ -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))
}

@ -0,0 +1,56 @@
{{define "base"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{template "title" .}}</title>
<link rel="stylesheet" href="/assets/style.css">
</head>
<body>
<nav>
<!-- Site -->
<div class="nav-item">
<a href="/">Poisson</a>
</div>
<div class="nav-item">
<a href="/utenti">Utenti</a>
</div>
<div class="nav-item">
<a href="/notizie">Notizie</a>
</div>
<div class="nav-item">
<a href="/guide">Guide</a>
</div>
<div class="nav-item">
<a href="/git">Git</a>
</div>
<div class="nav-item">
Forum
</div>
<!-- Spacing -->
<div class="hfill"></div>
<!-- User -->
{{if .User}}
<div class="nav-item">
<a href="/logout">Logout</a>
</div>
{{else}}
<div class="nav-item">
<a href="/login">Login</a>
</div>
{{end}}
</nav>
{{template "body" .}}
<footer>
macchinisti@poisson.phc.dm.unipi.it
</footer>
</body>
</html>
{{end}}

@ -1,84 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHC</title>
<link rel="stylesheet" href="/assets/style.css">
</head>
<body>
<nav>
<!-- Site -->
<div class="nav-item">
<a href="/">Poisson</a>
</div>
<div class="nav-item">
<a href="/utenti">Utenti</a>
</div>
<div class="nav-item">
<a href="/notizie">Notizie</a>
</div>
<div class="nav-item">
<a href="/guide">Guide</a>
</div>
<div class="nav-item">
<a href="/git">Git</a>
{{template "base" .}}
{{define "title"}}Poisson{{end}}
{{define "body"}}
<h1>Poisson</h1>
<h2>Macchinisti</h2>
<ul>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
</ul>
<div class="news-list">
<article>
<h3>Notizie 1</h3>
<div class="description">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet quis eligendi aut consequatur quasi
veniam animi ducimus enim consectetur a.
</div>
<div class="nav-item">
Forum
</article>
<article>
<h3>Notizie 2</h3>
<div class="description">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet quis eligendi aut consequatur quasi
veniam animi ducimus enim consectetur a.
</div>
<div class="hfill"></div>
<!-- User -->
<div class="nav-item">
<a href="/login">Login</a>
</article>
<article>
<h3>Notizie 3</h3>
<div class="description">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet quis eligendi aut consequatur quasi
veniam animi ducimus enim consectetur a.
</div>
</nav>
<h1>Poisson</h1>
<h2>Macchinisti</h2>
<ul>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
<li>Persona 1 &mdash; <a href="poisson.phc.dm.unipi.it/~persona">poisson.phc.dm.unipi.it/~persona</a></li>
</ul>
<div class="news-list">
<article>
<h3>Notizie 1</h3>
<div class="description">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet quis eligendi aut consequatur quasi
veniam animi ducimus enim consectetur a.
</div>
</article>
<article>
<h3>Notizie 2</h3>
<div class="description">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet quis eligendi aut consequatur quasi
veniam animi ducimus enim consectetur a.
</div>
</article>
<article>
<h3>Notizie 3</h3>
<div class="description">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Amet quis eligendi aut consequatur quasi
veniam animi ducimus enim consectetur a.
</div>
</article>
</div>
<footer>
&copy; macchinisti@poisson.phc.dm.unipi.it
</footer>
</body>
</html>
</article>
</div>
{{end}}

@ -0,0 +1,20 @@
{{template "base" .}}
{{define "title"}}Poisson - Utenti{{end}}
{{define "body"}}
<h1>Login</h1>
<p>Inserisci le tue credenziali di Poisson per accedere</p>
<form action="/login" method="post">
<label for="login-username">Username:</label>
<input type="text" name="username" id="login-username">
<label for="login-password">Password:</label>
<input type="text" name="password" id="login-password">
<input type="submit" value="Login">
</form>
{{end}}

@ -0,0 +1,30 @@
{{template "base" .}}
{{define "title"}}Poisson - Utenti{{end}}
{{define "body"}}
<h1>Utenti</h1>
<ul>
<li>Persona 1</li>
<li>Persona 2</li>
<li>Persona 3</li>
<li>Persona 4</li>
<li>Persona 5</li>
<li>Persona 6</li>
<li>Persona 7</li>
<li>Persona 8</li>
<li>Persona 9</li>
<li>Persona 10</li>
<li>Persona 11</li>
<li>Persona 12</li>
<li>Persona 13</li>
<li>Persona 14</li>
<li>Persona 15</li>
<li>Persona 16</li>
<li>Persona 17</li>
<li>Persona 18</li>
<li>Persona 19</li>
<li>Persona 20</li>
</ul>
{{end}}
Loading…
Cancel
Save