mirror of https://github.com/status-im/fathom.git
envelope all json responses in api endpoints
This commit is contained in:
parent
b3f47e3bc3
commit
9200df8569
12
api/api.go
12
api/api.go
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
@ -11,6 +12,17 @@ import (
|
|||
const defaultPeriod = 7
|
||||
const defaultLimit = 10
|
||||
|
||||
type envelope struct {
|
||||
Data interface{}
|
||||
Error interface{}
|
||||
}
|
||||
|
||||
func respond(w http.ResponseWriter, d interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
enc := json.NewEncoder(w)
|
||||
enc.Encode(d)
|
||||
}
|
||||
|
||||
// log fatal errors
|
||||
func checkError(err error) {
|
||||
if err != nil {
|
||||
|
|
12
api/auth.go
12
api/auth.go
|
@ -20,17 +20,19 @@ var store = sessions.NewFilesystemStore("./storage/sessions/", []byte(os.Getenv(
|
|||
|
||||
// URL: POST /api/session
|
||||
var LoginHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var l login
|
||||
json.NewDecoder(r.Body).Decode(&l)
|
||||
|
||||
// check login creds
|
||||
var l login
|
||||
json.NewDecoder(r.Body).Decode(&l)
|
||||
var hashedPassword string
|
||||
var u models.User
|
||||
stmt, _ := db.Conn.Prepare("SELECT id, email, password FROM users WHERE email = ? LIMIT 1")
|
||||
err := stmt.QueryRow(l.Email).Scan(&u.ID, &u.Email, &hashedPassword)
|
||||
|
||||
// compare pwd
|
||||
if err != nil || bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(l.Password)) != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
respond(w, envelope{Error: "invalid_credentials"})
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -40,8 +42,7 @@ var LoginHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)
|
|||
err = session.Save(r, w)
|
||||
checkError(err)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte("true"))
|
||||
respond(w, envelope{Data: true})
|
||||
})
|
||||
|
||||
// URL: DELETE /api/session
|
||||
|
@ -52,8 +53,7 @@ var LogoutHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request
|
|||
session.Save(r, w)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte("true"))
|
||||
respond(w, envelope{Data: true})
|
||||
})
|
||||
|
||||
/* middleware */
|
||||
|
|
|
@ -1,17 +1,13 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// URL: /api/browsers
|
||||
var GetBrowsersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
results := count.Browsers(before, after, getRequestedLimit(r))
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(results)
|
||||
respond(w, envelope{Data: results})
|
||||
})
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
|
@ -11,6 +10,5 @@ import (
|
|||
var GetLanguagesHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
results := count.Languages(before, after, getRequestedLimit(r))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(results)
|
||||
respond(w, envelope{Data: results})
|
||||
})
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
|
@ -44,22 +43,19 @@ var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
|
|||
err = rows.Err()
|
||||
checkError(err)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(results)
|
||||
respond(w, envelope{Data: results})
|
||||
})
|
||||
|
||||
// URL: /api/pageviews/count
|
||||
var GetPageviewsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
result := count.Pageviews(before, after)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(result)
|
||||
respond(w, envelope{Data: result})
|
||||
})
|
||||
|
||||
// URL: /api/pageviews/group/day
|
||||
var GetPageviewsPeriodCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
results := count.PageviewsPerDay(before, after)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(results)
|
||||
respond(w, envelope{Data: results})
|
||||
})
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
|
@ -11,6 +10,5 @@ import (
|
|||
var GetReferrersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
results := count.Referrers(before, after, getRequestedLimit(r))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(results)
|
||||
respond(w, envelope{Data: results})
|
||||
})
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
|
@ -11,6 +10,5 @@ import (
|
|||
var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
results := count.Screens(before, after, getRequestedLimit(r))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(results)
|
||||
respond(w, envelope{Data: results})
|
||||
})
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/dannyvankooten/ana/count"
|
||||
|
@ -11,21 +10,18 @@ import (
|
|||
var GetVisitorsCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
result := count.Visitors(before, after)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(result)
|
||||
respond(w, envelope{Data: result})
|
||||
})
|
||||
|
||||
// URL: /api/visitors/count/realtime
|
||||
var GetVisitorsRealtimeCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
result := count.RealtimeVisitors()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(result)
|
||||
respond(w, envelope{Data: result})
|
||||
})
|
||||
|
||||
// URL: /api/visitors/count/group/:period
|
||||
var GetVisitorsPeriodCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
before, after := getRequestedPeriods(r)
|
||||
results := count.VisitorsPerDay(before, after)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(results)
|
||||
respond(w, envelope{Data: results})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue