Commit 3134da4a authored by Strakhov Egor's avatar Strakhov Egor

Initial commit

parents
Pipeline #10891 failed with stages
in 46 seconds
# MongoDB REST API
## Installation
The recommended way to get started using the MongoDB Go driver is by using go modules to install the dependency in
your project. This can be done either by importing packages from `go.mongodb.org/mongo-driver` and having the build
step install the dependency or by explicitly running
```bash
go get go.mongodb.org/mongo-driver/mongo
```
## Usage
Document contains these six lines:
1) _id (ObjectID)- id of the document
2) Company (string) - name of the company
3) Timestamp (string) - pushing time with format: "2006-01-02 15:04:05"
4) Longitude (float64)
5) Attitude
6) Velocity
### Set Json
To push json in MongoDB, you can write it in test.json with the following fields:
1) "company": "Sirius",
2) "longitude": 12345,
3) "attitude": 346,
4) "velocity": 1
ID and Timestamp will be generated automatically
### Run
Run the application:
```bash
go run test.go
```
Push json:
```bash
curl -X POST -d @test.json http://localhost:8080/kick
```
Pull all docs:
```bash
curl -X GET http://localhost:8080/kicks
```
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
)
This diff is collapsed.
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/gorilla/mux"
"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/mongo"
)
var client *mongo.Client
type Kick struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Company string `json:"company,omitempty" bson:"company,omitempty"`
Timestamp string `json:"timestamp,omitempty" bson:"timestamp,omitempty"`
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)
collection := client.Database("mongodb").Collection("kicks")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
now := time.Now()
kick.Timestamp = now.Format("2006-01-02 15:04:05")
result, _ := collection.InsertOne(ctx, kick)
json.NewEncoder(response).Encode(result)
}
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() + `" }`))
return
}
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() + `" }`))
return
}
json.NewEncoder(response).Encode(kicks)
}
func main() {
fmt.Println("Starting the application...")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, _ = mongo.Connect(ctx, clientOptions)
router := mux.NewRouter()
router.HandleFunc("/kick", CreateKick).Methods("POST")
router.HandleFunc("/kicks", GetKicks).Methods("GET")
http.ListenAndServe(":8080", router)
}
{
"company": "Sirius",
"longitude": 12345,
"attitude": 346,
"velocity": 1
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment