Go-Svelte journey : Preparation and Project Structure (Part-1)

Angga Kesuma
4 min readAug 23, 2020

Preparation :

  1. Golang. just go here if you don’t know how : https://golang.org/doc/install. I use golang v1.15 for this journey
  2. Nodejs + npm. not yet ? just go here : https://nodejs.org/en/download/. I use node v12.18.3 + npm v6.14.6
  3. Python 2.7 or 3
  4. Docker. https://docs.docker.com/get-docker/. you can found my sharing session : https://medium.com/@anggakes/recap-kompasgramedia-sharing-session-hello-docker-202639a53fc7

Project’s Structure :

this picture is how I structured my project. Because in this project I’m just single developer with full stack role. Combine frontend and backend will make me easier to manage the project.

If you have different composition of dev team. maybe you must separate between frontend and backend to their own repository. So this project structure just an example for single fighter like me. Sure ! this structure can change or added some folder for some reason example if we already implement more stack like pubsub, elasticsearch, websocket, etc.

maybe another article I will share how I manage git and project structure depends on our company size. based on my experience. just breakdown the structure for now !

  1. Migrations folder

this folder contains migration data. You can notice, I use django for migration. let’s discuss why I use django for migration. I’ve tried this one https://github.com/golang-migrate/migrate. This package is wonderful, but I got this dirty version issue. This suck for me if migration cannot recover by itself if something error, especially if you already implement CI/CD. So I search the mature one, the option is laravel migration or django migration. I choose django because i think python used by most developer and after I tried have minimum size and file.

  • manage.py
import sys
import os

from django.conf import settings
from django.core.management import execute_from_command_line

settings.configure(
DEBUG=True,
INSTALLED_APPS=[
'db',
],
DATABASES={
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.getenv('POSTGRES_DB', 'membership'),
'USER': os.getenv('POSTGRES_USER', 'admin'),
'PASSWORD': os.getenv('POSTGRES_PASSWORD', 'dev-password'),
'HOST': os.getenv('POSTGRES_HOST', 'localhost'),
'PORT': os.getenv('POSTGRES_PORT', '5433'),
}
},
)

if __name__ == '__main__':
execute_from_command_line(sys.argv)

very minimum manage.py just. only settings of database

  • requirements.txt
Django==2.1.7
psycopg2==2.7.7
psycopg2-binary==2.7.7

dependencies just django and pgsql driver

  • Dockerfile
FROM python:3.7-alpine

# gcc jpeg-dev and zlib-dev required for Pillow (required by some library in requirements.txt)
# gcc libc-dev and linux headers required to install uwsgi
RUN set -e; \
apk add --no-cache zlib-dev postgresql-dev; \
apk add --no-cache --virtual .build-deps \
gcc \
libc-dev \
linux-headers \
musl-dev \
git \
; \
pip install --upgrade pip;
# Install all python dependency libs
# copy requirement only, not whole code so docker can cache it
COPY requirements.txt /app/
RUN
LIBRARY_PATH=/lib:/usr/lib /bin/sh -c "pip3 install -r /app/requirements.txt"
COPY . /app/
WORKDIR /
app/
  • docker-compose.yml
version: "3"
services:
migration:
build: .
environment:
- POSTGRES_DB=gosvelte
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_HOST=docker.for.mac.localhost
- POSTGRES_PORT=5432
volumes:
- .:/app/
command: ["sh", "-c", "python manage.py makemigrations db && python manage.py migrate db"]

2. scripts

scripts folder is the place for our bash script.

3. src

this folder contains our main code. I split by frontend and backend folder.
frontend will contains our svelte code and backend for our golang code. more information about this folder will be discus in next article.

4. docker-compse.yml

docker-compose for our main apps. now, only postgresql

version: '3.3'

services:
db:
image: postgres:11
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: user
POSTGRES_DB: gosvelte
networks:
- gosvelte
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- '5432:5432'

networks:
gosvelte:

5. go.mod + go.sum

our go dependencies, must be at root folder

6. Makefile

our makefile script.

Conclusion :

I think this structure is the best for single developer or team that have merged between frontend and backend team. I’m not egoist to use only one programming language. I want bring out the advantages of each language and tools.

Next article I want to share how to build simple todo API with golang with Hexagonal architecture.

For complete code you can access here:

--

--

Angga Kesuma

learn, do, and share ~ Senior Software Engineer @KompasGramedia