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
2bc68685
Commit
2bc68685
authored
Sep 17, 2021
by
Strakhov Egor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
coverage: 59.5% of statements
parent
af052a92
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
27 deletions
+69
-27
main.go
main.go
+1
-2
main_test.go
main_test.go
+67
-24
test.json
test.json
+1
-1
No files found.
main.go
View file @
2bc68685
...
@@ -32,6 +32,7 @@ func CreateKick(response http.ResponseWriter, request *http.Request) {
...
@@ -32,6 +32,7 @@ func CreateKick(response http.ResponseWriter, request *http.Request) {
if
err
!=
nil
{
if
err
!=
nil
{
response
.
WriteHeader
(
http
.
StatusBadRequest
)
response
.
WriteHeader
(
http
.
StatusBadRequest
)
fmt
.
Println
(
"Bad Json"
)
return
return
}
}
...
@@ -63,7 +64,6 @@ func GetKicks(response http.ResponseWriter, request *http.Request) {
...
@@ -63,7 +64,6 @@ func GetKicks(response http.ResponseWriter, request *http.Request) {
if
err
!=
nil
{
if
err
!=
nil
{
response
.
WriteHeader
(
http
.
StatusInternalServerError
)
response
.
WriteHeader
(
http
.
StatusInternalServerError
)
response
.
Write
([]
byte
(
`{ "message": "`
+
err
.
Error
()
+
`" }`
))
response
.
Write
([]
byte
(
`{ "message": "`
+
err
.
Error
()
+
`" }`
))
return
}
}
defer
cursor
.
Close
(
ctx
)
defer
cursor
.
Close
(
ctx
)
for
cursor
.
Next
(
ctx
)
{
for
cursor
.
Next
(
ctx
)
{
...
@@ -74,7 +74,6 @@ func GetKicks(response http.ResponseWriter, request *http.Request) {
...
@@ -74,7 +74,6 @@ func GetKicks(response http.ResponseWriter, request *http.Request) {
if
err
:=
cursor
.
Err
();
err
!=
nil
{
if
err
:=
cursor
.
Err
();
err
!=
nil
{
response
.
WriteHeader
(
http
.
StatusInternalServerError
)
response
.
WriteHeader
(
http
.
StatusInternalServerError
)
response
.
Write
([]
byte
(
`{ "message": "`
+
err
.
Error
()
+
`" }`
))
response
.
Write
([]
byte
(
`{ "message": "`
+
err
.
Error
()
+
`" }`
))
return
}
}
json
.
NewEncoder
(
response
)
.
Encode
(
kicks
)
json
.
NewEncoder
(
response
)
.
Encode
(
kicks
)
}
}
...
...
main_test.go
View file @
2bc68685
package
main
package
main
import
(
import
(
"io/ioutil"
"go.mongodb.org/mongo-driver/bson"
"encoding/json"
"os"
"os"
"context"
"context"
"log"
"log"
...
@@ -12,6 +15,24 @@ import (
...
@@ -12,6 +15,24 @@ import (
"testing"
"testing"
)
)
func
init
()
{
finished
:=
make
(
chan
bool
)
go
listen
(
finished
)
<-
finished
connect
()
}
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
connect
(){
func
connect
(){
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
clientOptions
:=
options
.
Client
()
.
ApplyURI
(
"mongodb://localhost:27017"
)
clientOptions
:=
options
.
Client
()
.
ApplyURI
(
"mongodb://localhost:27017"
)
...
@@ -20,31 +41,60 @@ func connect(){
...
@@ -20,31 +41,60 @@ func connect(){
err
:=
client
.
Ping
(
context
.
TODO
(),
nil
)
err
:=
client
.
Ping
(
context
.
TODO
(),
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"MongoDB connection error: %v"
,
err
)
log
.
Fatalf
(
"MongoDB connection error: %v"
,
err
)
}
else
{
log
.
Printf
(
"Connected!"
)
}
}
}
}
func
sendJson
()
{
func
sendJson
(
url
string
)
{
//reading initial struct
var
initial
Kick
file
,
_
:=
ioutil
.
ReadFile
(
"test.json"
)
_
=
json
.
Unmarshal
([]
byte
(
file
),
&
initial
)
log
.
Printf
(
"%v
\n
"
,
initial
)
//posting json
r
,
err
:=
os
.
Open
(
"test.json"
)
r
,
err
:=
os
.
Open
(
"test.json"
)
resp
,
err
:=
http
.
Post
(
"http://localhost:8080/kick"
,
"application/json"
,
r
)
resp
,
err
:=
http
.
Post
(
url
,
"application/json"
,
r
)
r
.
Close
()
//checking errors
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatal
f
(
"Can not Post json"
)
log
.
Print
f
(
"Can not Post json"
)
}
}
if
resp
.
StatusCode
==
400
{
if
resp
.
StatusCode
==
400
{
log
.
Fatal
f
(
"Bad json: %v
\n
"
,
r
)
log
.
Print
f
(
"Bad json: %v
\n
"
,
r
)
}
}
if
resp
.
StatusCode
==
406
{
if
resp
.
StatusCode
==
406
{
log
.
Fatal
f
(
"Cannot Insert document"
)
log
.
Print
f
(
"Cannot Insert document"
)
}
}
if
resp
.
StatusCode
==
409
{
if
resp
.
StatusCode
==
409
{
log
.
Fatal
f
(
"Cannot Encode json"
)
log
.
Print
f
(
"Cannot Encode json"
)
}
}
//reading last inserted document from BD
var
kick
Kick
collection
:=
client
.
Database
(
"mongodb"
)
.
Collection
(
"kicks"
)
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
30
*
time
.
Second
)
cursor
,
err
:=
collection
.
Find
(
ctx
,
bson
.
M
{})
defer
cursor
.
Close
(
ctx
)
for
cursor
.
Next
(
ctx
)
{
cursor
.
Decode
(
&
kick
)
}
//checking if the initial struct equals the readed one
if
(
kick
.
Company
!=
initial
.
Company
)
||
(
kick
.
Longitude
!=
initial
.
Longitude
)
||
(
kick
.
Attitude
!=
initial
.
Attitude
)
||
(
kick
.
Velocity
!=
initial
.
Velocity
)
{
log
.
Println
(
"Document Inserted Uncorrectly"
)
}
//delete testing document from BD
collection
.
DeleteOne
(
ctx
,
bson
.
M
{
"_id"
:
kick
.
ID
})
}
}
func
getJsons
()
{
func
getJsons
()
{
...
@@ -59,23 +109,16 @@ func getJsons() {
...
@@ -59,23 +109,16 @@ func getJsons() {
}
}
}
}
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
)
{
sendJson
(
"http://localhost:8080/kick"
)
getJsons
()
}
}
func
TestMain
(
t
*
testing
.
T
)
{
/*
finished
:=
make
(
chan
bool
)
func TestCreation(t *testing.T) {
go
listen
(
finished
)
sendJson("http://localhost:8080/kick"
)
<-
finished
}
connect
()
*/
sendJson
()
getJsons
()
}
test.json
View file @
2bc68685
{
{
"company"
:
"
company
"
,
"company"
:
"
Malchik
"
,
"longitude"
:
1
,
"longitude"
:
1
,
"attitude"
:
2
,
"attitude"
:
2
,
"velocity"
:
3
"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