Coder Social home page Coder Social logo

golang-microservice's Introduction

Golang-Microservice

A simple microservice with MySQL database connection. It contains parts.

Reqirements for running

1. Go 1.14
2. MySQL 8.0+ for Database
3. Make (optional but make larger commands in short)
4. Docker 

1. Installing the Required Application

I hope GO and MySQL is already installed.

1. Intalling Make in Windows the Easy Way

Install Chocolatey (Chocolatey is a software management solution)

  1. Open PowerShell with Adminitrator
  2. Run
Get-ExecutionPolicy

If it return Restricted then run

Set-ExecutionPolicy AllSigned

OR

Set-ExecutionPolicy Bypass -Scope Process
  1. Install by this command
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
  1. Install make
choco install make

Unix like OS already comes with make preinstalled so no worries.

2. Installing Docker

It's a a bit long process but in short you need to install Docker from https://hub.docker.com/editions/community/docker-ce-desktop-windows/ and it also need specefic version of Windows and WSL2, so check online for for further help.

3. Running Web Application

After Cloning it, download all depencies once

go mod download

Run the command and make sure to set config.yaml file for configuration

make run

OR

go run test

4. Run the MySQL Server

Create a DB with any name and make sure to give the set DB_NAME in Config file and create a table users with following fields.

  1. id AUTOINCREMENT PRIMARY KEY
  2. username UNIQUE VARCHAR (20)
  3. password VARCHAR (20)
  4. marks VARCHAR (20) NULL(by Defualt)

5. Running the Web Application in DOCKER

  1. Buiding the image Syntax :- docker build -t Example :-
docker build -t Test .
  1. Running the image Syntax :- docker run -p : --name Example :-
docker run -d -p 8080:8080 --name TesingMyApp test

Extra Flags can also be added check them out by

docker run --help

Flag that can be used when running the container

-d for running conatainer in detach mode (running in background) --rm for removing the conatiner after stopping it 3. For strating the container

docker start <container ID> or <container name>
  1. For stopping the container
docker stop <container ID> or <container name>
  1. For listing the running and stopped container
docker ps -a
  1. For listing Docker images
docker images
  1. For inspecting the Conatainer
docker inspect <name of container>

Note

-d for running conatainer in detach mode --rm for removing the conatiner after stopping it

6. Running MySQL in Another Container

  1. Pull the MySQL image from docker hub
docker pull mysql/mysql-server:latest
  1. To make sure the data persists even after stopping the container Create Volume in docker Syntax:
docker create -v <location> --name <nameOfTheImahe> <ImageName>

Example :-

docker create -v /var/lib/mysql --name mysqldata mysql/mysql-server:latest
  1. Running the MySQL Container Before running it, it required some ENV variable and config to set, atleast root password is required. You can check from here https://hub.docker.com/_/mysql. Syntax :-
docker run -p <internalPort>:<externalPort> -e <ENV variable name>=<value> --volumes-from <volume location> --name=<name of Container> <image name> 

Example :-

docker run --rm -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=pwd --volumes-from mysqldata --name=mysqldb mysql/mysql-server:latest     

7. Getting inside the running the Container

  1. A database could have been created when running the WebApllication but here will create by another way. Syntax :-
docker exec -it <container ID> <bash or any shell or command>

Example :-

docker exec -it mysqldb bash
  1. Login into mysql as Root Syntax :-
mysql -u <username> -p

Example :-

mysql -u root -p
  1. You will get a error related to Access denied to solve that issue Create a new user with host with IP or % for any IP access to DB Syntax :-
CREATE USER '<username>'@'<IP or %>' IDENTIFIED BY '<password>';

Giving privilages (For Learing purpose give all) Example :-

CREATE USER 'rajiv'@'%' IDENTIFIED BY 'pwd';
GRANT ALL PRIVILEGES ON *.* TO 'rajiv'@'%';
FLUSH PRIVILEGES;
  1. Create the DB as stated at #4

Note

  1. A postman collection has been shared, import in the Postman to see the API endpoints.
  2. For any help reguarding the docker type
docker <commandName> --help

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.