Add session and small improvements

This commit is contained in:
Peter Stuifzand 2018-09-29 16:28:43 +02:00
parent 00b73b8ac2
commit f9c3dbe6ae
3 changed files with 180 additions and 45 deletions

123
main.go
View File

@ -6,6 +6,7 @@ import (
"fmt"
"html/template"
"log"
"math"
"net/http"
"time"
@ -13,6 +14,7 @@ import (
)
const BucketKeyMoments = "moments"
const DBFilename = "./moments.db"
var indexPageTemplate = `<html>
<head>
@ -33,7 +35,8 @@ var indexPageTemplate = `<html>
<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 class="control"><input type="text" name="memo" class="input" autofocus
placeholder="add note / accomplishments"></div>
</div>
<div class="field">
<div class="control">
@ -46,18 +49,19 @@ var indexPageTemplate = `<html>
{{ 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 }}
<table class="table">
{{ range .Moments }}
<tr>
<td>{{ (.Time.Format "15:04") }}</td>
<td style="text-align:right">{{ if ne .Diff 0 }}{{ .Diff }}m{{ else }}start{{ end }}</td>
<td> {{ .Memo }}</td>
</tr>
{{ end }}
</table>
</div>
</div>
<script>
var timeout = 20000;
var timeout = 20000;
var timeoutID = setTimeout(adjustCounterText, timeout);
var lastTime = {{ .LastMomentSeconds }};
@ -66,43 +70,43 @@ var indexPageTemplate = `<html>
function adjustCounterText() {
var counters = document.querySelectorAll(".js-counter");
var date = new Date();
var minutes = Math.floor((date.getTime()/1000 - lastTime)/60);
var minutes = Math.floor((date.getTime() / 1000 - lastTime) / 60);
if (minutes >= 15) {
notifyMe(minutes + " minutes passed since writing the last memory");
}
if (minutes >= 15) {
notifyMe(minutes + " minutes passed since writing the last memory");
}
counters.forEach(function (v) {
v.innerHTML = minutes+"";
})
setTimeout(adjustCounterText, timeout);
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;
}
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);
}
// 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);
}
});
}
// 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.
}
// 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>
@ -113,13 +117,15 @@ function notifyMe(text) {
type Moment struct {
Key string
Time time.Time
Diff int64
Memo string
}
// main is the main function
func main() {
fmt.Println("Starting tracking backend server")
path := "./moments.db"
path := DBFilename
db, err := bolt.Open(path, 0666, nil)
if err != nil {
log.Println(err)
@ -128,6 +134,13 @@ func main() {
defer db.Close()
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()
moments, err := loadMoments(db, time.Now().Format("2006-01-02"))
if err != nil {
log.Println(err)
@ -142,8 +155,8 @@ func main() {
if len(moments) > 0 {
a := moments
for i := len(a)/2-1; i >= 0; i-- {
opp := len(a)-1-i
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]
@ -162,9 +175,14 @@ func main() {
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, "")
@ -195,10 +213,10 @@ func main() {
return
}
})
log.Fatal(http.ListenAndServe(":8096", nil))
}
// loadMoments loads the moments with today as the prefix of the key from the database
func loadMoments(db *bolt.DB, today string) ([]Moment, error) {
var moments []Moment
@ -214,12 +232,25 @@ func loadMoments(db *bolt.DB, today string) ([]Moment, error) {
c := b.Cursor()
prefix := []byte(today)
var prevTime time.Time
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
}
if prevTime.IsZero() {
moment.Diff = 0
} else {
d := float64(moment.Time.Sub(prevTime)) / 1000000000.0 / 60.0
moment.Diff = int64(math.Ceil(d))
}
prevTime = moment.Time
moments = append(moments, moment)
}
@ -230,6 +261,8 @@ func loadMoments(db *bolt.DB, today string) ([]Moment, error) {
return moments, err
}
// saveMemo saves one memo to the database, it automatically generates a key
// based on the timestamp
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))

87
session.go Normal file
View File

@ -0,0 +1,87 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"
)
type Session struct {
ID string
}
func NewSession(w http.ResponseWriter, r *http.Request) (*Session, error) {
sessionID, err := getSessionCookie(w, r)
if err != nil {
return nil, err
}
session := &Session{ID: sessionID}
err = loadSession(session)
if err != nil {
return nil, err
}
return session, nil
}
func (sess *Session) Flush() error {
return saveSession(sess)
}
func saveSession(sess *Session) error {
filename := generateFilename(sess.ID)
err := os.Mkdir("session", 0755)
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
err = json.NewEncoder(f).Encode(sess)
return err
}
func loadSession(sess *Session) error {
filename := generateFilename(sess.ID)
err := os.Mkdir("session", 0755)
f, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
// add defaults to session?
return nil
}
return err
}
defer f.Close()
err = json.NewDecoder(f).Decode(sess)
return err
}
func generateFilename(id string) string {
return fmt.Sprintf("session/%s.json", id)
}
func getSessionCookie(w http.ResponseWriter, r *http.Request) (string, error) {
c, err := r.Cookie("session")
var sessionVar string
if err != nil {
if err == http.ErrNoCookie {
sessionVar = RandStringBytes(16)
newCookie := &http.Cookie{
Name: "session",
Value: sessionVar,
Expires: time.Now().Add(24 * time.Hour),
}
http.SetCookie(w, newCookie)
return sessionVar, nil
}
return "", err
} else {
sessionVar = c.Value
}
return sessionVar, nil
}

15
util.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"math/rand"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}