Skip to content

Commit

Permalink
Merge pull request #1 from hbl-ngocnd1/master
Browse files Browse the repository at this point in the history
init project
  • Loading branch information
hbl-ngocnd1 committed Aug 27, 2022
2 parents 9faabae + aa76b77 commit 9956315
Show file tree
Hide file tree
Showing 42 changed files with 3,115 additions and 2 deletions.
37 changes: 37 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "kill $(lsof -t -i:8080) | go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
kill_delay = "0s"
log = "build-errors.log"
send_interrupt = false
stop_on_error = true

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
19 changes: 19 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Deploy to IBM Cloud Foundry
on:
push:
branches:
- main
jobs:

deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy to IBM Cloud Foundry
# You may pin to the exact commit or the version.
uses: IBM/cloudfoundry-deploy@master
with:
IBM_CLOUD_API_KEY: ${{ secrets.IBM_CLOUD_API_KEY }}
IBM_CLOUD_CF_API: ${{ secrets.IBM_CLOUD_CF_API }}
IBM_CLOUD_CF_ORG: ${{ secrets.IBM_CLOUD_CF_ORG }}
IBM_CLOUD_CF_SPACE: ${{ secrets.IBM_CLOUD_CF_SPACE }}
80 changes: 80 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: tests
on:
push:
tags:
- v*
branches:
- main
pull_request:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ^1.18
- name: Checkout code
uses: actions/checkout@v2
- name: Run linters
uses: golangci/golangci-lint-action@v2
with:
version: latest
test:
runs-on: ubuntu-latest
env:
CLOUDANT_URL: ${{ secrets.CLOUDANT_URL }}
GOOGLE_APPLICATION_API_KEY: ${{ secrets.GOOGLE_APPLICATION_API_KEY }}
DEBUG: true
CI_TEST: true
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v2
with:
go-version: 1.18
- name: Checkout code
uses: actions/checkout@v2
- name: Run tests
run: make test

coverage:
runs-on: ubuntu-latest
env:
CLOUDANT_URL: ${{ secrets.CLOUDANT_URL }}
GOOGLE_APPLICATION_API_KEY: ${{ secrets.GOOGLE_APPLICATION_API_KEY }}
DEBUG: true
CI_TEST: true
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v2
with:
go-version: 1.18
- name: Checkout code
uses: actions/checkout@v2
- name: Make coverage
run: |
make coverage.out
- name: Convert coverage.out to coverage.lcov
uses: jandelgado/gcov2lcov-action@v1.0.8
- name: Coveralls
uses: coverallsapp/github-action@1.1.3
with:
github-token: ${{ secrets.github_token }}
path-to-lcov: coverage.lcov

vulns:
runs-on: ubuntu-latest
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v2
with:
go-version: ^1.18
- name: Checkout code
uses: actions/checkout@v2
- name: Make Go list
run: make go.list
- name: Nancy
uses: sonatype-nexus-community/nancy-github-action@main
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

app
get-started
.env
.env_test
/vendor/
tmp/
bin/
.idea
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:latest
WORKDIR /go/src/app
COPY main.go /go/src/app
COPY vendor /go/src/app/vendor
COPY static /go/src/app/static
COPY public/views /go/src/app/public/views

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
RUN apk --no-cache add curl
WORKDIR /root/
COPY --from=0 go/src/app/main .
COPY --from=0 go/src/app/static static
COPY --from=0 go/src/app/public/views public/views
CMD ["./main"]
LABEL version=demo-3
5 changes: 3 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@
APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright {yyyy} {name of copyright owner}

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -199,3 +199,4 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
NAME := get-started
MOCKGEN_BUILD_FLAGS ?= -mod=mod
GO ?= go
GO_ENV ?= GOPRIVATE="github.com/hbl-ngocnd1/dictionary"

deploy:
ibmcloud cf push

go.list:
go list -json -m all > go.list

mockgen:
mockgen -build_flags=$(MOCKGEN_BUILD_FLAGS) -destination=./services/mock_services/mock_dictionary.go github.com/hbl-ngocnd1/dictionary/services DictionaryService
mockgen -build_flags=$(MOCKGEN_BUILD_FLAGS) -destination=./services/mock_services/mock_translate.go github.com/hbl-ngocnd1/dictionary/services TranslateService
mockgen -build_flags=$(MOCKGEN_BUILD_FLAGS) -destination=./usecase/mock_usecase/mock_dictionary.go github.com/hbl-ngocnd1/dictionary/usecase DictUseCase
.PHONY: test
test: FLAGS ?= -parallel 3
test:
$(GO_ENV) CI_TEST=test $(GO) test $(FLAGS) ./... -cover

coverage.out:
go test -v -covermode=count -coverprofile=coverage.out ./...

html_coverage.out:
go test -v -covermode=count -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: get-started
97 changes: 97 additions & 0 deletions README-kubernetes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Deploy to IBM Cloud Kubernetes Service

Follow these instructions to deploy this application to a Kubernetes cluster and connect it with a Cloudant database.

## Download

```bash
git clone https://github.com/IBM-Cloud/get-started-go
cd get-started-go
```

## Build Docker Image

1. Find your container registry **namespace** by running `ibmcloud cr namespaces`. If you don't have any, create one using `ibmcloud cr namespace-add <name>`

2. Identify your **Container Registry** by running `ibmcloud cr info` (Ex: registry.ng.bluemix.net)

3. Build and tag (`-t`)the docker image by running the command below replacing REGISTRY and NAMESPACE with he appropriate values.

```sh
docker build . -t <REGISTRY>/<NAMESPACE>/myapp:v1.0.0
```
Example: `docker build . -t registry.ng.bluemix.net/mynamespace/myapp:v1.0.0

4. Push the docker image to your Container Registry on IBM Cloud

```sh
docker push <REGISTRY>/<NAMESPACE>/myapp:v1.0.0
```

## Deploy

#### Create a Kubernetes cluster

1. [Creating a Kubernetes cluster in IBM Cloud](https://console.bluemix.net/docs/containers/container_index.html#clusters).
2. Follow the instructions in the Access tab to set up your `kubectl` cli.

#### Create a Cloudant Database

1. Go to the [Catalog](https://console.bluemix.net/catalog/) and create a new [Cloudant](https://console.bluemix.net/catalog/services/cloudant-nosql-db) database instance.

2. Choose `Legacy and IAM` for **Authentication**

3. Create new credentials under **Service Credentials** and copy value of the **url** field.

4. Create a Kubernetes secret with your Cloudant credentials.

```bash
kubectl create secret generic cloudant --from-literal=url=<URL>
```
Example:
```bash
kubectl create secret generic cloudant --from-literal=url=https://username:passw0rd@username-bluemix.cloudant.com
```

#### Create the deployment

1. Replace `<REGISTRY>` and `<NAMESPACE>` with the appropriate values in `kubernetes/deployment.yaml`
2. Create a deployment:
```shell
kubectl create -f kubernetes/deployment.yaml
```
- **Paid Cluster**: Expose the service using an External IP and Loadbalancer
```
kubectl expose deployment get-started-go --type LoadBalancer --port 8080 --target-port 8080
```

- **Free Cluster**: Use the Worker IP and NodePort
```bash
kubectl expose deployment get-started-go --type NodePort --port 8080 --target-port 8080
```

### Access the application

Verify **STATUS** of pod is `RUNNING`

```shell
kubectl get pods -l app=get-started-go
```

**Standard (Paid) Cluster:**

1. Identify your LoadBalancer Ingress IP using `kubectl get service get-started-go`
2. Access your application at t `http://<EXTERNAL-IP>:8080/`

**Free Cluster:**

1. Identify your Worker Public IP using `ibmcloud cs workers YOUR_CLUSTER_NAME`
2. Identify the Node Port using `kubectl describe service get-started-go`
3. Access your application at `http://<WORKER-PUBLIC-IP>:<NODE-PORT>/`


## Clean Up
```bash
kubectl delete deployment,service -l app=get-started-go
kubectl delete secret cloudant
```
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[![tests](https://github.com/hbl-ngocnd1/dictionary/actions/workflows/tests.yml/badge.svg)](https://github.com/hbl-ngocnd1/dictionary/actions/workflows/tests.yml)
[![Deploy to IBM Cloud Foundry](https://github.com/hbl-ngocnd1/dictionary/actions/workflows/dev.yml/badge.svg)](https://github.com/hbl-ngocnd1/dictionary/actions/workflows/dev.yml)
## Prerequisites

You'll need the following:
* [Git](https://git-scm.com/downloads)
* [Go](https://golang.org/dl/)

## 1. Clone the app

Now you're ready to start working with the simple Go *hello world* app. Clone the repository and change to the directory where the sample app is located.
```
git clone https://github.com/hbl-ngocnd1/dictionary
cd get-started
```

Peruse the files in the *get-started-go* directory to familiarize yourself with the contents.

## 2. Run the app locally use [air](https://github.com/cosmtrek/air)
Create .env file
```cmd
CLOUDANT_URL=dasdsa
GOOGLE_APPLICATION_API_KEY=sss
DEBUG=true
SYNC_PASS=123456
```
Build and run the app.
```
air
```

View your app at: http://localhost:8080
Loading

0 comments on commit 9956315

Please sign in to comment.