From ebfe56905f8cf20a92c51e5aa77d667f9b45a87b Mon Sep 17 00:00:00 2001 From: Sultron Date: Wed, 7 Oct 2020 00:35:50 -0700 Subject: [PATCH 1/5] Added Gin framework in place of net/http #14 --- internal/server/server.go | 88 +++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index d6eac50..a19aa3e 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -3,6 +3,7 @@ package server import ( "encoding/json" "fmt" + "github.com/gin-gonic/gin" "go-osmand-tracker/internal/types" "log" "net/http" @@ -18,16 +19,22 @@ var ( // Listen spins up a webserver and listens for incoming connections func Listen(port uint, db *storm.DB) { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Server", serverIdentifier) - w.Header().Add("Content-Type", "text/plain") - w.WriteHeader(501) // Not implemented - fmt.Fprintf(w, "Sorry, %s is not implemented.", r.RequestURI) + + var request = gin.Default() // Creates a gin router with default middleware + + request.GET("/", func(c *gin.Context) { + c.Header("Server", serverIdentifier) + c.Header("Content-Type", "text/plain") + c.Writer.WriteHeader(501) + c.String(http.StatusNotImplemented, "Sorry, %s is not implemented.", c.Request.RequestURI) + + }) - http.HandleFunc("/retrieve", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Server", serverIdentifier) - w.Header().Add("Content-Type", "application/json") + request.GET("/retrieve", func(c *gin.Context) { + + c.Header("Server", serverIdentifier) + c.Header("Content-Type", "application/json") var entry []types.Entry db.All(&entry, storm.Limit(1), storm.Reverse()) @@ -36,30 +43,32 @@ func Listen(port uint, db *storm.DB) { if len(entry) == 0 { // There is no data. Let's return HTTP Status 204: No Content - w.WriteHeader(204) // The server successfully processed the request, but is not returning any content. + c.Writer.WriteHeader(204) // The server successfully processed the request, but is not returning any content. } else { // There is data. Let's process it. processedEntry, err := json.Marshal(entry[0]) if err != nil { - w.WriteHeader(500) // HTTP 500 Internal Server Error + c.Writer.WriteHeader(500) // HTTP 500 Internal Server Error log.Println("Error retrieving last database entry", err) } else { - w.WriteHeader(200) // HTTP 200 OK + c.Writer.WriteHeader(200) // HTTP 200 OK responseData = processedEntry } } - w.Write(responseData) + c.String(http.StatusOK, string(responseData)) + }) - http.HandleFunc("/retrieve/multi", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Server", serverIdentifier) - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(200) + request.GET("/retrieve/multi", func(c *gin.Context) { + c.Header("Server", serverIdentifier) + c.Header("Content-Type", "application/json") + c.Writer.WriteHeader(200) var cnt uint16 = 10 - if len(r.URL.Query().Get("count")) > 0 { - parsedCountValue, err := strconv.ParseUint(r.URL.Query().Get("count"), 10, 16) + c.Request.URL.Query().Get("count") + if len(c.Request.URL.Query().Get("count")) > 0 { + parsedCountValue, err := strconv.ParseUint(c.Request.URL.Query().Get("count"), 10, 16) if err != nil { log.Println("Error parsing query parameter 'count'", err) } else { @@ -74,29 +83,34 @@ func Listen(port uint, db *storm.DB) { if err != nil { log.Fatal("Processing entries from database failed", err) } - w.Write(responseData) + + c.String(http.StatusOK, string(responseData)) + + + }) - http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Server", serverIdentifier) - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(204) // The server successfully processed the request, and is not returning any content. + request.GET("/submit", func(c *gin.Context) { + c.Header("Server", serverIdentifier) + c.Header("Content-Type", "application/json") + c.Writer.WriteHeader(204) // The server successfully processed the request, and is not returning any content. + // TODO: Error handling - retrievedLatitude, _ := strconv.ParseFloat(r.URL.Query().Get("lat"), 64) - retrievedLongitude, _ := strconv.ParseFloat(r.URL.Query().Get("lon"), 64) - retrievedTimestamp, _ := strconv.ParseUint(r.URL.Query().Get("timestamp"), 10, 64) - retrievedHdop, _ := strconv.ParseFloat(r.URL.Query().Get("hdop"), 64) - retrievedAltitude, _ := strconv.ParseFloat(r.URL.Query().Get("altitude"), 64) - retrievedSpeed, _ := strconv.ParseFloat(r.URL.Query().Get("speed"), 64) + retrievedLatitude, _ := strconv.ParseFloat(c.Request.URL.Query().Get("lat"), 64) + retrievedLongitude, _ := strconv.ParseFloat(c.Request.URL.Query().Get("lon"), 64) + retrievedTimestamp, _ := strconv.ParseUint(c.Request.URL.Query().Get("timestamp"), 10, 64) + retrievedHdop, _ := strconv.ParseFloat(c.Request.URL.Query().Get("hdop"), 64) + retrievedAltitude, _ := strconv.ParseFloat(c.Request.URL.Query().Get("altitude"), 64) + retrievedSpeed, _ := strconv.ParseFloat(c.Request.URL.Query().Get("speed"), 64) locationUpdate := types.LocationUpdate{ - retrievedLatitude, - retrievedLongitude, - retrievedTimestamp, - retrievedHdop, - retrievedAltitude, - retrievedSpeed, + Latitude: retrievedLatitude, + Longitude: retrievedLongitude, + Timestamp: retrievedTimestamp, + Hdop: retrievedHdop, + Altitude: retrievedAltitude, + Speed: retrievedSpeed, } // Checks if the data (in 'locationUpdate') conforms to the types of the struct 'LocationUpdate' @@ -114,8 +128,12 @@ func Listen(port uint, db *storm.DB) { if err != nil { log.Fatal("Saving entry to database failed ", err) } + + }) + + var listenAddr string = fmt.Sprintf(":%d", port) fmt.Printf("Starting server at port: %v\n", port) From f56aba682485fb530c28d3be1eef12104b02df4b Mon Sep 17 00:00:00 2001 From: Ricardo Balk <14904229+ricardobalk@users.noreply.github.com> Date: Wed, 7 Oct 2020 17:43:14 +0200 Subject: [PATCH 2/5] Added Gin framework to Go modules files --- go.mod | 1 + go.sum | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/go.mod b/go.mod index cfa7d9e..1a79f02 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.14 require ( github.com/asdine/storm v2.1.2+incompatible + github.com/gin-gonic/gin v1.6.3 go.etcd.io/bbolt v1.3.5 // indirect ) diff --git a/go.sum b/go.sum index 22489a3..5929c17 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,45 @@ github.com/asdine/storm v1.1.0 h1:lwDLqMMPhokfYk8EuU1RRHTi54T68EI+QnCqK5t4TCM= github.com/asdine/storm v2.1.2+incompatible h1:dczuIkyqwY2LrtXPz8ixMrU/OFgZp71kbKTHGrXYt/Q= github.com/asdine/storm v2.1.2+incompatible/go.mod h1:RarYDc9hq1UPLImuiXK3BIWPJLdIygvV3PsInK0FbVQ= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= +github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= +github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0= go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= From 68f546ebbb03b714747c2ef08b7ea939fed699da Mon Sep 17 00:00:00 2001 From: Ricardo Balk <14904229+ricardobalk@users.noreply.github.com> Date: Wed, 7 Oct 2020 17:44:38 +0200 Subject: [PATCH 3/5] Moved external dependency import --- internal/server/server.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/server/server.go b/internal/server/server.go index a19aa3e..21f6945 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -3,13 +3,14 @@ package server import ( "encoding/json" "fmt" - "github.com/gin-gonic/gin" "go-osmand-tracker/internal/types" "log" "net/http" "runtime" "strconv" + "github.com/gin-gonic/gin" + "github.com/asdine/storm" ) From 077eb1dd142838c2efb7a24a5c64572e8052eaf5 Mon Sep 17 00:00:00 2001 From: Ricardo Balk <14904229+ricardobalk@users.noreply.github.com> Date: Wed, 7 Oct 2020 17:46:25 +0200 Subject: [PATCH 4/5] Renamed `request` to `ginEngine` The var basically refers to the Gin engine --- internal/server/server.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index 21f6945..82f3246 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -21,18 +21,17 @@ var ( // Listen spins up a webserver and listens for incoming connections func Listen(port uint, db *storm.DB) { - var request = gin.Default() // Creates a gin router with default middleware + ginEngine := gin.Default() // Creates a gin router with default middleware - request.GET("/", func(c *gin.Context) { + ginEngine.GET("/", func(c *gin.Context) { c.Header("Server", serverIdentifier) c.Header("Content-Type", "text/plain") c.Writer.WriteHeader(501) c.String(http.StatusNotImplemented, "Sorry, %s is not implemented.", c.Request.RequestURI) - }) - request.GET("/retrieve", func(c *gin.Context) { + ginEngine.GET("/retrieve", func(c *gin.Context) { c.Header("Server", serverIdentifier) c.Header("Content-Type", "application/json") @@ -61,7 +60,7 @@ func Listen(port uint, db *storm.DB) { }) - request.GET("/retrieve/multi", func(c *gin.Context) { + ginEngine.GET("/retrieve/multi", func(c *gin.Context) { c.Header("Server", serverIdentifier) c.Header("Content-Type", "application/json") c.Writer.WriteHeader(200) @@ -87,16 +86,13 @@ func Listen(port uint, db *storm.DB) { c.String(http.StatusOK, string(responseData)) - - }) - request.GET("/submit", func(c *gin.Context) { + ginEngine.GET("/submit", func(c *gin.Context) { c.Header("Server", serverIdentifier) c.Header("Content-Type", "application/json") c.Writer.WriteHeader(204) // The server successfully processed the request, and is not returning any content. - // TODO: Error handling retrievedLatitude, _ := strconv.ParseFloat(c.Request.URL.Query().Get("lat"), 64) retrievedLongitude, _ := strconv.ParseFloat(c.Request.URL.Query().Get("lon"), 64) From 5e879ddfb581a539273ae6a184f6ea275a37ddf7 Mon Sep 17 00:00:00 2001 From: Ricardo Balk <14904229+ricardobalk@users.noreply.github.com> Date: Wed, 7 Oct 2020 17:47:03 +0200 Subject: [PATCH 5/5] Attached the Gin router to the actual HTTP Listen function Otherwise, it didn't work. But now it does! --- internal/server/server.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/server/server.go b/internal/server/server.go index 82f3246..1547e40 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -126,15 +126,12 @@ func Listen(port uint, db *storm.DB) { log.Fatal("Saving entry to database failed ", err) } - }) - - var listenAddr string = fmt.Sprintf(":%d", port) fmt.Printf("Starting server at port: %v\n", port) - err := http.ListenAndServe(listenAddr, nil) + err := http.ListenAndServe(listenAddr, ginEngine) if err != nil { log.Fatal(err) }