- install packages: Mux router, Mongo, Toml
- go get -u -v github.com/gorilla/mux
- go get -u -v gopkg.in/mgo.v2
- go get -u -v github.com/BurntSushi/toml
-
install mongoDB
-
put DB configuration to config.toml, if you have other DB port, e.g. server="127.0.0.1:27017" database="posts"
-
run mongoDB:
- for Windows, so "C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe"
- run main.go
- go run main.go -> the server will be run in http://localhost:8080
- CURL for testing
-
GET /login - to get TOKEN, which then you have to use to authenticate in HEADER
-
POST /posts - to create a post, for example: curl -H "Token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE1NDMzNjEyNDcsInVzZXIiOiJpdXJpaSJ9.dxZ3FISCEfIPpoYuE9tBvFrTKAJ7beElCIWGxB_lvKU" -d '{"title":"test", "content":"test"}' -H "Content-Type: application/json" -X POST http://localhost:8080/posts
where "Token" was generated by "/login",
{"title":"test", "content":"test", "created":"timestamp"} is payload
- GET /posts - get all posts curl -H "Token":"" http://localhost:8080/posts
where "Token" was generated by "/login"
-
GET /posts/{id} - get exact post by id curl -H "Token":"" http://localhost:8080/posts/3
-
PUT /posts - update post curl -H "Token":"" -d '{"id":"<_id>", "title":"test NEW", "content":"test"}' -H "Content-Type: application/json" -X PUT http://localhost:8080/posts
-
DELETE /posts/{id} - delete posts curl -H "Token":"" -X DELETE http://localhost:8080/posts/{id}
From scrach DB is empty, it is droped every run of main.go (see init() in rest.go)
NOTE: to test in your ENV it should be extracted here $GOPATH/src/github.com/ The whole path will be like that: $GOPATH/src/github.com/<name_of_the_working_directory>