Skip to content

Commit 70e44ce

Browse files
committed
Add the Grafana dashboard (#16)
Improve docker-compose Improve CI Improve Dockerfile Add the docstring Update the buf version
1 parent 1aed921 commit 70e44ce

File tree

23 files changed

+2058
-70
lines changed

23 files changed

+2058
-70
lines changed

.github/workflows/build.yml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
name: build
22

3-
on: [push, pull_request]
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
# https://crontab.guru/
8+
# At 12:00 AM, only on Monday
9+
- cron: 0 0 * * 1
410

511
jobs:
612
build:
13+
strategy:
14+
matrix:
15+
go-version: [1.17.x]
16+
os: [ubuntu-latest, macos-latest, windows-latest]
17+
runs-on: ${{ matrix.os }}
18+
steps:
19+
- name: Install Go
20+
uses: actions/setup-go@v2
21+
with:
22+
go-version: ${{ matrix.go-version }}
23+
- name: Checkout code
24+
uses: actions/checkout@v2
25+
- name: Build
26+
shell: bash
27+
run: |
28+
make all
29+
30+
docker:
731
runs-on: ubuntu-latest
832
steps:
9-
- uses: actions/checkout@v2
10-
- name: Build a Docker image
11-
run: docker build . --file Dockerfile --tag image:$(date +%s)
33+
- uses: actions/checkout@v2
34+
- name: Build a Docker image
35+
run: docker build . --file Dockerfile --tag image:$(date +%s)

.github/workflows/image.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@ name: image
22

33
on:
44
push:
5-
branches:
6-
- main
5+
tags:
6+
- '*'
77

88
jobs:
99
deploy:
1010
name: Build
1111
runs-on: ubuntu-latest
1212
env:
1313
REGISTRY: ghcr.io
14-
IMAGE_NAME: docker.pkg.github.com/${{ github.repository }}
1514
COMMIT_HASH: ${{ github.sha }}
1615
environment: default
1716

1817
steps:
1918
- name: Set env version
20-
run: echo "IMAGE_TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
19+
run: echo "GIT_TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
2120

2221
- name: Set up QEMU
2322
uses: docker/setup-qemu-action@v1
@@ -44,4 +43,7 @@ jobs:
4443
- uses: docker/build-push-action@v2
4544
with:
4645
push: true
47-
tags: ghcr.io/${{ github.repository_owner }}/reddit-feed-api/server:${{ env.IMAGE_TAG }}
46+
build-args: |
47+
VERSION=${{ env.GIT_TAG }}
48+
COMMIT_HASH=${{ env.COMMIT_HASH }}
49+
tags: ghcr.io/${{ github.repository_owner }}/reddit-feed-api/server:${{ env.GIT_TAG }}

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ ARG GITHUB_PATH=github.com/arttet/reddit-feed-api
33

44
FROM golang:1.17-alpine AS builder
55

6+
ARG VERSION COMMIT_HASH
7+
68
RUN apk add --update make git protoc protobuf protobuf-dev curl
79
COPY . /home/${GITHUB_PATH}
810
WORKDIR /home/${GITHUB_PATH}
@@ -17,7 +19,7 @@ WORKDIR /root/
1719

1820
COPY --from=builder /home/${GITHUB_PATH}/bin/reddit-feed-api .
1921
COPY --from=builder /home/${GITHUB_PATH}/config.yml .
20-
COPY --from=builder /home/${GITHUB_PATH}/migrations/ ./migrations/
22+
COPY --from=builder /home/${GITHUB_PATH}/migrations/ ./migrations/001_init_db.sql
2123

2224
RUN chown root:root reddit-feed-api
2325
CMD ["./reddit-feed-api"]

Makefile

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,18 @@ SERVICE_EXE=./bin/$(SERVICE_NAME)$(shell go env GOEXE)
1212

1313
###############################################################################
1414

15-
# https://github.com/bufbuild/buf/releases
16-
BUF_VERSION=v1.0.0-rc1
17-
18-
OS_NAME=$(shell uname -s)
19-
OS_ARCH=$(shell uname -m)
20-
GOBIN?=$(GOPATH)/bin
21-
22-
ifeq ("NT", "$(findstring NT,$(OS_NAME))")
23-
OS_NAME=Windows
24-
endif
15+
.PHONY: all
16+
all: deps build
2517

26-
###############################################################################
18+
.PHONY: deps
19+
deps: .deps
2720

2821
.PHONY: build
2922
build: generate .build
3023

24+
.PHONY: generate
25+
generate: .generate
26+
3127
.PHONY: run
3228
run:
3329
go run \
@@ -74,9 +70,6 @@ grpcui:
7470

7571
###############################################################################
7672

77-
.PHONY: deps
78-
deps: .deps
79-
8073
.deps:
8174
go env -w GO111MODULE=on
8275

@@ -96,17 +89,25 @@ deps: .deps
9689

9790
###############################################################################
9891

99-
.PHONY: generate
100-
generate: .generate
92+
# https://github.com/bufbuild/buf/releases
93+
BUF_VERSION=v1.0.0-rc2
10194

102-
.generate:
103-
@command -v buf 2>&1 > /dev/null || (echo "Install buf" && \
104-
mkdir -p "$(GOBIN)" && \
105-
curl -k -sSL0 https://github.com/bufbuild/buf/releases/download/$(BUF_VERSION)/buf-$(OS_NAME)-$(OS_ARCH)$(shell go env GOEXE) -o "$(GOBIN)/buf" && \
106-
chmod +x "$(GOBIN)/buf")
107-
@PATH="$(GOBIN):$(PATH)" buf generate
95+
OS_NAME=$(shell uname -s)
96+
OS_ARCH=$(shell uname -m)
97+
GO_BIN=$(shell go env GOPATH)/bin
98+
BUF_EXE=$(GO_BIN)/buf$(shell go env GOEXE)
10899

109-
mv pkg/$(SERVICE_NAME)/$(SERVICE_PATH)/pkg/$(SERVICE_NAME)/* pkg/$(SERVICE_NAME)
100+
ifeq ("NT", "$(findstring NT,$(OS_NAME))")
101+
OS_NAME=Windows
102+
endif
103+
104+
.generate:
105+
@ command -v buf 2>&1 > /dev/null || (echo "Install buf" && \
106+
mkdir -p "$(GO_BIN)" && \
107+
curl -k -sSL0 https://github.com/bufbuild/buf/releases/download/$(BUF_VERSION)/buf-$(OS_NAME)-$(OS_ARCH)$(shell go env GOEXE) -o "$(BUF_EXE)" && \
108+
chmod +x "$(BUF_EXE)")
109+
@ $(BUF_EXE) generate
110+
cp -R pkg/$(SERVICE_NAME)/$(SERVICE_PATH)/pkg/$(SERVICE_NAME)/* pkg/$(SERVICE_NAME)/
110111
rm -rf pkg/$(SERVICE_NAME)/github.com/
111112
cd pkg/$(SERVICE_NAME) && ls go.mod || (go mod init $(SERVICE_PATH)/pkg/$(SERVICE_NAME) && go mod tidy)
112113

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ docker-compose build
2020

2121
### For local development
2222
```sh
23-
./reddit-feed-api --cfg config-dev.yml --migration up
23+
make run
2424
```
2525

2626
### Docker
@@ -96,6 +96,13 @@ The Swagger UI is an open source project to visually render documentation for an
9696

9797
- http://localhost:8080
9898

99+
### Grafana:
100+
101+
- http://localhost:3000
102+
- login `admin`
103+
- password `ADMIN`
104+
- [Dashboard: Reddit Feed API](http://localhost:3000/d/QXuFMwN7z/reddit-feed-api?orgId=1&refresh=5s)
105+
99106
### Jaeger UI
100107

101108
Monitor and troubleshoot transactions in complex distributed systems.
@@ -108,12 +115,6 @@ Prometheus is an open-source systems monitoring and alerting toolkit
108115

109116
- http://localhost:9090
110117

111-
### Grafana:
112-
113-
- http://localhost:3000
114-
- login `admin`
115-
- password `MYPASSWORT`
116-
117118
### Kafka UI
118119

119120
UI for Apache Kafka is a simple tool that makes your data flows observable, helps find and troubleshoot issues faster and deliver optimal performance. Its lightweight dashboard makes it easy to track key metrics of your Kafka clusters - Brokers, Topics, Partitions, Production, and Consumption.

doc/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Requirements
2+
3+
Your task will be to build a simplified version of the Reddit feed API that powers
4+
http://old.reddit.com. **Please use Go as the programming language**. This will be a REST API
5+
that allows users to do the following:
6+
7+
**Create new posts.** Posts should be validated for correctness. They should have the following
8+
fields:
9+
10+
1. Title
11+
2. Author: This should be a random 8 character string prefixed with t2_. The 8 character
12+
string should only contain lowercase letters and numbers. For example, my user ID is
13+
t2_11qnzrqv.
14+
3. Link: This should be a valid URL. It's ok if your validation is not perfect.
15+
4. Subreddit: The subreddit associated with this post.
16+
5. Content: In the case of a text-only post. **A post cannot have both a link and content
17+
populated.**
18+
6. Score: The total score associated with the upvotes and downvotes of a post.
19+
7. Promoted: A boolean field indicating whether or not the post is an ad or not.
20+
8. NSFW: Not safe for work. A boolean that indicates whether or not the post is safe for
21+
work
22+
23+
**Generate a feed of posts.** This feed should have the following characteristics:
24+
25+
1. It should be ranked by score, and the post with the highest score should show up first.
26+
2. It should be paginated, and each page should have at most 27 posts. Your API should
27+
support fetching a specific page in the feed.
28+
3. If a page has 3 posts or greater, the second post should always be a promoted post if a
29+
promoted post is available, regardless of the score.
30+
4. If a page has greater than 16 posts, the 16th post should always be a promoted post if a
31+
promoted post is available, regardless of the score.
32+
5. As an exception to rules 3 and 4, **a promoted post should never be shown adjacent
33+
to an NSFW post**. You can ignore rules 3 and 4 in this case.

docker-compose.yaml

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ services:
5959
networks:
6060
- reddit-network
6161
volumes:
62-
# - pgdata:/var/lib/postgresql/data
62+
- pg_data:/var/lib/postgresql/data
6363
- ./external/scripts/init-database.sh:/docker-entrypoint-initdb.d/init-database.sh
6464

6565
jaeger:
@@ -89,7 +89,7 @@ services:
8989
networks:
9090
- reddit-network
9191
volumes:
92-
- "./external/prometheus.yml:/etc/prometheus/prometheus.yml"
92+
- ./external/prometheus.yml:/etc/prometheus/prometheus.yml
9393

9494
zookeeper:
9595
image: confluentinc/cp-zookeeper
@@ -151,7 +151,7 @@ services:
151151
- 8080:8080
152152
restart: always
153153
volumes:
154-
- ./external/swagger:/docs
154+
- swagger_data:/docs
155155
environment:
156156
SWAGGER_JSON: /docs/api.swagger.json
157157
depends_on:
@@ -167,12 +167,13 @@ services:
167167
links:
168168
- prometheus
169169
environment:
170-
- GF_SECURITY_ADMIN_PASSWORD=MYPASSWORT
170+
- GF_SECURITY_ADMIN_PASSWORD=ADMIN
171171
- GF_USERS_ALLOW_SIGN_UP=false
172172
networks:
173173
- reddit-network
174174
volumes:
175-
- "./data/grafana:/var/lib/grafana"
175+
- grafana_data:/var/lib/grafana
176+
- grafana_init:/etc/grafana/provisioning
176177

177178
# Graylog: https://hub.docker.com/r/graylog/graylog/
178179
graylog:
@@ -212,7 +213,7 @@ services:
212213
networks:
213214
- reddit-network
214215
volumes:
215-
- mongodata:/data/db
216+
- mongo_data:/data/db
216217

217218
# Elasticsearch: https://www.elastic.co/guide/en/elasticsearch/reference/6.x/docker.html
218219
elasticsearch:
@@ -233,14 +234,32 @@ services:
233234
networks:
234235
- reddit-network
235236
volumes:
236-
- esdata:/usr/share/elasticsearch/data
237+
- es_data:/usr/share/elasticsearch/data
237238

238239
volumes:
239-
pgdata:
240+
pg_data:
240241
driver: local
241-
esdata:
242+
swagger_data:
242243
driver: local
243-
mongodata:
244+
driver_opts:
245+
type: none
246+
o: bind
247+
device: ${PWD}/external/swagger
248+
grafana_data:
249+
driver: local
250+
driver_opts:
251+
type: none
252+
o: bind
253+
device: ${PWD}/data/grafana
254+
grafana_init:
255+
driver: local
256+
driver_opts:
257+
type: none
258+
o: bind
259+
device: ${PWD}/external/grafana
260+
mongo_data:
261+
driver: local
262+
es_data:
244263
driver: local
245264

246265
networks:

0 commit comments

Comments
 (0)