A simple server for prototyping made using golang.
This is not production-ready code, it is just for proof of concept, which means I don't write all the required code (ex. data validation), I just write enough to get the most of what I am trying to implement/learn.
go 1.16+
- Run Server:
make run
- Run Test:
make test
-
Make sure you have docker installed for your operating system. Download Link
-
Make sure that you are not running the server on your local machine (aka. have run the
go run .
) -
If you are using linux make sure you have the make command installed.
For Ubuntu you can use these commands:sudo apt update sudo apt install make
-
Build the image:
make build-image
-
Run the image
make run-image
make stop-image
make remove-image
-
Open Terminal
-
Navigate to the project directory
-
Build the docker image (with tag
server-example
)docker build . -t server-example
-
Check that the image was created
To check that the docker image was created run the commanddocker images
This should output something like this
REPOSITORY TAG IMAGE ID CREATED SIZE server-example latest ... 1 minute ago 323MB
If you do not see the second line go back to step 3. Make sure that you type the command correctly, or even better copy it and paste it into your terminal.
-
Run the docker image We need to run the image while making sure to bound a port so that we can access our services
docker run -p 8080:8080 --name server-example -d server-example
-
Make sure your image is running To check that the docker image is running run the command
docker ps
This should output something like this
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ... server-example "/server" 26 seconds ago Up 25 seconds 0.0.0.0:8080->8080/tcp server-example
If you do not see the second line go back to step 5. Make sure that you type the command correctly, or even better copy it and paste it into your terminal.
-
Test your app.
To test the app you can use the routes, as described in theRoutes
.
-
Open Terminal
-
Navigate to the project directory
-
Check if the server image is running To check if the server image is running run the command
docker ps
If the image is running you should see something like this
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ... server-example "/server" 26 seconds ago Up 25 seconds 0.0.0.0:8080->8080/tcp server-example
-
Stop the server
To stop the server run:docker stop server-example
The output of this command should be the name of the image
server-example
-
Validate that the server image stoped running To check that the server image has stopped running run the command
docker ps
You should get an output like so
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Or if you have other images running you should not see the image
server-example
in the list
-
Open Terminal
-
Navigate to the project directory
-
Check that the server image is running To check that the server image is not running run the command
docker ps
You should get an output like so
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Or if you have other images running you should not see the image
server-example
in the list -
Remove the image
To remove the docker image run:docker rm server-example
The output of this command should be the name of the image
server-example
-
Validate that the image was deleted running To check that the image was deleted running run the command
docker ps -a
You should get an output like so
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Or if you have built other images previously you should not see the image
server-example
in the list
You can find the available routes inside the request-examples
folder.
If you are using VS Code as your editor you can install the REST Client extension and click on the send request button above each Request to execute the request.
If you are not using VS Code you can use Postman
- Create a server using Golang, with a simple endpoint that logs in the server console
Here I wanted:- to learn how to create a simple Server in golang without using any external libraries
- to experiment with different file structures
- to learn how to use make files
- Create an image manipulation endpoint (I chose Gaussian blur of an image)
Here I wanted:- to learn how to handle file uploading via form-data post request
- do simple image processing using go
- use goroutines and channels to run intensive code on a separate thread
- Create a simple sum endpoint
Here I wanted:- to learn how to write unit tests (test utils.Sum)
- to learn how to write integration tests (test the whole endpoint)
- to experiment with recursion in golang
- Create a CI/CD environment
Here I wanted:- to learn how linting works in go
- to learn how to write a workflow file that checks linting, ability to build and that the tests pass
- Run the app inside a Docker container
Here I wanted:- to learn what is docker
- to learn how docker works
- to learn how to open a container
- to learn the basics of deploying an application inside a container