📰 Good News app. Backend in Golang. MongoDB setup using official Golang driver.

It is time to start working with MongoDB!

  1. 📰 Good News app. Backend in Golang behind Traefik reverse proxy with https available.
  2. [in progress] 📰 Good News app. Flutter for rapid mobile applications development.
  3. [in progress] 📰 Good News app. Hummingbird as a promising replacement for frontend frameworks.
  1. Prerequisites & Idea, project and database structure and API endpoints.
  2. Project creation, go modules & GIN (beautiful framework) integration.
  3. Colly usage.
  4. MongoDB setup using official Golang driver.
  5. Running all together locally with Docker and Docker Compose & Traefik v2.0 configuration.
  6. Publishing to Digital Ocean, Let’s Encrypt and DNS Challenge configuration.

MongoDB configuration

We are going to launch MongoDB in a Docker container. While being inside a Docker container, MongoDB will be exposed on a specific url that has following schema: mongodb://mongo_db_user:mongo_db_pswd@mongo:27017/mongo_db_name. As we can see, in order to connect to MongoDB from our code we will need to know mongo_db_user, mongo_db_pswd and mongo_db_name. This information will be set in .env file to avoid having our credentials inside the code. It is also convenient for the cases if some information would change, we will need to set new values only inside .env file and won’t directly touch the code so no need to rebuild whole program.

API_GIN_DEBUG_MODE=trueMONGO_ADMIN=GGCTeamBatr
MONGO_ADMIN_PSWD=MySuperSecretPassword
MONGO_DB_USER=suuuper_user
MONGO_DB_PSWD=soop3r_U$eR_PSWD
MONGO_DB_NAME=good_news_db
type EnvVariables struct {
DebugMode bool
MongoDBName string
MongoDBURL string
}

db/ folder structure

As you might have noticed, I have created a separate folder (package) for crawlers in the previous article so I want to keep all logic separately, divided, let’s say, to microservices. I am going to do the same with accessing, writing and reading information to/from a database.

> go get go.mongodb.org/mongo-driver/mongo

--

--

Problems solver. Open Source Staff. MS in CS. https://batyr.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store