CRUD APIs for Articles and their Tags
The project is dockerized and has a docker-compose file that points to Dockerfiles for the API and DB. The Dockerfile for database is located in db directory and the Dockerfile for the API is in the root. The table structure for article and tag tables is in the pg.sql file of db directory.
- Go version 1.11 and upwards is required to build this project. Please check your go version using the command
go version
- I have used go1.11 to create the project and distributed go.mod which contains all the dependencies and can run a project outside the $GOPATH.
- Create a directory structure of the form
ffx/github.com/pamelag
. I have used github code layout. - Next cd to the directory pamelag and run the command
git clone https://github.com/pamelag/blue.git
- Now the directory structure would be
ffx/github.com/pamelag/blue
- To run the project docker must be installed and the project needs to be built using
docker-compose build
- Next use the command
docker-compose up
to start. The application runs on port8080
- The last line of a successful startup would be
Created connection pool successfully
, otherwise please try stopping it withdocker-compose down
and starting again withdocker-compose build
anddocker-compose up
- The Post API for
/articles
only needs"title", "body" and "tags"
. Thedate
would be asystem date
inserted usingtime.Now()
- Please check the Docker container date at the time of container start, as the records would be inserted for that date.
- content is the domain package which contains the main entities Article, Tag, the factory methods and Repository interfaces
- article package contains the Article services for AddArticle and GetArticle. The article package depends on the content package.
- tag package has implementation of the GetTag service. The tag package has a dependency on the content package.
- server package has all the handlers for routing, decoding requests and encoding responses. The handlers in the server package invoke the service functions in the article and tag packages.
- postgres package has all the database statements and queries. It has the implementations of the ArticleRepository and TagRepository interfaces defined in the content package
- main package has the main.go file which initializes all the repositories and services and handles the wiring and dependency injection of repositories into services and the services to the http server. The config.go reads all configurations for the host and port and authentication details.
- db package has the pg.sql file which contains the DDL for the article and tag tables