Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
Golang MongoDB using gorilla mux
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Strakhov Egor
Golang MongoDB using gorilla mux
Commits
c640f803
Commit
c640f803
authored
Sep 16, 2021
by
Strakhov Egor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
main_test.go: coverage: 58.1%
parent
13c1c0d1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
106 additions
and
11 deletions
+106
-11
README.md
README.md
+1
-1
go.mod
go.mod
+2
-2
main.go
main.go
+18
-4
main_test.go
main_test.go
+81
-0
test.json
test.json
+4
-4
No files found.
README.md
View file @
c640f803
...
...
@@ -37,7 +37,7 @@ ID and Timestamp will be generated automatically
Run the application:
```
bash
go run
test
.go
go run
main
.go
```
Push json:
...
...
go.mod
View file @
c640f803
...
...
@@ -3,6 +3,6 @@ module github.com/mongodb/mongo-go-driver/bson
go 1.13
require (
github.com/gorilla/mux v1.8.0
// indirect
go.mongodb.org/mongo-driver v1.7.2
// indirect
github.com/gorilla/mux v1.8.0
go.mongodb.org/mongo-driver v1.7.2
)
test
.go
→
main
.go
View file @
c640f803
...
...
@@ -22,13 +22,19 @@ type Kick struct {
Longitude
float64
`json:"longitude,omitempty" bson:"longitude,omitempty"`
Attitude
float64
`json:"attitude,omitempty" bson:"attitude,omitempty"`
Velocity
float64
`json:"velocity,omitempty" bson:"velocity,omitempty"`
}
func
CreateKick
(
response
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
response
.
Header
()
.
Set
(
"content-type"
,
"application/json"
)
var
kick
Kick
_
=
json
.
NewDecoder
(
request
.
Body
)
.
Decode
(
&
kick
)
err
:=
json
.
NewDecoder
(
request
.
Body
)
.
Decode
(
&
kick
)
if
err
!=
nil
{
response
.
WriteHeader
(
http
.
StatusBadRequest
)
return
}
collection
:=
client
.
Database
(
"mongodb"
)
.
Collection
(
"kicks"
)
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
5
*
time
.
Second
)
...
...
@@ -36,8 +42,16 @@ func CreateKick(response http.ResponseWriter, request *http.Request) {
kick
.
Timestamp
=
now
.
Format
(
"2006-01-02 15:04:05"
)
result
,
_
:=
collection
.
InsertOne
(
ctx
,
kick
)
json
.
NewEncoder
(
response
)
.
Encode
(
result
)
result
,
err
:=
collection
.
InsertOne
(
ctx
,
kick
)
if
err
!=
nil
{
response
.
WriteHeader
(
http
.
StatusNotAcceptable
)
}
err
=
json
.
NewEncoder
(
response
)
.
Encode
(
result
)
if
err
!=
nil
{
response
.
WriteHeader
(
http
.
StatusConflict
)
}
}
func
GetKicks
(
response
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
...
...
main_test.go
0 → 100644
View file @
c640f803
package
main
import
(
"os"
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo/options"
"net/http"
"go.mongodb.org/mongo-driver/mongo"
"github.com/gorilla/mux"
"testing"
)
func
connect
(){
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
clientOptions
:=
options
.
Client
()
.
ApplyURI
(
"mongodb://localhost:27017"
)
client
,
_
=
mongo
.
Connect
(
ctx
,
clientOptions
)
err
:=
client
.
Ping
(
context
.
TODO
(),
nil
)
if
err
!=
nil
{
log
.
Fatalf
(
"MongoDB connection error: %v"
,
err
)
}
else
{
log
.
Printf
(
"Connected!"
)
}
}
func
sendJson
()
{
r
,
err
:=
os
.
Open
(
"test.json"
)
resp
,
err
:=
http
.
Post
(
"http://localhost:8080/kick"
,
"application/json"
,
r
)
if
err
!=
nil
{
log
.
Fatalf
(
"Can not Post json"
)
}
if
resp
.
StatusCode
==
400
{
log
.
Fatalf
(
"Bad json: %v
\n
"
,
r
)
}
if
resp
.
StatusCode
==
406
{
log
.
Fatalf
(
"Cannot Insert document"
)
}
if
resp
.
StatusCode
==
409
{
log
.
Fatalf
(
"Cannot Encode json"
)
}
}
func
getJsons
()
{
resp
,
err
:=
http
.
Get
(
"http://localhost:8080/kicks"
)
if
err
!=
nil
{
log
.
Fatalf
(
"Cannot Get jsons"
)
}
if
resp
.
StatusCode
!=
200
{
log
.
Printf
(
resp
.
Status
)
}
}
func
listen
(
finished
chan
bool
)
{
router
:=
mux
.
NewRouter
()
router
.
HandleFunc
(
"/kick"
,
CreateKick
)
.
Methods
(
"POST"
)
router
.
HandleFunc
(
"/kicks"
,
GetKicks
)
.
Methods
(
"GET"
)
finished
<-
true
http
.
ListenAndServe
(
":8080"
,
router
)
}
func
TestMain
(
t
*
testing
.
T
)
{
finished
:=
make
(
chan
bool
)
go
listen
(
finished
)
<-
finished
connect
()
sendJson
()
getJsons
()
}
test.json
View file @
c640f803
{
"company"
:
"
Sirius
"
,
"longitude"
:
1
2345
,
"attitude"
:
346
,
"velocity"
:
1
"company"
:
"
company
"
,
"longitude"
:
1
,
"attitude"
:
2
,
"velocity"
:
3
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment