Move handler to own type and add logout

This commit is contained in:
Peter Stuifzand 2018-09-29 22:08:14 +02:00
parent d253dea160
commit 8406cd6fd6
2 changed files with 143 additions and 88 deletions

View File

@ -32,16 +32,37 @@ func (h *IndieAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
if r.URL.Path == "" {
fmt.Fprint(w, `
<!doctype html>
fmt.Fprint(w, `<!doctype html>
<html>
<head>
<title>Login - Track Me</title>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css"
integrity="sha256-zIG416V1ynj3Wgju/scU80KAEWOsO5rRLfVyRDuOv7Q=" crossorigin="anonymous"/>
</head>
<body>
<div class="section">
<div class="container">
<form action="/auth/login" method="post">
<h1 class="title">Login in with your own website</h1>
<div class="field">
<label class="label">Web Sign In:</label>
<div class="control">
<input type="url" name="url" class="input" placeholder="url" />
</div>
</div>
<div class="field">
<div class="control">
<button class="button is-primary" type="submit">Sign in</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
`)
return
@ -66,6 +87,15 @@ func (h *IndieAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
return
} else if r.URL.Path == "logout" {
sess.LoggedIn = false
sess.State = ""
sess.Me = ""
sess.AuthorizationEndpoint = ""
sess.NextURI = ""
sess.RedirectURI = ""
http.Redirect(w, r, "/", 302)
return
}
} else if r.Method == http.MethodPost {
r.ParseForm()

67
main.go
View File

@ -23,13 +23,14 @@ var indexPageTemplate = `<html>
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Add moments of your day</title>
<link rel="stylesheet" href="">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css"
integrity="sha256-zIG416V1ynj3Wgju/scU80KAEWOsO5rRLfVyRDuOv7Q=" crossorigin="anonymous"/>
</head>
<body>
<div class="section">
<div class="container">
<p>Logged in as: <a href="{{ .Me }}">{{ .Me }}</a> | <a href="/auth/logout">Logout</a></p>
<h1 class="title">Remember moments: <em class="js-counter is-primary">N</em> minutes since your last memo</h1>
<form action="/moment" class="form" method="post">
@ -121,22 +122,11 @@ type Moment struct {
Memo string
}
// main is the main function
func main() {
fmt.Println("Starting tracking backend server")
path := DBFilename
db, err := bolt.Open(path, 0666, nil)
if err != nil {
log.Println(err)
return
type indexHandler struct {
DB *bolt.DB
}
defer db.Close()
indieAuthHandler := &IndieAuthHandler{}
http.Handle("/auth/", http.StripPrefix("/auth/", indieAuthHandler))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sess, err := NewSession(w, r)
if err != nil {
log.Printf("Error loading session: %s", err)
@ -144,7 +134,12 @@ func main() {
}
defer sess.Flush()
moments, err := loadMoments(db, time.Now().Format("2006-01-02"))
if !sess.LoggedIn {
http.Redirect(w, r, "/auth/", 302)
return
}
moments, err := loadMoments(h.DB, time.Now().Format("2006-01-02"))
if err != nil {
log.Println(err)
}
@ -152,9 +147,11 @@ func main() {
type indexPageInfo struct {
Moments []Moment
LastMomentSeconds int64
Me string
}
indexPage := indexPageInfo{Moments: moments}
indexPage.Me = sess.Me
if len(moments) > 0 {
a := moments
@ -177,8 +174,13 @@ func main() {
log.Println(err)
return
}
})
http.HandleFunc("/moment", func(w http.ResponseWriter, r *http.Request) {
}
type momentHandler struct {
DB *bolt.DB
}
func (mh *momentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
sess, err := NewSession(w, r)
if err != nil {
@ -188,11 +190,13 @@ func main() {
defer sess.Flush()
if r.Method == http.MethodGet {
moments, err := loadMoments(db, "")
moments, err := loadMoments(mh.DB, "")
if err != nil {
log.Println(err)
}
w.Header().Add("Content-Type", "application/json; charset=utf8")
err = json.NewEncoder(w).Encode(&moments)
if err != nil {
log.Println(err)
@ -206,7 +210,7 @@ func main() {
}
memo := r.FormValue("memo")
timestamp := time.Now()
err = saveMemo(db, timestamp, memo)
err = saveMemo(mh.DB, timestamp, memo)
if err != nil {
log.Println(err)
}
@ -215,7 +219,28 @@ func main() {
http.Error(w, "Method Not Allowed", 405)
return
}
})
}
// main is the main function
func main() {
fmt.Println("Starting tracking backend server")
path := DBFilename
db, err := bolt.Open(path, 0666, nil)
if err != nil {
log.Println(err)
return
}
defer db.Close()
indieAuthHandler := &IndieAuthHandler{}
http.Handle("/auth/", http.StripPrefix("/auth/", indieAuthHandler))
ih := &indexHandler{DB: db}
http.Handle("/", ih)
mh := &momentHandler{DB: db}
http.Handle("/moment", mh)
log.Fatal(http.ListenAndServe(":8096", nil))
}