Skip to content

Dirc/webapp_fullstack_go_docker

Repository files navigation

GoLang Webapp

Based on this great blog of Soham Kamani.

# Run unit tests
go test -v

# Run web app
go run main.go bird_handlers.go store.go

Browse to

wget localhost:8080
wget localhost:8080/hello
wget localhost:8080/assets/

Dependecies

Use dep to manage dependencies.

# Install all dependencies
dep ensure

GoLang Docker image

docker build -t gowebapp .
docker run -it --rm -p 8080:8080 --name my-running-gowebapp gowebapp

When you develop new code, you will need to remove the image in order to get it rebuild. (COPY in the Dockerfile does not detect changes?)

docker image rm gowebapp

Postgres Docker image

docker pull postgres
# Start postgress container
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
# Connect using psql
docker run -it --rm --link some-postgres:postgres postgres psql -h postgres -U postgres
#> password: mysecretpassword

Play around with SQL.

-- Create the database
CREATE DATABASE bird_encyclopedia;
-- Enter the database
\c bird_encyclopedia

-- Create table
CREATE TABLE birds (
  id SERIAL PRIMARY KEY,
  species VARCHAR(256),
  description VARCHAR(1024)
);

-- Get info
\d
\d birds
select * from birds;

-- Insert a value
INSERT INTO birds (species, description) VALUES ('kanarie', 'Small yellow brid');
select * from birds;
select species, description from birds;

Docker-compose

docker-compose up

You can verify if the database is initialized using psql:

docker exec -it webappfullstackgodocker_db_1 psql -d bird_encyclopedia -U postgres -c "select * from birds;"

Cleaning things:

docker-compose -f docker-compose.yml rm --force

Connect Go webapp to Postgres container

connString := "host=db user=postgres password=secret dbname=bird_encyclopedia sslmode=disable"

Reference

ToDo

  • Add unit test for store
  • Add CI flow, Travis CI or Cirlce CI?

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published