A RESTful chat API based on Go's net/http package, httprouter, and my GoFileDb package.
Setting this API up should be fairly easy if you have Go set up.
- Install GoLang from the official Go website.
- Install the following Go packages:
- httprouter:
go get github.com/julienschmidt/httprouter
- gofiledb:
go get github.com/teejays/gofiledb
- httprouter:
-
Clone this repository:
git clone https://github.com/teejays/restfulchat.git
-
In the project folder, edit the
GoFiledbRoot
variable in settings.json file to a folder path that you have read & write access to (this is where all the data will be stored). -
Compile the application:
go build -o server.out
orgo install
or however you feel comfortable. -
Assuming that the compiled executable is named server.out, start the server:
./server.out
You can use Go's internal testing framework to test this project. The unit tests for this package are located in ./tests folder. To run the tests, use the following command from the project root directory:
go tests -v ./tests
We have the following API endpoints
-
GET /v1/chat/:userid: Fetches the chat log of the userid
- CURL e.g.
curl localhost:8080/v1/chat/someuser1
- CURL e.g.
-
POST /v1/chat/:userid: Sends a message from userid. The message content and recipient is provided in the request body.
- CURL e.g.
curl localhost:8080/v1/chat/someuser1 -X POST -H "Content-Type: application/json" -d '{"Content":"Hello World!", "To":"someuser2"}'
- CURL e.g.
-
PUT /v1/chat/:userid: Edits a message previously sent from userid to a given recipient. The id of the message to edit, the recipient, and the new message content are provided in the request body.
- CURL e.g.
curl localhost:8080/v1/chat/someuser1 -X PUT -H "Content-Type: application/json" -d '{"MessageId": 1, Content":"Hello World! (edited)", "To":"someuser2"}'
- CURL e.g.
-
DELETE /v1/chat/:userid: Deletes a message previously sent from userid to a given recipient. The id of the message to delete and the recipientare provided in the request body.
- CURL e.g.
curl localhost:8080/v1/chat/someuser1 -X DELETE -H "Content-Type: application/json" -d '{"MessageId": 1, "To":"someuser2"}'
- CURL e.g.
The applications is based on three objects:
-
User: Represents a user.
- Structure:
- UserId (string)
- Buddy: a user that another user is interacts with.
- Structure:
-
Conversation: A conversation is stored communication between two or more users.
- Structure:
- UserIds: an array of user ids of all the users that are a part of a conversation
- Messages: An array of Message
- LastMessageId (int): Keeps track of the last (also largest) unique message id so the new messages can be given an appropriate id.
- Structure:
-
Message: The most basic data unit that makes a conversation.
- Structure:
- Id (int): unique identifier of a message within a conversation
- Content (string): the content of a message
- TimestampCreated (time): when the message was sent
- TimestampUpdated (time): when the message was last updated
- From (string): contributed the message in a conversation
- Structure:
The authentication layer for this server hasn't been implemented yet. However, the API is built in a way that that Basic Auth could be incorporated easily without changing the structure of the code.
I did not use any web framework for the project. The project folder structure and the relationships between different Go packages is something I decided on looking at the how complicated the code was. The simple nature of the project didn't make it necessary to go for a proper framework.
I decided to write this in Go for one main reason: I love Go. I picked up GoLang while at work about a year ago. Since then, I have fallen in love the opinionatedness of the language, the strictly typed nature. There is a Go way of doing things, and that's something I've started to admire a lot. I don't consider my a Go expert yet, but I really want to be.
I am using my own GoFiledb package for as a database. GoFiledb is a simple, minimalistic Go client that lets applications use the filesystem as a database. The client is still in development phase, and this is the second project that has used it. The main advantage of GoFiledb is that it uses the years of optimization efforts that went into file systems to make reading and serving of data is very fast. It is very quick to set up (vs. a proper database, which are sometimes an overkill for a simple project).
Scalability: This is a minimalistic API, developed mostly for fun and experimentation reasons. In order to scale it further, a few decisions probably need to be changed. For example, the local file syetem based data storage should probably be replaced by a proper schemaless DB system.
For any issues or feedback, please create an issue in Github for this repo.