Compare commits

...

10 commits

Author SHA1 Message Date
Simon Rieger
4bb6f33028 README.md aktualisiert 2023-11-26 12:58:13 +01:00
Simon Rieger
e6e6f91955 README.md aktualisiert 2023-11-24 14:23:47 +01:00
Simon Rieger
89ed313df8 README.md aktualisiert 2023-11-24 14:21:55 +01:00
Simon Rieger
ca802d9f7b Dateien nach "/" hochladen 2023-11-24 14:19:39 +01:00
Simon Rieger
d061a7e2b0 Owntracks-1700830419043.json gelöscht 2023-11-24 14:19:30 +01:00
Simon Rieger
5fb3556686 README.md aktualisiert 2023-11-24 14:03:49 +01:00
Simon Rieger
7679b06276 README.md aktualisiert 2023-11-24 13:57:21 +01:00
Simon Rieger
62cbd281b0 Dateien nach "/" hochladen 2023-11-24 13:54:03 +01:00
e2e2ad1822 replace query with header 2023-11-24 11:26:12 +01:00
2a4e2c86f6 First Commit 2023-11-23 15:42:07 +01:00
11 changed files with 640 additions and 2 deletions

8
.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/go-location-logger.iml" filepath="$PROJECT_DIR$/.idea/go-location-logger.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

22
Dockerfile Normal file
View file

@ -0,0 +1,22 @@
# Use an official Golang runtime as a parent image
FROM golang:1.21.4
# Set the working directory in the container
WORKDIR /go/src/app
# Copy the local package files to the container's workspace
COPY ../../Downloads .
# Download and install any required third-party dependencies into the container.
#RUN go get -u github.com/gorilla/mux
RUN go get -u github.com/go-sql-driver/mysql
RUN go get -u github.com/sirupsen/logrus
# Build the Go application
RUN go build -o main .
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the application with environment variables
CMD ["./main"]

133
README.md
View file

