2022-01-09 05:27:18 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2022-01-09 06:59:28 +01:00
|
|
|
"github.com/ddworken/hishtory/shared"
|
2022-03-30 06:56:28 +02:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
POSTGRES_DB = "postgresql://postgres:O74Ji4735C@postgres-postgresql.default.svc.cluster.local:5432/hishtory?sslmode=disable"
|
2022-01-09 06:59:28 +01:00
|
|
|
)
|
2022-01-09 05:27:18 +01:00
|
|
|
|
2022-04-03 07:27:20 +02:00
|
|
|
var GLOBAL_DB *gorm.DB
|
|
|
|
|
2022-01-09 05:27:18 +01:00
|
|
|
func apiSubmitHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
var entry shared.HistoryEntry
|
|
|
|
err := decoder.Decode(&entry)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-04-03 07:27:20 +02:00
|
|
|
GLOBAL_DB.Create(&entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func apiESubmitHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
var entries []shared.EncHistoryEntry
|
|
|
|
err := decoder.Decode(&entries)
|
2022-03-30 06:56:28 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-04-04 05:55:37 +02:00
|
|
|
GLOBAL_DB.Where("user_id = ?")
|
2022-04-03 07:27:20 +02:00
|
|
|
for _, entry := range entries {
|
2022-04-03 19:08:18 +02:00
|
|
|
tx := GLOBAL_DB.Where("user_id = ?", entry.UserId)
|
2022-04-04 05:55:37 +02:00
|
|
|
var devices []*shared.Device
|
2022-04-03 19:08:18 +02:00
|
|
|
result := tx.Find(&devices)
|
|
|
|
if result.Error != nil {
|
|
|
|
panic(fmt.Errorf("DB query error: %v", result.Error))
|
|
|
|
}
|
2022-04-04 05:55:37 +02:00
|
|
|
if len(devices) == 0 {
|
2022-04-03 19:08:18 +02:00
|
|
|
panic(fmt.Errorf("Found no devices associated with user_id=%s, can't save history entry!", entry.UserId))
|
|
|
|
}
|
|
|
|
for _, device := range devices {
|
2022-04-04 05:55:37 +02:00
|
|
|
entry.DeviceId = device.DeviceId
|
2022-04-03 19:08:18 +02:00
|
|
|
GLOBAL_DB.Create(&entry)
|
|
|
|
}
|
2022-04-03 07:27:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func apiEQueryHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
deviceId := r.URL.Query().Get("device_id")
|
2022-04-04 05:55:37 +02:00
|
|
|
// Increment the count
|
2022-04-03 07:27:20 +02:00
|
|
|
GLOBAL_DB.Exec("UPDATE enc_history_entries SET read_count = read_count + 1 WHERE device_id = ?", deviceId)
|
|
|
|
|
|
|
|
// Then retrieve, to avoid a race condition
|
|
|
|
tx := GLOBAL_DB.Where("device_id = ?", deviceId)
|
|
|
|
var historyEntries []*shared.EncHistoryEntry
|
|
|
|
result := tx.Find(&historyEntries)
|
|
|
|
if result.Error != nil {
|
|
|
|
panic(fmt.Errorf("DB query error: %v", result.Error))
|
|
|
|
}
|
|
|
|
resp, err := json.Marshal(historyEntries)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
w.Write(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func apiEBootstrapHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userId := r.URL.Query().Get("user_id")
|
|
|
|
tx := GLOBAL_DB.Where("user_id = ?", userId)
|
|
|
|
var historyEntries []*shared.EncHistoryEntry
|
|
|
|
result := tx.Find(&historyEntries)
|
|
|
|
if result.Error != nil {
|
|
|
|
panic(fmt.Errorf("DB query error: %v", result.Error))
|
|
|
|
}
|
|
|
|
resp, err := json.Marshal(historyEntries)
|
2022-01-09 05:27:18 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-04-03 07:27:20 +02:00
|
|
|
w.Write(resp)
|
|
|
|
}
|
|
|
|
|
2022-04-03 19:08:18 +02:00
|
|
|
func apiERegisterHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userId := r.URL.Query().Get("user_id")
|
|
|
|
deviceId := r.URL.Query().Get("device_id")
|
|
|
|
GLOBAL_DB.Create(&shared.Device{UserId: userId, DeviceId: deviceId})
|
2022-01-09 05:27:18 +01:00
|
|
|
}
|
|
|
|
|
2022-03-30 06:56:28 +02:00
|
|
|
func OpenDB() (*gorm.DB, error) {
|
|
|
|
if shared.IsTestEnvironment() {
|
|
|
|
return shared.OpenLocalSqliteDb()
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := gorm.Open(postgres.Open(POSTGRES_DB), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to connect to the DB: %v", err)
|
|
|
|
}
|
|
|
|
db.AutoMigrate(&shared.HistoryEntry{})
|
2022-04-03 07:27:20 +02:00
|
|
|
db.AutoMigrate(&shared.EncHistoryEntry{})
|
|
|
|
db.AutoMigrate(&shared.Device{})
|
2022-03-30 06:56:28 +02:00
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2022-01-09 05:27:18 +01:00
|
|
|
func apiSearchHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userSecret := r.URL.Query().Get("user_secret")
|
|
|
|
query := r.URL.Query().Get("query")
|
2022-01-09 20:00:53 +01:00
|
|
|
fmt.Println("Received search query: " + query)
|
2022-01-09 05:27:18 +01:00
|
|
|
limitStr := r.URL.Query().Get("limit")
|
|
|
|
limit, err := strconv.Atoi(limitStr)
|
|
|
|
if err != nil {
|
|
|
|
limit = 0
|
|
|
|
}
|
2022-04-03 07:27:20 +02:00
|
|
|
entries, err := shared.Search(GLOBAL_DB, userSecret, query, limit)
|
2022-01-09 05:27:18 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
|
|
entry.UserSecret = ""
|
|
|
|
}
|
|
|
|
resp, err := json.Marshal(entries)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
w.Write(resp)
|
|
|
|
}
|
|
|
|
|
2022-04-03 07:27:20 +02:00
|
|
|
func init() {
|
2022-04-03 07:54:09 +02:00
|
|
|
InitDB()
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitDB() {
|
2022-04-03 07:27:20 +02:00
|
|
|
var err error
|
|
|
|
GLOBAL_DB, err = OpenDB()
|
2022-01-09 05:27:18 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-04-03 07:27:20 +02:00
|
|
|
tx, err := GLOBAL_DB.DB()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = tx.Ping()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-01-09 05:27:18 +01:00
|
|
|
fmt.Println("Listening on localhost:8080")
|
|
|
|
http.HandleFunc("/api/v1/submit", apiSubmitHandler)
|
|
|
|
http.HandleFunc("/api/v1/search", apiSearchHandler)
|
2022-04-03 07:27:20 +02:00
|
|
|
http.HandleFunc("/api/v1/esubmit", apiESubmitHandler)
|
|
|
|
http.HandleFunc("/api/v1/equery", apiEQueryHandler)
|
|
|
|
http.HandleFunc("/api/v1/ebootstrap", apiEBootstrapHandler)
|
2022-04-03 19:08:18 +02:00
|
|
|
http.HandleFunc("/api/v1/eregister", apiERegisterHandler)
|
2022-01-09 05:27:18 +01:00
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
}
|