Skip to content

This is a Go language learning project with simple RestFul services using docker with Postgres

Notifications You must be signed in to change notification settings

fatihtotrakanli/simple-golang-restful-with-docker-and-postgres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

This project is a Go language learning project with simple RestFul services. It uses postgres db inside with docker-compose. You can compose with dockerfile or create your own postgres database without it.

For run docker-compose, you need to write following commands. In your project folder,

      cd docker
      docker-compose up

then PostgreSQL works on 32300 Port (32300 -> 5432). You can access with database IDE (DataGrip, Intellij etc.) with configure port 32300.

If you want to conncect from your host system type the following command to terminal.

      psql -h localhost -p 32300 -d docker -U docker --password

For more information about it,

Dockerize PostgreSQL

Database table configuration

      CREATE TABLE USERS (
        ID INT PRIMARY KEY,
        NAME TEXT NOT NULL,
        SURNAME TEXT NOT NULL,
        AGE INT NOT NULL
      );
      
      CREATE SEQUENCE public.users_id_seq NO MINVALUE NO MAXVALUE NO CYCLE;
      ALTER TABLE public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq');
      ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;

Database access configuration inside code

Under config/config.go directory in the project, you will find database access configuration. You can change it with your custom configuration.

      DB_USER     = "docker"
      DB_PASSWORD = "docker"
      DB_NAME     = "docker"
      PORT = "32770"

How can run?

First of all, you need to have Go in your computer. For Mac you can install with brew easily.

      brew install go

If everything is OK, you should encounter an output like this at terminal when wrote go version.

      go version                                    
      go version go1.9.2 darwin/amd64

You need to following library for the postgres.

      go get github.com/lib/pq

For run the project, in the project directory you need to write following command.

      go run main.go

If everything works correctly, you can start the CRUD operations with following URL.

      http://127.0.0.1:3000

URL's and Example

List all of user (Need To Use GET method)

      http://127.0.0.1:3000/getAll

Add new User with JSON type ((Need To Use POST method))

      http://127.0.0.1:3000/newUser
      
      {
      	"name": "mockName",
      	"surname": "mockSurname",
      	"age": 30
      	}

List one user with the given Id (Need To Use GET method)

      http://127.0.0.1:3000/users/1

Update one user with the given Id (Need To Use PUT method)

      http://127.0.0.1:3000/users/1

Delete one user with the given Id (Need To Use DELETE method)

      http://127.0.0.1:3000/users/1

About

This is a Go language learning project with simple RestFul services using docker with Postgres

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages