DEV Community

Matija Krajnik
Matija Krajnik

Posted on • Updated on • Originally published at

Server configuration


Currently we have hardcoded values for server address and port. Same goes for database options required for connecting to our database. That is obviously not a great solution, so let's extract this configuration to ENV variables and add logic to read values from there. First we will create new directory internal/conf/ and there we will create conf.go file with content:

package conf

import (

const (
  hostKey       = "RGB_HOST"
  portKey       = "RGB_PORT"
  dbHostKey     = "RGB_DB_HOST"
  dbPortKey     = "RGB_DB_PORT"
  dbNameKey     = "RGB_DB_NAME"
  dbUserKey     = "RGB_DB_USER"
  dbPasswordKey = "RGB_DB_PASSWORD"

type Config struct {
  Host       string
  Port       string
  DbHost     string
  DbPort     string
  DbName     string
  DbUser     string
  DbPassword string

func NewConfig() Config {
  host, ok := os.LookupEnv(hostKey)
  if !ok || host == "" {

  port, ok := os.LookupEnv(portKey)
  if !ok || port == "" {
    if _, err := strconv.Atoi(port); err != nil {

  dbHost, ok := os.LookupEnv(dbHostKey)
  if !ok || dbHost == "" {

  dbPort, ok := os.LookupEnv(dbPortKey)
  if !ok || dbPort == "" {
    if _, err := strconv.Atoi(dbPort); err != nil {

  dbName, ok := os.LookupEnv(dbNameKey)
  if !ok || dbName == "" {

  dbUser, ok := os.LookupEnv(dbUserKey)
  if !ok || dbUser == "" {

  dbPassword, ok := os.LookupEnv(dbPasswordKey)
  if !ok || dbPassword == "" {

  return Config{
    Host:       host,
    Port:       port,
    DbHost:     dbHost,
    DbPort:     dbPort,
    DbName:     dbName,
    DbUser:     dbUser,
    DbPassword: dbPassword,

func logAndPanic(envVar string) {
  log.Println("ENV variable not set or value not valid: ", envVar)
Enter fullscreen mode Exit fullscreen mode

With configuration logic implemented, all that is left to do is use it. For that we will need to slightly refactor our existing code. Our files will then look like this:


package database

import (


func NewDBOptions(cfg conf.Config) *pg.Options {
  return &pg.Options{
    Addr:     cfg.DbHost + ":" + cfg.DbPort,
    Database: cfg.DbName,
    User:     cfg.DbUser,
    Password: cfg.DbPassword,
Enter fullscreen mode Exit fullscreen mode


package server

import (

func Start(cfg conf.Config) {

  router := setRouter()

  // Start listening and serving requests
Enter fullscreen mode Exit fullscreen mode


package main

import (

func main() {
Enter fullscreen mode Exit fullscreen mode

One more change is required in migrations/main.go file. Just import rgb/internal/conf package and change line

Enter fullscreen mode Exit fullscreen mode


Enter fullscreen mode Exit fullscreen mode

We are now ready to read ENV variables required for our configuration. But one more thing is missing. We need to feed those values to ENV. To do that let's create new file in root project directory named .env:

export RGB_HOST=
export RGB_PORT=8080
export RGB_DB_HOST=localhost
export RGB_DB_PORT=5432
export RGB_DB_NAME=rgb
export RGB_DB_USER=postgres
export RGB_DB_PASSWORD=postgres
Enter fullscreen mode Exit fullscreen mode

We can feed these values to ENV using source command. So, now we can start our server like this:

source .env
go run cmd/rgb/main.go
Enter fullscreen mode Exit fullscreen mode

Top comments (0)