Label Lab system Back-end api via go lang
- Please create your own branch for your features. If you want to merge your codes into
main
branch, please create a new pull request and contact project owner or other team members to review your codes. - If you need dramatic refactoring, please discuss with code owners.
- Make sure you have golang installed:
- Install through homebrew:
brew install golang
- Check whether golang is successfully installed:
go version
- Install through homebrew:
- Go environment:
- Step 1: In your terminal (take zsh for example), create a directory specially for Go projects, you have to put and run your Go projects in this directory. For example, in your Documents directory:
$ mkdir go-projects
- Step 2: Set Go Paths environment variables. Feel free to choose any one from
Vim version
andterminal version
.
- Vim version:
- terminal command:
$ vim ~/.zshenv
- In your vim:
export GOROOT=/usr/local/go export GOPATH=~/Documents/go-projects export PATH=$PATH:$GOPATH/bin export PATH=$PATH:$GOROOT/bin
- Remind: GOROOT would be where you installed your golang. Can be checked by
$ which go
- terminal command:
- Terminal version:
- Termianl commands:
$ echo 'export GOPATH=~/Documents/go-projects' >> ~/.zshenv $ echo 'export GOROOT=/usr/local/go' >> ~/.zshenv $ echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.zshenv $ echo 'export PATH=$PATH:$GOROOT/bin' >> ~/.zshenv
- Termianl commands:
$ echo $GOPATH
(should show~/Documents/go-projects
) (can deduce the rest from this) - Vim version:
- Step 3: Create go workspace by command:
$ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin
- Step 4: Clone or create your Go projects in
$GOPATH/src
. Since this repo have absolute directorygithub.com/...
, your have to create a directory namedgithub.com
in$GOPATH/src
, and then clone this repo ingithub.com
. - Step 5: Install packages. You can use
$ go mod download
. - Step 6: Run this project, use
go run .
orgin -i run .
- Step 7: To check whether server is successfully running, you can try:
$ curl -X GET 'http://127.0.0.1:9090'
in a new terminal window. - Ref
- Step 1: In your terminal (take zsh for example), create a directory specially for Go projects, you have to put and run your Go projects in this directory. For example, in your Documents directory:
- Some file/directory notes:
main.go
: main function that run this server.route.go
: routes of restful api can be found here./controller
: main logics here./service
: connect to db and do CRUDs./models
: db schemas/db
: deal with mongodb connection.- others: are not important.
- make sure you have docker installed.
- After cloning this repo, use the following commands (in this project directory) to run the server:
$ docker build -t 'bilab-backend' . $ docker run -p 9090:9090 bilab-backend
- Access api with:
http://localhost:9090/articles
- To run the server, you can use
go mod download
go run .
- If you need live-reload, install
gin
:$ go get github.com/codegangsta/gin
. Then run this repo by:$ gin -i run .
. Ref
To check the error log or CI/CD process.
- make ssh connection:
ssh [email protected]
and enter the password. - attach to tmux session:
tmux attach -t golang_server
- if Server is shutdown, run:
bash server.sh
to re-run the server.