Skip to content

Commit 756ad5e

Browse files
authored
Make the go sample application work with docker compose (#176)
* Make the go application work with docker compose. * Updated README.md. * Formatted main.go using gofmt. * Add newlines to few files as recommended by Github.
1 parent 1d2bee9 commit 756ad5e

13 files changed

Lines changed: 117 additions & 38 deletions

File tree

go/samples/http/.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
sqlcommenter-http
2-
docker-data/
1+
docker-data/

go/samples/http/Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Build Stage
2+
# First pull Golang image
3+
FROM golang:1.19-alpine as build-env
4+
5+
ENV APP_NAME sqlcommenter-http
6+
ENV CMD_PATH main.go
7+
8+
# Copy application data into image
9+
COPY . $GOPATH/src/$APP_NAME
10+
WORKDIR $GOPATH/src/$APP_NAME
11+
12+
# Budild application
13+
RUN CGO_ENABLED=0 go build -v -o /$APP_NAME $GOPATH/src/$APP_NAME/$CMD_PATH
14+
15+
# Run Stage
16+
FROM alpine:3.16.3
17+
18+
# Set environment variable
19+
ENV APP_NAME sqlcommenter-http
20+
21+
# Copy only required data into this image
22+
COPY --from=build-env /$APP_NAME .
23+
24+
# Expose application port
25+
EXPOSE 8081
26+
27+
# Start app
28+
CMD ["./sqlcommenter-http", "--db_engine=mysql"]

go/samples/http/README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
# sqlcommenter-http
22

3+
This is a sample application used to test out `sqlcommenter-go-http` instrumentation libraries.
34

45
## Installation
56

7+
We have containerized this application and its related dependencies (i.e. Postgres and MySQL servers) using `docker-compose`.
68

7-
* Clone the source
8-
* Update `connection` string in Index function
9-
* Install dependencies
10-
```
11-
go build
12-
```
9+
To start the application:
10+
```sh
11+
docker compose build
12+
docker compose up
13+
```
1314

14-
* Run the app
15-
```
16-
go run main.go
17-
````
18-
* Hit the url http://localhost:8080/ and observe the mysql logs to see comments appended
15+
This will start the application server as well as MySQL and Postgres databases
16+
(NOTE: At present both database will start even if one is used).
17+
18+
By default the application server will connect with `Postgres` database. To change that,
19+
update the `db_engine` parameter in the [`Dockerfile`](https://github.com/google/sqlcommenter/blob/master/go/samples/http/Dockerfile) to `mysql`.
20+
21+
To view postgres logs, just use the `docker logs` command:
22+
23+
```sh
24+
docker logs --follow postgres
25+
```
26+
27+
To view MySQL logs, use the provided script `./tail_mysql_log.sh`.
28+
29+
30+
## Execution
31+
Hit the url http://localhost:8081/ and observe the MySQL/Postgres logs to see comments appended.
32+
33+
Alternatively, there are few scripts present in the `curls` folder to execute CRUD operations.

go/samples/http/curls/delete.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
curl --header "Content-Type: application/json" \
44
--request DELETE \
5-
http://localhost:8080/todos/$1
5+
http://localhost:8081/todos/$1
66
echo

go/samples/http/curls/index.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# usage: index.sh
22

3-
curl http://localhost:8080
3+
curl http://localhost:8081
44
echo

go/samples/http/curls/insert.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
curl --header "Content-Type: application/json" \
44
--request POST \
55
--data '{"task":"'$1'"}' \
6-
http://localhost:8080/todos
6+
http://localhost:8081/todos
77
echo

go/samples/http/curls/list.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# usage: list.sh
22

3-
curl http://localhost:8080/todos?search=$1
3+
curl http://localhost:8081/todos?search=$1
44
echo

go/samples/http/curls/update.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
curl --header "Content-Type: application/json" \
44
--request PUT \
55
--data '{"task":"'$2'"}' \
6-
http://localhost:8080/todos/$1
6+
http://localhost:8081/todos/$1
77
echo

go/samples/http/docker-compose.yml

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,62 @@
11
version: '3.8'
2+
networks:
3+
mynet:
4+
driver: bridge
5+
6+
27
services:
3-
sqlcommenter_mysql_db:
4-
container_name: sqlcommenter_mysql_db
8+
mysql:
9+
container_name: mysql
510
image: mysql:8
611
command:
712
--general-log=TRUE
813
--general-log-file=/var/lib/mysql/mysql-log.log
914
ports:
10-
- '3306:3306'
15+
- 3306:3306
1116
environment:
1217
MYSQL_DATABASE: 'sqlcommenter_db'
1318
MYSQL_ROOT_PASSWORD: 'password'
19+
healthcheck:
20+
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
21+
interval: 10s
22+
timeout: 5s
23+
retries: 5
24+
networks:
25+
- mynet
1426
volumes:
15-
- './docker-data/mysql:/var/lib/postgresql/data'
16-
sqlcommenter_pg_db:
17-
container_name: sqlcommenter_pg_db
27+
- './docker-data/mysql:/var/lib/mysql'
28+
postgres:
1829
image: postgres:14
30+
container_name: postgres
31+
# restart: always
32+
volumes:
33+
- './docker-data/postgres:/var/lib/postgresql/data'
1934
command: postgres -c log_statement=all
2035
ports:
21-
- '5432:5432'
36+
- 5432:5432
2237
environment:
23-
POSTGRES_USER: dev
24-
POSTGRES_PASSWORD: dev
25-
POSTGRES_DB: sqlcommenter_db
26-
volumes:
27-
- './docker-data/pg:/var/lib/postgresql/data'
38+
POSTGRES_USER: postgres
39+
POSTGRES_PASSWORD: postgres
40+
healthcheck:
41+
test: ["CMD-SHELL", "pg_isready -U postgres"]
42+
interval: 10s
43+
timeout: 5s
44+
retries: 5
45+
networks:
46+
- mynet
47+
http-service:
48+
container_name: http-service
49+
build:
50+
context: .
51+
depends_on:
52+
postgres:
53+
condition: service_healthy
54+
mysql:
55+
condition: service_healthy
56+
links:
57+
- postgres
58+
- mysql
59+
ports:
60+
- 8081:8081
61+
networks:
62+
- mynet

go/samples/http/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,21 @@ func runApp(todosController *todos.TodosController) {
7676
router.PUT("/todos/:id", middleware(todosController.ActionUpdate))
7777
router.DELETE("/todos/:id", middleware(todosController.ActionDelete))
7878

79-
http.ListenAndServe(":8080", router)
79+
http.ListenAndServe(":8081", router)
8080
}
8181

82+
// host = “host.docker.internal”
83+
8284
func runForMysql() *gosql.DB {
83-
connection := "root:password@/sqlcommenter_db"
85+
connection := "root:password@tcp(mysql:3306)/sqlcommenter_db"
8486
db := mysqldb.ConnectMySQL(connection)
8587
todosController := &todos.TodosController{Engine: "mysql", DB: db, SQL: todos.MySQLQueries{}}
8688
runApp(todosController)
8789
return db
8890
}
8991

9092
func runForPg() *gosql.DB {
91-
connection := "postgres://dev:dev@localhost/sqlcommenter_db?sslmode=disable"
93+
connection := "host=postgres user=postgres password=postgres dbname=postgres port=5432 sslmode=disable"
9294
db := pgdb.ConnectPG(connection)
9395
todosController := &todos.TodosController{Engine: "pg", DB: db, SQL: todos.PGQueries{}}
9496
runApp(todosController)

0 commit comments

Comments
 (0)