258 lines
6.1 KiB
Go
258 lines
6.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
bolt "go.etcd.io/bbolt"
|
|
)
|
|
|
|
const BucketKeyMoments = "moments"
|
|
|
|
var indexPageTemplate = `<html>
|
|
<head>
|
|
<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">
|
|
<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">
|
|
<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">
|
|
<div class="field">
|
|
<label class="label">What did you do in the last <em class="js-counter"></em> minutes:</label>
|
|
<div class="control"><input type="text" name="memo" class="input" autofocus placeholder="add note / accomplishments"></div>
|
|
</div>
|
|
<div class="field">
|
|
<div class="control">
|
|
<button class="button is-primary" type="submit">Save moment</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
{{ if .Moments }}
|
|
<div class="section">
|
|
<div class="container">
|
|
{{ range .Moments }}
|
|
<div class="media">
|
|
<div class="media-body">
|
|
{{ (.Time.Format "15:04") }}
|
|
{{ .Memo }}
|
|
</div>
|
|
</div>
|
|
{{ end }}
|
|
</div>
|
|
</div>
|
|
<script>
|
|
var timeout = 20000;
|
|
var timeoutID = setTimeout(adjustCounterText, timeout);
|
|
var lastTime = {{ .LastMomentSeconds }};
|
|
|
|
adjustCounterText();
|
|
|
|
function adjustCounterText() {
|
|
var counters = document.querySelectorAll(".js-counter");
|
|
var date = new Date();
|
|
var minutes = Math.floor((date.getTime()/1000 - lastTime)/60);
|
|
|
|
if (minutes >= 15) {
|
|
notifyMe(minutes + " minutes passed since writing the last memory");
|
|
}
|
|
|
|
counters.forEach(function (v) {
|
|
v.innerHTML = minutes+"";
|
|
})
|
|
setTimeout(adjustCounterText, timeout);
|
|
}
|
|
|
|
function notifyMe(text) {
|
|
// Let's check if the browser supports notifications
|
|
if (!("Notification" in window)) {
|
|
return;
|
|
}
|
|
|
|
// Let's check whether notification permissions have already been granted
|
|
else if (Notification.permission === "granted") {
|
|
// If it's okay let's create a notification
|
|
var notification = new Notification(text);
|
|
}
|
|
|
|
// Otherwise, we need to ask the user for permission
|
|
else if (Notification.permission !== "denied") {
|
|
Notification.requestPermission(function (permission) {
|
|
// If the user accepts, let's create a notification
|
|
if (permission === "granted") {
|
|
var notification = new Notification(text);
|
|
}
|
|
});
|
|
}
|
|
|
|
// At last, if the user has denied notifications, and you
|
|
// want to be respectful there is no need to bother them any more.
|
|
}
|
|
</script>
|
|
{{ end }}
|
|
</body>
|
|
</html>
|
|
`
|
|
|
|
// Moment is the main information this servers remembers
|
|
type Moment struct {
|
|
Key string
|
|
Time time.Time
|
|
Memo string
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println("Starting tracking backend server")
|
|
|
|
path := "./moments.db"
|
|
db, err := bolt.Open(path, 0666, nil)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
defer db.Close()
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
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()
|
|
|
|
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))
|
|
}
|
|
|
|
func loadMoments(db *bolt.DB, today string) ([]Moment, error) {
|
|
var moments []Moment
|
|
|
|
err := db.View(func(tx *bolt.Tx) error {
|
|
// Assume bucket exists and has keys
|
|
b := tx.Bucket([]byte(BucketKeyMoments))
|
|
|
|
if b == nil {
|
|
// no bucket found, so moments should be empty
|
|
return nil
|
|
}
|
|
|
|
c := b.Cursor()
|
|
|
|
prefix := []byte(today)
|
|
for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
|
var moment Moment
|
|
err := json.Unmarshal(v, &moment)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
moments = append(moments, moment)
|
|
}
|
|
|
|
// TODO(peter): if there are no moments, here we can add a moment for the start of the day
|
|
return nil
|
|
})
|
|
|
|
return moments, err
|
|
}
|
|
|
|
func saveMemo(db *bolt.DB, timestamp time.Time, memo string) error {
|
|
err := db.Update(func(tx *bolt.Tx) error {
|
|
bucket, err := tx.CreateBucketIfNotExists([]byte(BucketKeyMoments))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
key := timestamp.Format(time.RFC3339)
|
|
|
|
m := Moment{
|
|
Key: key,
|
|
Memo: memo,
|
|
Time: timestamp,
|
|
}
|
|
|
|
buf, err := json.Marshal(m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return bucket.Put([]byte(m.Key), buf)
|
|
})
|
|
|
|
return err
|
|
}
|