@ -1,2 +1,133 @@
# go-location-logger
# Go Location Logger
A simple Go program that handles location data sent via HTTP POST requests and inserts it into a MySQL database.
## Features
- Handles JSON payloads containing location data.
- Inserts location data into a MySQL database.
- Utilizes headers `X-Limit-U` and `X-Limit-D` for specifying user and device information in HTTP requests.
## Prerequisites
Before running the program, ensure you have the following:
- Go installed: [https://golang.org/doc/install](https://golang.org/doc/install)
- Docker and Docker Compose installed: [https://docs.docker.com/get-docker/](https://docs.docker.com/get-docker/)
## Installation
1. Clone the repository:
```bash
git clone https://code.brothertec.eu/simono41/go-location-logger.git
```
2. Navigate to the project directory:
```bash
cd go-location-logger
```
3. Install dependencies:
```bash
go get -u github.com/sirupsen/logrus
```
4. Create an `.env` file in the project root with your environment variables:
```plaintext
# .env
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_HOST=your_db_host
DB_PORT=your_db_port
DB_NAME=your_db_name
```
5. Create Database Table
```
CREATE TABLE `locations` (
`dt` timestamp NULL DEFAULT NULL,
`tid` char(2) DEFAULT NULL,
`lat` decimal(9,6) DEFAULT NULL,
`lon` decimal(9,6) DEFAULT NULL,
`batt` int(11) DEFAULT NULL,
`vac` int(11) DEFAULT NULL,
`device` varchar(255) DEFAULT NULL,
`user` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
```
## Usage
### Running with Docker Compose
1. Use Docker Compose to start the services:
```bash
docker-compose up -d
```
2. Access your Go application at `http://localhost:8080`. Make HTTP POST requests as described in the README.md.
3. Import the Grafana Dashboard from `dashboard.json`. Use the Grafana UI to import the dashboard and visualize your data.
4. To stop the services, run:
```bash
docker-compose down
```
### Running without Docker
If you prefer not to use Docker, you can run the Go application directly using:
```bash
go run main.go
```
The server will start on `http://localhost:8080`.
Make a POST request with a JSON payload to `http://localhost:8080/?device=your_device&user=your_user`. The JSON payload should include location data.
Example:
```bash
curl -X POST -H "Content-Type: application/json" \
-H "X-Limit-U: your_user" \
-H "X-Limit-D: your_device" \
-H "Authorization: Basic your_token" \
-d '{
"_type": "location",
"tst": 1700820453,
"lat": 37.7749,
"lon": -122.4194,
"alt": 90,
"batt": 94,
"acc": 35,
"bs": 2,
"p": 99.314,
"created_at": 1700820457,
"BSSID": "e8:48:b8:7f:b4:d4",
"SSID": "Mr.Puhu",
"vac": 20,
"tag": "Arbeit",
"topic": "owntracks/simono41/6193B679-AD67-4B93-9DF2-158501A055AF",
"conn": "w",
"m": 1,
"tid": "AF"
}' \
http://localhost:8080/
```
## Contributing
Feel free to open issues or submit pull requests.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

283
dashboard.json Normal file
View file

@ -0,0 +1,283 @@
{
"__inputs": [
{
"name": "DS_MYSQL_- OWNTRACKS",
"label": "MySQL - owntracks",
"description": "",
"type": "datasource",
"pluginId": "mysql",
"pluginName": "MySQL"
}
],
"__elements": {},
"__requires": [
{
"type": "panel",
"id": "geomap",
"name": "Geomap",
"version": ""
},
{
"type": "grafana",
"id": "grafana",
"name": "Grafana",
"version": "10.2.2"
},
{
"type": "datasource",
"id": "mysql",
"name": "MySQL",
"version": "1.0.0"
}
],
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "grafana",
"uid": "-- Grafana --"
},
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": null,
"links": [],
"liveNow": false,
"panels": [
{
"datasource": {
"type": "mysql",
"uid": "${DS_MYSQL_- OWNTRACKS}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"custom": {
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
}
},
"mappings": [],
"thresholds": {
"mode": "percentage",
"steps": [
{
"color": "red",
"value": null
},
{
"color": "orange",
"value": 30
},
{
"color": "yellow",
"value": 50
},
{
"color": "green",
"value": 70
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 20,
"w": 24,
"x": 0,
"y": 0
},
"id": 1,
"options": {
"basemap": {
"config": {},
"name": "Layer 0",
"type": "default"
},
"controls": {
"mouseWheelZoom": true,
"showAttribution": true,
"showDebug": false,
"showMeasure": false,
"showScale": false,
"showZoom": true
},
"layers": [
{
"config": {
"arrow": 1,
"style": {
"color": {
"field": "batt",
"fixed": "dark-green"
},
"lineWidth": 2,
"opacity": 1,
"rotation": {
"fixed": 0,
"max": 360,
"min": -360,
"mode": "mod"
},
"size": {
"fixed": 5,
"max": 15,
"min": 2
},
"symbol": {
"fixed": "img/icons/marker/circle.svg",
"mode": "fixed"
},
"symbolAlign": {
"horizontal": "center",
"vertical": "center"
},
"text": {
"fixed": "",
"mode": "field"
},
"textConfig": {
"fontSize": 12000,
"offsetX": 0,
"offsetY": 0,
"textAlign": "left",
"textBaseline": "top"
}
}
},
"filterData": {
"id": "byRefId",
"options": "A"
},
"name": "Akku",
"opacity": 1,
"tooltip": true,
"type": "route"
}
],
"tooltip": {
"mode": "details"
},
"view": {
"allLayers": true,
"id": "fit",
"lat": 52.2013,
"lon": 10.114725,
"zoom": 15
}
},
"pluginVersion": "10.2.2",
"targets": [
{
"dataset": "owntracks",
"datasource": {
"type": "mysql",
"uid": "${DS_MYSQL_- OWNTRACKS}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
"rawSql": "SELECT\n dt AS \"time\",\n lat,\n lon,\n batt\nFROM owntracks.locations\nWHERE\n $__timeFilter(dt) AND\n user = $user AND\n device = $device\nORDER BY dt",
"refId": "A",
"sql": {
"columns": [
{
"parameters": [
{
"name": "lat",
"type": "functionParameter"
}
],
"type": "function"
}
],
"groupBy": [
{
"property": {
"type": "string"
},
"type": "groupBy"
}
],
"limit": 50
},
"table": "recordings"
}
],
"title": "Geräte",
"type": "geomap"
}
],
"refresh": "5s",
"schemaVersion": 38,
"tags": [],
"templating": {
"list": [
{
"current": {},
"datasource": {
"type": "mysql",
"uid": "${DS_MYSQL_- OWNTRACKS}"
},
"definition": "SELECT\n device\nFROM\n owntracks.locations",
"hide": 0,
"includeAll": false,
"label": "Gerät",
"multi": true,
"name": "device",
"options": [],
"query": "SELECT\n device\nFROM\n owntracks.locations",
"refresh": 1,
"regex": "",
"skipUrlSync": false,
"sort": 1,
"type": "query"
},
{
"current": {},
"datasource": {
"type": "mysql",
"uid": "${DS_MYSQL_- OWNTRACKS}"
},
"definition": "SELECT\n user\nFROM\n owntracks.locations",
"description": "",
"hide": 0,
"includeAll": false,
"label": "Benutzer",
"multi": true,
"name": "user",
"options": [],
"query": "SELECT\n user\nFROM\n owntracks.locations",
"refresh": 1,
"regex": "",
"skipUrlSync": false,
"sort": 1,
"type": "query"
}
]
},
"time": {
"from": "now-24h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "Owntracks",
"uid": "dbb80681-ef8e-4ce9-a47c-3c1fb432f4e9",
"version": 16,
"weekStart": ""
}

34
docker-compose.yml Normal file
View file

@ -0,0 +1,34 @@
version: "3.9"
services:
# Go application service
go-app:
build:
context: go/.
args:
- GO111MODULE=on
ports:
- "8080:8080"
depends_on:
- db
environment:
- DB_HOST=db
- DB_PORT=3306
- DB_USER=root
- DB_PASSWORD=owntracks
- DB_NAME=owntracks
restart: always
networks:
default:
db:
image: mariadb
volumes:
- ./mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: owntracks
MYSQL_DATABASE: owntracks
networks:
default:

10
go.mod Normal file
View file

@ -0,0 +1,10 @@
module go-app
go 1.21.4
require (
github.com/go-sql-driver/mysql v1.7.1
github.com/sirupsen/logrus v1.9.3
)
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

17
go.sum Normal file
View file

@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

110
main.go Normal file
View file

@ -0,0 +1,110 @@
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/sirupsen/logrus"
)
var log = logrus.New()
// Location represents the JSON payload structure
type Location struct {
Type string `json:"_type"`
Tst int64 `json:"tst"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Tid string `json:"tid"`
Batt int `json:"batt"`
Vac int `json:"vac"`
}
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&logrus.JSONFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(logrus.InfoLevel)
}
func main() {
log.Info("Server started. Listening on :8080")
http.HandleFunc("/", handleRequest)
http.ListenAndServe(":8080", nil)
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
log.Info("Handling request")
if r.Method != http.MethodPost {
log.Warn("Method not allowed")
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Extract values from headers
device := r.Header.Get("X-Limit-D")
user := r.Header.Get("X-Limit-U")
log.Infof("Received request with device=%s and user=%s", device, user)
var loc Location
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&loc)
if err != nil {
log.Error("Invalid JSON payload: ", err)
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
log.Infof("Received JSON payload: %+v", loc)
if loc.Type == "location" {
db, err := sql.Open("mysql", getDBConnectionString())
if err != nil {
log.Error("Database connection error: ", err)
http.Error(w, fmt.Sprintf("Database connection error: %v", err), http.StatusInternalServerError)
return
}
defer db.Close()
dt := time.Unix(loc.Tst, 0).Format("2006-01-02 15:04:05")
_, err = db.Exec("INSERT INTO locations (dt, tid, lat, lon, batt, vac, device, user) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
dt, loc.Tid, loc.Lat, loc.Lon, loc.Batt, loc.Vac, device, user)
if err != nil {
log.Error("Database insertion error: ", err)
http.Error(w, fmt.Sprintf("Database insertion error: %v", err), http.StatusInternalServerError)
return
}
log.Info("Location data inserted into the database")
}
response := make(map[string]interface{})
// Optionally add objects to return to the app (e.g., friends or cards)
json.NewEncoder(w).Encode(response)
log.Info("Request handled successfully")
}
func getDBConnectionString() string {
user := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
host := os.Getenv("DB_HOST")
port := os.Getenv("DB_PORT")
dbName := os.Getenv("DB_NAME")
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, password, host, port, dbName)
}