Move handler to own type and add logout
This commit is contained in:
parent
d253dea160
commit
8406cd6fd6
46
indieauth.go
46
indieauth.go
|
@ -32,16 +32,37 @@ func (h *IndieAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
if r.URL.Path == "" {
|
if r.URL.Path == "" {
|
||||||
fmt.Fprint(w, `
|
fmt.Fprint(w, `<!doctype html>
|
||||||
<!doctype html>
|
|
||||||
<html>
|
<html>
|
||||||
<form action="/auth/login" method="post">
|
<head>
|
||||||
<div class="field">
|
<title>Login - Track Me</title>
|
||||||
<div class="control">
|
<meta charset="UTF-8">
|
||||||
<input type="url" name="url" class="input" placeholder="url" />
|
<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>
|
</div>
|
||||||
</div>
|
</body>
|
||||||
</form>
|
|
||||||
</html>
|
</html>
|
||||||
`)
|
`)
|
||||||
return
|
return
|
||||||
|
@ -66,6 +87,15 @@ func (h *IndieAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
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 {
|
} else if r.Method == http.MethodPost {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
|
185
main.go
185
main.go
|
@ -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">
|
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">
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
<title>Add moments of your day</title>
|
<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"
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css"
|
||||||
integrity="sha256-zIG416V1ynj3Wgju/scU80KAEWOsO5rRLfVyRDuOv7Q=" crossorigin="anonymous"/>
|
integrity="sha256-zIG416V1ynj3Wgju/scU80KAEWOsO5rRLfVyRDuOv7Q=" crossorigin="anonymous"/>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="section">
|
<div class="section">
|
||||||
<div class="container">
|
<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>
|
<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">
|
<form action="/moment" class="form" method="post">
|
||||||
|
@ -121,6 +122,105 @@ type Moment struct {
|
||||||
Memo string
|
Memo string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type indexHandler struct {
|
||||||
|
DB *bolt.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer sess.Flush()
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
type indexPageInfo struct {
|
||||||
|
Moments []Moment
|
||||||
|
LastMomentSeconds int64
|
||||||
|
Me string
|
||||||
|
}
|
||||||
|
|
||||||
|
indexPage := indexPageInfo{Moments: moments}
|
||||||
|
indexPage.Me = sess.Me
|
||||||
|
|
||||||
|
if len(moments) > 0 {
|
||||||
|
a := moments
|
||||||
|
for i := len(a)/2 - 1; i >= 0; i-- {
|
||||||
|
opp := len(a) - 1 - i
|
||||||
|
a[i], a[opp] = a[opp], a[i]
|
||||||
|
}
|
||||||
|
lastMoment := moments[0]
|
||||||
|
indexPage.LastMomentSeconds = lastMoment.Time.Unix()
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := template.New("index").Parse(indexPageTemplate)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.Execute(w, indexPage)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer sess.Flush()
|
||||||
|
|
||||||
|
if r.Method == http.MethodGet {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
} else if r.Method == http.MethodPost {
|
||||||
|
// save input values
|
||||||
|
err := r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
memo := r.FormValue("memo")
|
||||||
|
timestamp := time.Now()
|
||||||
|
err = saveMemo(mh.DB, timestamp, memo)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
} else {
|
||||||
|
http.Error(w, "Method Not Allowed", 405)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// main is the main function
|
// main is the main function
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Starting tracking backend server")
|
fmt.Println("Starting tracking backend server")
|
||||||
|
@ -136,86 +236,11 @@ func main() {
|
||||||
indieAuthHandler := &IndieAuthHandler{}
|
indieAuthHandler := &IndieAuthHandler{}
|
||||||
http.Handle("/auth/", http.StripPrefix("/auth/", indieAuthHandler))
|
http.Handle("/auth/", http.StripPrefix("/auth/", indieAuthHandler))
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
ih := &indexHandler{DB: db}
|
||||||
sess, err := NewSession(w, r)
|
http.Handle("/", ih)
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error loading session: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer sess.Flush()
|
|
||||||
|
|
||||||
moments, err := loadMoments(db, time.Now().Format("2006-01-02"))
|
mh := &momentHandler{DB: db}
|
||||||
if err != nil {
|
http.Handle("/moment", mh)
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
type indexPageInfo struct {
|
|
||||||
Moments []Moment
|
|
||||||
LastMomentSeconds int64
|
|
||||||
}
|
|
||||||
|
|
||||||
indexPage := indexPageInfo{Moments: moments}
|
|
||||||
|
|
||||||
if len(moments) > 0 {
|
|
||||||
a := moments
|
|
||||||
for i := len(a)/2 - 1; i >= 0; i-- {
|
|
||||||
opp := len(a) - 1 - i
|
|
||||||
a[i], a[opp] = a[opp], a[i]
|
|
||||||
}
|
|
||||||
lastMoment := moments[0]
|
|
||||||
indexPage.LastMomentSeconds = lastMoment.Time.Unix()
|
|
||||||
}
|
|
||||||
|
|
||||||
t, err := template.New("index").Parse(indexPageTemplate)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = t.Execute(w, indexPage)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
http.HandleFunc("/moment", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
defer r.Body.Close()
|
|
||||||
sess, err := NewSession(w, r)
|
|
||||||
if err != nil{
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer sess.Flush()
|
|
||||||
|
|
||||||
if r.Method == http.MethodGet {
|
|
||||||
moments, err := loadMoments(db, "")
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(&moments)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
} else if r.Method == http.MethodPost {
|
|
||||||
// save input values
|
|
||||||
err := r.ParseForm()
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
memo := r.FormValue("memo")
|
|
||||||
timestamp := time.Now()
|
|
||||||
err = saveMemo(db, timestamp, memo)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
|
||||||
} else {
|
|
||||||
http.Error(w, "Method Not Allowed", 405)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(":8096", nil))
|
log.Fatal(http.ListenAndServe(":8096", nil))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user