Add session and small improvements
This commit is contained in:
parent
00b73b8ac2
commit
f9c3dbe6ae
123
main.go
123
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const BucketKeyMoments = "moments"
|
const BucketKeyMoments = "moments"
|
||||||
|
const DBFilename = "./moments.db"
|
||||||
|
|
||||||
var indexPageTemplate = `<html>
|
var indexPageTemplate = `<html>
|
||||||
<head>
|
<head>
|
||||||
|
@ -33,7 +35,8 @@ var indexPageTemplate = `<html>
|
||||||
<form action="/moment" class="form" method="post">
|
<form action="/moment" class="form" method="post">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">What did you do in the last <em class="js-counter"></em> minutes:</label>
|
<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>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<div class="control">
|
<div class="control">
|
||||||
|
@ -46,18 +49,19 @@ var indexPageTemplate = `<html>
|
||||||
{{ if .Moments }}
|
{{ if .Moments }}
|
||||||
<div class="section">
|
<div class="section">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
{{ range .Moments }}
|
<table class="table">
|
||||||
<div class="media">
|
{{ range .Moments }}
|
||||||
<div class="media-body">
|
<tr>
|
||||||
{{ (.Time.Format "15:04") }}
|
<td>{{ (.Time.Format "15:04") }}</td>
|
||||||
{{ .Memo }}
|
<td style="text-align:right">{{ if ne .Diff 0 }}{{ .Diff }}m{{ else }}start{{ end }}</td>
|
||||||
</div>
|
<td> {{ .Memo }}</td>
|
||||||
</div>
|
</tr>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
var timeout = 20000;
|
var timeout = 20000;
|
||||||
var timeoutID = setTimeout(adjustCounterText, timeout);
|
var timeoutID = setTimeout(adjustCounterText, timeout);
|
||||||
var lastTime = {{ .LastMomentSeconds }};
|
var lastTime = {{ .LastMomentSeconds }};
|
||||||
|
|
||||||
|
@ -66,43 +70,43 @@ var indexPageTemplate = `<html>
|
||||||
function adjustCounterText() {
|
function adjustCounterText() {
|
||||||
var counters = document.querySelectorAll(".js-counter");
|
var counters = document.querySelectorAll(".js-counter");
|
||||||
var date = new Date();
|
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) {
|
if (minutes >= 15) {
|
||||||
notifyMe(minutes + " minutes passed since writing the last memory");
|
notifyMe(minutes + " minutes passed since writing the last memory");
|
||||||
}
|
}
|
||||||
|
|
||||||
counters.forEach(function (v) {
|
counters.forEach(function (v) {
|
||||||
v.innerHTML = minutes+"";
|
v.innerHTML = minutes + "";
|
||||||
})
|
})
|
||||||
setTimeout(adjustCounterText, timeout);
|
setTimeout(adjustCounterText, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
function notifyMe(text) {
|
function notifyMe(text) {
|
||||||
// Let's check if the browser supports notifications
|
// Let's check if the browser supports notifications
|
||||||
if (!("Notification" in window)) {
|
if (!("Notification" in window)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's check whether notification permissions have already been granted
|
// Let's check whether notification permissions have already been granted
|
||||||
else if (Notification.permission === "granted") {
|
else if (Notification.permission === "granted") {
|
||||||
// If it's okay let's create a notification
|
// If it's okay let's create a notification
|
||||||
var notification = new Notification(text);
|
var notification = new Notification(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, we need to ask the user for permission
|
// Otherwise, we need to ask the user for permission
|
||||||
else if (Notification.permission !== "denied") {
|
else if (Notification.permission !== "denied") {
|
||||||
Notification.requestPermission(function (permission) {
|
Notification.requestPermission(function (permission) {
|
||||||
// If the user accepts, let's create a notification
|
// If the user accepts, let's create a notification
|
||||||
if (permission === "granted") {
|
if (permission === "granted") {
|
||||||
var notification = new Notification(text);
|
var notification = new Notification(text);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// At last, if the user has denied notifications, and you
|
// At last, if the user has denied notifications, and you
|
||||||
// want to be respectful there is no need to bother them any more.
|
// want to be respectful there is no need to bother them any more.
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</body>
|
</body>
|
||||||
|
@ -113,13 +117,15 @@ function notifyMe(text) {
|
||||||
type Moment struct {
|
type Moment struct {
|
||||||
Key string
|
Key string
|
||||||
Time time.Time
|
Time time.Time
|
||||||
|
Diff int64
|
||||||
Memo string
|
Memo string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// main is the main function
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Starting tracking backend server")
|
fmt.Println("Starting tracking backend server")
|
||||||
|
|
||||||
path := "./moments.db"
|
path := DBFilename
|
||||||
db, err := bolt.Open(path, 0666, nil)
|
db, err := bolt.Open(path, 0666, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -128,6 +134,13 @@ func main() {
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
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"))
|
moments, err := loadMoments(db, time.Now().Format("2006-01-02"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -142,8 +155,8 @@ func main() {
|
||||||
|
|
||||||
if len(moments) > 0 {
|
if len(moments) > 0 {
|
||||||
a := moments
|
a := moments
|
||||||
for i := len(a)/2-1; i >= 0; i-- {
|
for i := len(a)/2 - 1; i >= 0; i-- {
|
||||||
opp := len(a)-1-i
|
opp := len(a) - 1 - i
|
||||||
a[i], a[opp] = a[opp], a[i]
|
a[i], a[opp] = a[opp], a[i]
|
||||||
}
|
}
|
||||||
lastMoment := moments[0]
|
lastMoment := moments[0]
|
||||||
|
@ -162,9 +175,14 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/moment", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/moment", func(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
sess, err := NewSession(w, r)
|
||||||
|
if err != nil{
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer sess.Flush()
|
||||||
|
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
moments, err := loadMoments(db, "")
|
moments, err := loadMoments(db, "")
|
||||||
|
@ -195,10 +213,10 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(":8096", nil))
|
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) {
|
func loadMoments(db *bolt.DB, today string) ([]Moment, error) {
|
||||||
var moments []Moment
|
var moments []Moment
|
||||||
|
|
||||||
|
@ -214,12 +232,25 @@ func loadMoments(db *bolt.DB, today string) ([]Moment, error) {
|
||||||
c := b.Cursor()
|
c := b.Cursor()
|
||||||
|
|
||||||
prefix := []byte(today)
|
prefix := []byte(today)
|
||||||
|
|
||||||
|
var prevTime time.Time
|
||||||
|
|
||||||
for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
|
||||||
var moment Moment
|
var moment Moment
|
||||||
err := json.Unmarshal(v, &moment)
|
err := json.Unmarshal(v, &moment)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
moments = append(moments, moment)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,6 +261,8 @@ func loadMoments(db *bolt.DB, today string) ([]Moment, error) {
|
||||||
return moments, err
|
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 {
|
func saveMemo(db *bolt.DB, timestamp time.Time, memo string) error {
|
||||||
err := db.Update(func(tx *bolt.Tx) error {
|
err := db.Update(func(tx *bolt.Tx) error {
|
||||||
bucket, err := tx.CreateBucketIfNotExists([]byte(BucketKeyMoments))
|
bucket, err := tx.CreateBucketIfNotExists([]byte(BucketKeyMoments))
|
||||||
|
|
87
session.go
Normal file
87
session.go
Normal 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
15
util.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user