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.URL.Path == "" {
|
||||
fmt.Fprint(w, `
|
||||
<!doctype html>
|
||||
fmt.Fprint(w, `<!doctype html>
|
||||
<html>
|
||||
<form action="/auth/login" method="post">
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
<input type="url" name="url" class="input" placeholder="url" />
|
||||
<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>
|
||||
</div>
|
||||
</form>
|
||||
</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()
|
||||
|
|
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">
|
||||
<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,6 +122,105 @@ type Moment struct {
|
|||
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
|
||||
func main() {
|
||||
fmt.Println("Starting tracking backend server")
|
||||
|
@ -136,86 +236,11 @@ func main() {
|
|||
indieAuthHandler := &IndieAuthHandler{}
|
||||
http.Handle("/auth/", http.StripPrefix("/auth/", indieAuthHandler))
|
||||
|
||||
http.HandleFunc("/", func(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()
|
||||
ih := &indexHandler{DB: db}
|
||||
http.Handle("/", ih)
|
||||
|
||||
moments, err := loadMoments(db, time.Now().Format("2006-01-02"))
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
})
|
||||
mh := &momentHandler{DB: db}
|
||||
http.Handle("/moment", mh)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8096", nil))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user