Skip to content

3. Project Structure

ekedonald edited this page Jul 19, 2024 · 5 revisions

Project Structure

Overview

The project is organized to promote modular development and maintainability. Here’s a concise overview of the main directories and files.

Directories

/internal

Contains internal application logic and configurations.

  • config: Configuration setup.
  • models: Database models and migrations.

/pkg

Houses core packages and utilities.

  • repository: Database repository interfaces and implementations.
    • storage: General storage interface.
    • postgresql: PostgreSQL-specific storage implementation.
  • router: HTTP router setup.

/services

Contains the business logic and services used by the application.

/tests

Includes all test files for unit and integration testing.

/utility

Utility functions and helpers used throughout the application.

Key Files

main.go

Entry point of the application. It initializes the configuration, connects to the database, sets up the router, and starts the HTTP server.

Example Code

package main

import (
    "fmt"
    "log"
    "github.com/go-playground/validator/v10"
    "github.com/hngprojects/hng_boilerplate_golang_web/internal/config"
    "github.com/hngprojects/hng_boilerplate_golang_web/internal/models/migrations"
    "github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage"
    "github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage/postgresql"
    "github.com/hngprojects/hng_boilerplate_golang_web/pkg/router"
    "github.com/hngprojects/hng_boilerplate_golang_web/utility"
)

func main() {
    logger := utility.NewLogger() 
    configuration := config.Setup(logger, "./app")
    postgresql.ConnectToDatabase(logger, configuration.Database)
    validatorRef := validator.New()
    db := storage.Connection()

    if configuration.Database.Migrate {
        migrations.RunAllMigrations(db)
    }

    r := router.Setup(logger, validatorRef, db, &configuration.App)
    utility.LogAndPrint("Server running on port 8080")
    log.Fatal(r.Listen(":8080"))
}