Browse Source

Move momentsHandler to moments.go

master
Peter Stuifzand 3 years ago
parent
commit
b18ea7b7f4
  1. 45
      main.go
  2. 55
      moments.go

45
main.go

@ -176,51 +176,6 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
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")

55
moments.go

@ -0,0 +1,55 @@
package main
import (
"encoding/json"
"go.etcd.io/bbolt"
"log"
"net/http"
"time"
)
type momentHandler struct {
DB *bbolt.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
}
}
Loading…
Cancel
Save