update libraries and remove logrus

This commit is contained in:
Simon Rieger 2025-03-26 22:47:15 +00:00
parent 3a043d2b15
commit 1249ad3f35
5 changed files with 43 additions and 68 deletions

View file

@ -1,5 +1,3 @@
version: "3.9"
services:
# Go application service
@ -8,12 +6,12 @@ services:
context: go/.
args:
- GO111MODULE=on
#ports:
# - "8080:8080"
ports:
- "10.0.1.3:8096:8080"
depends_on:
- db
environment:
- DB_HOST=172.28.0.25
- DB_HOST=10.2.0.30
- DB_PORT=3306
- DB_USER=root
- DB_PASSWORD=owntracks
@ -25,8 +23,8 @@ services:
networks:
default:
dns:
ipv4_address: 172.28.0.24
monitoring:
ipv4_address: 10.2.0.29
db:
image: mariadb:11
@ -38,10 +36,9 @@ services:
restart: always
networks:
default:
dns:
ipv4_address: 172.28.0.25
monitoring:
ipv4_address: 10.2.0.30
networks:
dns:
name: dns
monitoring:
external: true

View file

@ -1,22 +1,28 @@
# Use an official Golang runtime as a parent image
FROM golang:1.21.4
# syntax=docker/dockerfile:1
# Set the working directory in the container
WORKDIR /go/src/app
# Build the application from source
FROM golang:1.21.4 AS build-stage
# Copy the local package files to the container's workspace
COPY . .
WORKDIR /app
# 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
COPY * ./
RUN go mod download
# Build the Go application
RUN go build -o main .
RUN CGO_ENABLED=0 GOOS=linux go build -o /main
# Run the tests in the container
FROM build-stage AS run-test-stage
RUN go test -v ./...
# Deploy the application binary into a lean image
FROM gcr.io/distroless/base-debian11 AS build-release-stage
WORKDIR /
COPY --from=build-stage /main /main
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the application with environment variables
CMD ["./main"]
USER nonroot:nonroot
ENTRYPOINT ["/main"]

View file

@ -2,9 +2,4 @@ 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
require github.com/go-sql-driver/mysql v1.7.1

View file

@ -1,17 +1,2 @@
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=

View file

@ -4,16 +4,14 @@ import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/sirupsen/logrus"
_ "github.com/go-sql-driver/mysql"
)
var log = logrus.New()
// Location represents the JSON payload structure
type Location struct {
Type string `json:"_type"`
@ -26,29 +24,23 @@ type Location struct {
}
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
// Konfigurieren Sie den Standard-Logger
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(logrus.InfoLevel)
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
}
func main() {
log.Info("Server started. Listening on :8080")
log.Println("Server started. Listening on :8080")
http.HandleFunc("/", handleRequest)
http.ListenAndServe(":8080", nil)
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
log.Info("Handling request")
log.Println("Handling request")
if r.Method != http.MethodPost {
log.Warn("Method not allowed")
log.Println("Method not allowed")
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
@ -57,23 +49,23 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
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)
log.Printf("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)
log.Printf("Invalid JSON payload: %v", err)
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
log.Infof("Received JSON payload: %+v", loc)
log.Printf("Received JSON payload: %+v", loc)
if loc.Type == "location" {
db, err := sql.Open("mysql", getDBConnectionString())
if err != nil {
log.Error("Database connection error: ", err)
log.Printf("Database connection error: %v", err)
http.Error(w, fmt.Sprintf("Database connection error: %v", err), http.StatusInternalServerError)
return
}
@ -84,19 +76,19 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
_, 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)
log.Printf("Database insertion error: %v", err)
http.Error(w, fmt.Sprintf("Database insertion error: %v", err), http.StatusInternalServerError)
return
}
log.Info("Location data inserted into the database")
log.Println("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")
log.Println("Request handled successfully")
}
func getDBConnectionString() string {