This is a user store keeping the application users acting as a gRPC server.
It listens to the port 5300.
- Have Go
- Have gRPC
- Have Docker
- Set Environment variables:
USER_STORE_REDIS_URL(used in./dal/client.go)
installed.
Everything starts with designing the protobuf file
(./proto/user.proto for this project) and once the messages and services are designed
(or changed later) the following command should be run:
cd $GOPATH/src/github.com/vahdet/go-user-store
protoc -I proto proto/user.proto --go_out=plugins=grpc:protoThis command will generate (or recreate if it exists) a .pb.go file just beside itself.
It is ./proto/user.pb.go here and it allows implementing Go services, repositories, in short any Go code
behind the services defined in the .proto file.
For this case, the server implementation is performed in ./grpcserver/server.go file.
A Redis instance should be up at the port defined in the config file ./resources/app.yaml. Unless the port is changed, the default one is 6379 on localhost
A sample Docker command to install an official Redis image (with data persistence to disk) is as follows:
docker container run -d --name redisTest \
-v /opt/docker/data/redis:/data \
-p 6379:6379
redis:latest
- See all running containers
docker ps
- See all containers (running or not)
docker ps -a
- Remove Docker image (although it is a bad practice, add
-fforce remove) with a few characters of theCONTAINER_IDwhich you can find the one assigned to your container with thepscommands above.docker rm
- Bonus: Delete stopped containers
docker rm -v $(docker ps -a -q -f status=exited)
Set a user/pass for the redis container.
In this app, data store testing is adopts the approach that a separate redis docker container is created for test usage. However, there are other ways worth trying:
or, if you dare to code through another API anyway:
See the
./Dockerfile
Run the following command in the root directory in order to build as specified in the Dockerfile
docker image build -t go-user-store .This will create an image named go-user-store locally.
You can check the docker images by the command and check
go-user-storeis listed:docker image ls
To run a container out of the image, run the following command
docker container run --publish 5300:5300 --name my-goddamn-user-store --rm outyet
It binds the internal port 5300 to external port 5300. Change the one before colon if you want to expose the container through a different port.
See Pushing and Pulling to and from Docker Hub
A volume is assigned to redis container by -v flag (see previous sections).
There is currently no need for a volume or bind mount for the go code.
The StackOverflow answer resembles three lines of general purpose git commands
that can be used anytime a change is made and should be committed to master branch:
git add .
git commit -a -m "My classical message to be replaced"
git push- A full, secure gRPC client & server implementation guide
- GOENV and environment specific settings??
