update libraries and remove logrus
This commit is contained in:
parent
3a043d2b15
commit
1249ad3f35
5 changed files with 43 additions and 68 deletions
|
@ -1,5 +1,3 @@
|
||||||
version: "3.9"
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
|
||||||
# Go application service
|
# Go application service
|
||||||
|
@ -8,12 +6,12 @@ services:
|
||||||
context: go/.
|
context: go/.
|
||||||
args:
|
args:
|
||||||
- GO111MODULE=on
|
- GO111MODULE=on
|
||||||
#ports:
|
ports:
|
||||||
# - "8080:8080"
|
- "10.0.1.3:8096:8080"
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
- db
|
||||||
environment:
|
environment:
|
||||||
- DB_HOST=172.28.0.25
|
- DB_HOST=10.2.0.30
|
||||||
- DB_PORT=3306
|
- DB_PORT=3306
|
||||||
- DB_USER=root
|
- DB_USER=root
|
||||||
- DB_PASSWORD=owntracks
|
- DB_PASSWORD=owntracks
|
||||||
|
@ -25,8 +23,8 @@ services:
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
default:
|
default:
|
||||||
dns:
|
monitoring:
|
||||||
ipv4_address: 172.28.0.24
|
ipv4_address: 10.2.0.29
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: mariadb:11
|
image: mariadb:11
|
||||||
|
@ -38,10 +36,9 @@ services:
|
||||||
restart: always
|
restart: always
|
||||||
networks:
|
networks:
|
||||||
default:
|
default:
|
||||||
dns:
|
monitoring:
|
||||||
ipv4_address: 172.28.0.25
|
ipv4_address: 10.2.0.30
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
dns:
|
monitoring:
|
||||||
name: dns
|
|
||||||
external: true
|
external: true
|
||||||
|
|
|
@ -1,22 +1,28 @@
|
||||||
# Use an official Golang runtime as a parent image
|
# syntax=docker/dockerfile:1
|
||||||
FROM golang:1.21.4
|
|
||||||
|
|
||||||
# Set the working directory in the container
|
# Build the application from source
|
||||||
WORKDIR /go/src/app
|
FROM golang:1.21.4 AS build-stage
|
||||||
|
|
||||||
# Copy the local package files to the container's workspace
|
WORKDIR /app
|
||||||
COPY . .
|
|
||||||
|
|
||||||
# Download and install any required third-party dependencies into the container.
|
COPY * ./
|
||||||
#RUN go get -u github.com/gorilla/mux
|
RUN go mod download
|
||||||
RUN go get -u github.com/go-sql-driver/mysql
|
|
||||||
RUN go get -u github.com/sirupsen/logrus
|
|
||||||
|
|
||||||
# Build the Go application
|
RUN CGO_ENABLED=0 GOOS=linux go build -o /main
|
||||||
RUN 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
|
EXPOSE 8080
|
||||||
|
|
||||||
# Command to run the application with environment variables
|
USER nonroot:nonroot
|
||||||
CMD ["./main"]
|
|
||||||
|
ENTRYPOINT ["/main"]
|
||||||
|
|
|
@ -2,9 +2,4 @@ module go-app
|
||||||
|
|
||||||
go 1.21.4
|
go 1.21.4
|
||||||
|
|
||||||
require (
|
require github.com/go-sql-driver/mysql v1.7.1
|
||||||
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
|
|
||||||
|
|
15
go/go.sum
15
go/go.sum
|
@ -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 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
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=
|
|
||||||
|
|
34
go/main.go
34
go/main.go
|
@ -4,16 +4,14 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = logrus.New()
|
|
||||||
|
|
||||||
// Location represents the JSON payload structure
|
// Location represents the JSON payload structure
|
||||||
type Location struct {
|
type Location struct {
|
||||||
Type string `json:"_type"`
|
Type string `json:"_type"`
|
||||||
|
@ -26,29 +24,23 @@ type Location struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Log as JSON instead of the default ASCII formatter.
|
// Konfigurieren Sie den Standard-Logger
|
||||||
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)
|
log.SetOutput(os.Stdout)
|
||||||
|
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
|
||||||
// Only log the warning severity or above.
|
|
||||||
log.SetLevel(logrus.InfoLevel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Info("Server started. Listening on :8080")
|
log.Println("Server started. Listening on :8080")
|
||||||
|
|
||||||
http.HandleFunc("/", handleRequest)
|
http.HandleFunc("/", handleRequest)
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Info("Handling request")
|
log.Println("Handling request")
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
log.Warn("Method not allowed")
|
log.Println("Method not allowed")
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -57,23 +49,23 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
device := r.Header.Get("X-Limit-D")
|
device := r.Header.Get("X-Limit-D")
|
||||||
user := r.Header.Get("X-Limit-U")
|
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
|
var loc Location
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
err := decoder.Decode(&loc)
|
err := decoder.Decode(&loc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Invalid JSON payload: ", err)
|
log.Printf("Invalid JSON payload: %v", err)
|
||||||
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
|
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Received JSON payload: %+v", loc)
|
log.Printf("Received JSON payload: %+v", loc)
|
||||||
|
|
||||||
if loc.Type == "location" {
|
if loc.Type == "location" {
|
||||||
db, err := sql.Open("mysql", getDBConnectionString())
|
db, err := sql.Open("mysql", getDBConnectionString())
|
||||||
if err != nil {
|
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)
|
http.Error(w, fmt.Sprintf("Database connection error: %v", err), http.StatusInternalServerError)
|
||||||
return
|
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 (?, ?, ?, ?, ?, ?, ?, ?)",
|
_, 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)
|
dt, loc.Tid, loc.Lat, loc.Lon, loc.Batt, loc.Vac, device, user)
|
||||||
if err != nil {
|
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)
|
http.Error(w, fmt.Sprintf("Database insertion error: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Location data inserted into the database")
|
log.Println("Location data inserted into the database")
|
||||||
}
|
}
|
||||||
|
|
||||||
response := make(map[string]interface{})
|
response := make(map[string]interface{})
|
||||||
// Optionally add objects to return to the app (e.g., friends or cards)
|
// Optionally add objects to return to the app (e.g., friends or cards)
|
||||||
json.NewEncoder(w).Encode(response)
|
json.NewEncoder(w).Encode(response)
|
||||||
|
|
||||||
log.Info("Request handled successfully")
|
log.Println("Request handled successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDBConnectionString() string {
|
func getDBConnectionString() string {
|
||||||
|
|
Loading…
Add table
Reference in a new issue