summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-07-08 00:57:18 +0200
committerAki <please@ignore.pl>2020-07-08 00:57:18 +0200
commit61fa862c8e7054f13373317fc9a6a78cbb37e313 (patch)
treed63750c1ccdbcf0594bcb4da1d36d1fdf94ca89a
downloadstats-61fa862c8e7054f13373317fc9a6a78cbb37e313.zip
stats-61fa862c8e7054f13373317fc9a6a78cbb37e313.tar.gz
stats-61fa862c8e7054f13373317fc9a6a78cbb37e313.tar.bz2
Initial backend that collects entries
-rw-r--r--.gitignore2
-rw-r--r--entry.go62
-rw-r--r--main.go38
3 files changed, 102 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..54438e8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+stats
+*.db
diff --git a/entry.go b/entry.go
new file mode 100644
index 0000000..544a8c5
--- /dev/null
+++ b/entry.go
@@ -0,0 +1,62 @@
+package main
+
+import (
+ "github.com/jmoiron/sqlx"
+ _ "github.com/mattn/go-sqlite3"
+ "log"
+ "os"
+ "time"
+)
+
+const schema = `
+CREATE TABLE IF NOT EXISTS entries (
+ id INTEGER PRIMARY KEY,
+ location TEXT NOT NULL,
+ referrer TEXT,
+ started_at DATETIME NOT NULL,
+ time_spent INTEGER NOT NULL,
+ scrolled_to REAL NOT NULL
+);`
+
+type Entry struct {
+ Id int `db:"id"`
+ Location string `json:"location" db:"location"`
+ Referrer string `json:"referrer" db:"referrer"`
+ StartedAt time.Time `json:"startedAt" db:"started_at"`
+ TimeSpent int `json:"timeSpent" db:"time_spent"`
+ ScrolledTo float32 `json:"scrolledTo" db:"scrolled_to"`
+}
+
+var db *sqlx.DB
+
+func InitEntries() {
+ file := os.Getenv("STATSDB")
+ if file == "" {
+ log.Println("Defaulting to STATSDB=./stats.db")
+ file = "./stats.db"
+ }
+
+ var err error
+ db, err = sqlx.Connect("sqlite3", file)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ _, err = db.Exec(schema)
+ if err != nil {
+ log.Fatalln("Could not initialize schema")
+ log.Fatalln(err)
+ }
+}
+
+func CloseEntries() {
+ db.Close()
+}
+
+func InsertEntry(entry *Entry) error {
+ const query = `
+ INSERT INTO entries (location, referrer, started_at, time_spent, scrolled_to)
+ VALUES (:location, :referrer, :started_at, :time_spent, :scrolled_to);`
+ _, err := db.NamedExec(query, entry)
+ return err
+}
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..1b5dd5b
--- /dev/null
+++ b/main.go
@@ -0,0 +1,38 @@
+package main
+
+import (
+ "encoding/json"
+ "github.com/gorilla/mux"
+ "log"
+ "net/http"
+)
+
+func handleEntry(w http.ResponseWriter, r *http.Request) {
+ var entry Entry
+
+ err := json.NewDecoder(r.Body).Decode(&entry)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ err = InsertEntry(&entry)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+func handleRequests() {
+ router := mux.NewRouter()
+ router.HandleFunc("/entry", handleEntry).Methods("POST")
+
+ log.Fatal(http.ListenAndServe(":8080", router))
+}
+
+func main() {
+ log.Println("Starting up")
+ InitEntries()
+ defer CloseEntries()
+ handleRequests()
+}