Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Gin framework in place of net/http #17

Merged
merged 5 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
39 changes: 39 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
84 changes: 48 additions & 36 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"runtime"
"strconv"

"github.com/gin-gonic/gin"

"github.com/asdine/storm"
)

Expand All @@ -18,16 +20,21 @@ 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)

ginEngine := gin.Default() // Creates a gin router with default middleware

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)

})

http.HandleFunc("/retrieve", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Server", serverIdentifier)
w.Header().Add("Content-Type", "application/json")
ginEngine.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())
Expand All @@ -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)
ginEngine.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 {
Expand All @@ -74,29 +83,31 @@ 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.
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(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'
Expand All @@ -114,12 +125,13 @@ 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)

err := http.ListenAndServe(listenAddr, nil)
err := http.ListenAndServe(listenAddr, ginEngine)
if err != nil {
log.Fatal(err)
}
Expand Down