This project was created for learning purposes and is a crawler that go through the web looking for any information by clicking on each available link.
Some tools used do not represent the best choice, they were only used for learning purposes. For example MongoDB was used, but thinking about performance Redis might be a better alternative. The frontend was not the focus for learning purposes, so the template package
was used.
- Go 1.20+
- Docker
- Docker-compose
- Nginx
- GNU make
- Direnv
- This is not mandatory but is a easily way to control your environment variable for each project without configuring the variables globally
- Golangci-lint
- Godoc
- Govulncheck
- Deadcode
- Viper
- Configuration solution
- Cobra
- Library for creating CLI applications
- Gin web framework
- Gin metrics exporter for Prometheus
- Prometheus client golang
- Testify
- Tools for testifying
- Testcontainers-Go
- Zap logger
- MongoDB
- The web-based admin interface Mongo Express was used
- HTTP expect
- Used to API test
- Gock HTTP mocking
- Sonarqube
You can run the command below to see all the useful commands available and your goals.
make help
To run the project locally you need to export some environment variables and this can be done using direnv
. You can export the variables below.
NGINX_PORT='80'
LOG_LEVEL='ERROR'
MONGODB_USERNAME='root'
MONGODB_PASSWORD='example'
MONGODB_DATABASE='crawler'
MONGODB_COLLECTION='page'
MONGODB_PORT='27017'
MONGODB_EXPRESS_USERNAME='root'
MONGODB_EXPRESS_PASSWORD='example'
MONGODB_EXPRESS_PORT='8081'
After exporting the environment variables, you can run the make compose-up
command and open the http://localhost/index
address.
If you want to run the API outside of Docker:
- you need to export the
MONGODB_HOST
variable aslocalhost
- expose the
MONGODB_PORT
indocker-compose.yml
in themongo
service as belowports: - ${MONGODB_PORT}:${MONGODB_PORT}
- comment out the
api
andnginx
service indocker-compose.yml
and runmake compose-up
and thenmake build-run-api
, the API will run on the default porthttp://localhost:8888/index
- if you want to debug the API, you don't need to run
make build-run-api
, but in your IDE you need to set the command toapi
when starting the application given its using cobra library
Fill in the URI and Depth in the form(it will be used to limit the depth when fetching pages with so many links that they can underperform and can take so long).
By default, the HTTP request takes a timeout of 60 seconds which can be changed by environment variable(API_REQUEST_TIMEOUT).
You can do this by running the make doc
command and going to the address http://localhost:6060
.
The project is set up to run sonarqube
locally and has three commands in the Makefile. The sonarqube
will be downloaded by Docker, but you need to install sonar-scanner following your operating system.
To run sonarqube
locally, you need to export the following environment variables. You can do this using direnv
.
SONAR_PORT='9000'
SONAR_HOST='http://localhost:9000'
SONAR_LOGIN='admin'
SONAR_PASSWORD='admin'
SONAR_BINARY='Here you need to fill it according to your operational system. Example: sonar-scanner for Linux/MacOS or sonar-scanner.bat for Windows'
After installing and configuring sonar-scanner
in $PATH
(if needed) you will be able to run the commands below. By running the make sonarqube-up
and make sonarqube-analysis
commands you can open the http://localhost:9000
address in your browser and login and password as admin
(perhaps sonarqube
may prompt you to change your password).
sonarqube-up: start sonarqube container
sonarqube-analysis: run sonar scanner
sonarqube-down: stop sonarqube container
The project was instrumented using Prometheus
and Grafana
, both of which are configured and downloaded through Docker. Prometheus and Grafana will run together with the application, but you need to export the following environment variables below, and you can do this using direnv
.
PROMETHEUS_PORT='9090'
GRAFANA_PORT='3000'
The application metrics are exposed using the ginmetrics library and can be accessed at http://localhost:8888/metrics
. These exposed metrics are collected by Prometheus and can be accessed at http://localhost:9090
.
The collected metrics are sent to Grafana and can be accessed at http://localhost:3000
. The default credentials are admin
/admin
(Grafana may prompt you to reset the password, but it is optional).
Please note that due to some changes to the tools, the arrangement of items may be different.
After that, you need to configure the data source
by clicking on the Configuration
option in the left hand panel and then clicking on Data source
. Click on the Add Data Source
button and select Prometeus
under Time Series Database
. Fill in the address in the HTTP option as in the image below:
After setting up the data source, you can import the file from the dashboard by clicking on the Dashboard
option in the left side panel and then clicking + Import
. You can upload the file placed in this project at /metrics/grafana/dashboards.json
. After it is loaded, you will see the panels as below: