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
073ddfbe
Commit
073ddfbe
authored
Sep 17, 2021
by
Strakhov Egor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Push Document Test
parent
2bc68685
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
0 additions
and
48 deletions
+0
-48
main.go
main.go
+0
-25
main_test.go
main_test.go
+0
-23
No files found.
main.go
View file @
073ddfbe
...
@@ -8,7 +8,6 @@ import (
...
@@ -8,7 +8,6 @@ import (
"time"
"time"
"github.com/gorilla/mux"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo"
)
)
...
@@ -55,29 +54,6 @@ func CreateKick(response http.ResponseWriter, request *http.Request) {
...
@@ -55,29 +54,6 @@ func CreateKick(response http.ResponseWriter, request *http.Request) {
}
}
}
}
func
GetKicks
(
response
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
response
.
Header
()
.
Set
(
"content-type"
,
"application/json"
)
var
kicks
[]
Kick
collection
:=
client
.
Database
(
"mongodb"
)
.
Collection
(
"kicks"
)
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
30
*
time
.
Second
)
cursor
,
err
:=
collection
.
Find
(
ctx
,
bson
.
M
{})
if
err
!=
nil
{
response
.
WriteHeader
(
http
.
StatusInternalServerError
)
response
.
Write
([]
byte
(
`{ "message": "`
+
err
.
Error
()
+
`" }`
))
}
defer
cursor
.
Close
(
ctx
)
for
cursor
.
Next
(
ctx
)
{
var
kick
Kick
cursor
.
Decode
(
&
kick
)
kicks
=
append
(
kicks
,
kick
)
}
if
err
:=
cursor
.
Err
();
err
!=
nil
{
response
.
WriteHeader
(
http
.
StatusInternalServerError
)
response
.
Write
([]
byte
(
`{ "message": "`
+
err
.
Error
()
+
`" }`
))
}
json
.
NewEncoder
(
response
)
.
Encode
(
kicks
)
}
func
main
()
{
func
main
()
{
fmt
.
Println
(
"Starting the application..."
)
fmt
.
Println
(
"Starting the application..."
)
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
(),
10
*
time
.
Second
)
...
@@ -85,6 +61,5 @@ func main() {
...
@@ -85,6 +61,5 @@ func main() {
client
,
_
=
mongo
.
Connect
(
ctx
,
clientOptions
)
client
,
_
=
mongo
.
Connect
(
ctx
,
clientOptions
)
router
:=
mux
.
NewRouter
()
router
:=
mux
.
NewRouter
()
router
.
HandleFunc
(
"/kick"
,
CreateKick
)
.
Methods
(
"POST"
)
router
.
HandleFunc
(
"/kick"
,
CreateKick
)
.
Methods
(
"POST"
)
router
.
HandleFunc
(
"/kicks"
,
GetKicks
)
.
Methods
(
"GET"
)
http
.
ListenAndServe
(
":8080"
,
router
)
http
.
ListenAndServe
(
":8080"
,
router
)
}
}
main_test.go
View file @
073ddfbe
...
@@ -26,7 +26,6 @@ func init() {
...
@@ -26,7 +26,6 @@ func init() {
func
listen
(
finished
chan
bool
)
{
func
listen
(
finished
chan
bool
)
{
router
:=
mux
.
NewRouter
()
router
:=
mux
.
NewRouter
()
router
.
HandleFunc
(
"/kick"
,
CreateKick
)
.
Methods
(
"POST"
)
router
.
HandleFunc
(
"/kick"
,
CreateKick
)
.
Methods
(
"POST"
)
router
.
HandleFunc
(
"/kicks"
,
GetKicks
)
.
Methods
(
"GET"
)
finished
<-
true
finished
<-
true
http
.
ListenAndServe
(
":8080"
,
router
)
http
.
ListenAndServe
(
":8080"
,
router
)
...
@@ -49,7 +48,6 @@ func sendJson(url string) {
...
@@ -49,7 +48,6 @@ func sendJson(url string) {
var
initial
Kick
var
initial
Kick
file
,
_
:=
ioutil
.
ReadFile
(
"test.json"
)
file
,
_
:=
ioutil
.
ReadFile
(
"test.json"
)
_
=
json
.
Unmarshal
([]
byte
(
file
),
&
initial
)
_
=
json
.
Unmarshal
([]
byte
(
file
),
&
initial
)
log
.
Printf
(
"%v
\n
"
,
initial
)
//posting json
//posting json
r
,
err
:=
os
.
Open
(
"test.json"
)
r
,
err
:=
os
.
Open
(
"test.json"
)
...
@@ -97,28 +95,7 @@ func sendJson(url string) {
...
@@ -97,28 +95,7 @@ func sendJson(url string) {
}
}
func
getJsons
()
{
resp
,
err
:=
http
.
Get
(
"http://localhost:8080/kicks"
)
if
err
!=
nil
{
log
.
Fatalf
(
"Cannot Get jsons"
)
}
if
resp
.
StatusCode
!=
200
{
log
.
Fatalf
(
"%v
\n
"
,
resp
.
Status
)
}
}
func
TestMain
(
t
*
testing
.
T
)
{
func
TestMain
(
t
*
testing
.
T
)
{
sendJson
(
"http://localhost:8080/kick"
)
sendJson
(
"http://localhost:8080/kick"
)
getJsons
()
}
/*
func TestCreation(t *testing.T) {
sendJson("http://localhost:8080/kick")
}
}
*/
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