Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/docker local build #293

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,64 @@
# OpenWeightlifting
This is the monorepo for the OpenWeightlifting.org project. The aim of this project is to build a database of the latest Olympic Weightlifting results from all around the world. This originally started from a scraping tool and quickly grew into what you see here now. All the results within the database were pulled directly from the event results pages from the National Governing Body of that nation. We try to avoid manual data entry so this is all done with our tooling written in Python.

[![codecov](https://codecov.io/gh/euanwm/OpenWeightlifting/branch/development/graph/badge.svg?token=CX7H10ZNLM)](https://codecov.io/gh/euanwm/OpenWeightlifting)
There's always some questions around certain design decisions in this project so we'll address them now.

### Why Golang for the backend?
Originally it was Python but the build time was terrible and the response times were slow. Not only that but the memory usage was high. Golang was chosen because it's fast, has a low memory footprint and the build times are quick. It's also a language that's easy to pick up and learn. We migrated from Python to Golang within in a week of picking up the language.

### Why NextJS for the frontend?
This was a bit of a no brainer. We wanted to use React and NextJS (TS) is a great framework for it. The amount of features around rounting, server side rendering and static site generation is great. While it can also serve as a backend, we've chosen to keep the backend and frontend separate due the performance benefits of having a dedicated backend.

## Local Testing
We've added a docker-compose file to make it easier to test locally. This will spin up a local instance of the backend and frontend services. In production, these services are deployed separately.
Majority of the contributors on this are FE developers, so we've containerised the front and backend portions of the project. To get going quickly with the project, you'll need to have docker installed.

### Frontend Development (NextJS)
From the root of the project, run the following commands to spin up a backend container and launch the frontend.
```bash
docker compose up -d backend
cd frontend
npm install
npm run dev
```
While the backend service is running, you'll also be able to run the FE API call tests against it.
```bash
docker-compose build
docker-compose up
npm run test
```
When you get bored and want to kill it...

Once you're done, you can stop the backend container with the following command.
```bash
docker-compose down
docker compose down
```

### Backend-only
### Backend Development (Golang)
When launching the backend service you'll need to toggle the CORS flag which is done be adding the 'local' argument when calling the executable.
```bash
go build backend.go
./backend local
```

### Frontend-only
We prefer to use npm for the frontend stuff.
## Database Management (Python)
To pull the latest results from the all relevant federations, we've added a Makefile with a few commands to make it easier. You'll need to have pipenv installed to run the commands.
### Pulling the latest results
```bash
npm install
npm run dev
make update_database
```

### Staging the latest results
This came about so if you have a messy amount of unstaged changes, you can stage them all and then commit them in one go.
```bash
make stage_csv
```

### Updating the database
To pull the latest results from the all relevant federations, you'll need to run the following command from the python_tools directory:
### Checking the database
In order to reduce the amount of checks at runtime, we've added a check to make sure the database is in a good state. This will check for duplicate entries and missing data.
```bash
pipenv install
pipenv run python backend_cli.py --update all
make check_db
```

### License
# License
Done this under the BSD-3-Clause license. Simply because it's what the sport80 library is under and i'm hella lazy.

### Contributing
# Contributing
If you want to contribute, please do! We're always looking for more people to help out. If you're not sure where to start, check out the issues page and join the discord server.
https://discord.gg/kqnBqdktgr
7 changes: 5 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
FROM golang:1.20.2-alpine3.16 AS build
FROM golang:1.21-alpine AS build
WORKDIR /work
COPY . .
RUN \
--mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
go build -o backend backend.go
go build -o backend

FROM alpine:3.16 AS final
COPY --from=build /work/backend /bin/backend

ENV HOSTNAME backend
EXPOSE 8080

WORKDIR /
ENTRYPOINT ["/bin/backend"]
4 changes: 2 additions & 2 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func CORSConfig(localEnv bool) cors.Config {
corsConfig := cors.DefaultConfig()
if localEnv {
log.Println("Local mode - Disabling CORS nonsense")
corsConfig.AllowOrigins = []string{"https://www.openweightlifting.org", "http://localhost:3000"}
corsConfig.AllowOrigins = []string{"https://www.openweightlifting.org", "http://localhost:3000", "http://frontend-app:3000", "*"}
} else {
corsConfig.AllowOrigins = []string{"https://www.openweightlifting.org"}
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func CacheMeOutsideHowBoutDat() {
func main() {
apiServer := buildServer()
go CacheMeOutsideHowBoutDat()
err := apiServer.Run()
err := apiServer.Run("backend:8080") // listen and serve on
if err != nil {
log.Fatal("Failed to run server")
}
Expand Down
18 changes: 16 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,38 @@ services:
restart: always
build:
context: ./backend
expose:
- "8080"
ports:
- "8080:8080"
networks:
- backend-network
volumes:
- ./backend/event_data:/event_data
environment:
- HOSTNAME=backend
- PORT=8080
command: local

client:
container_name: next-app
container_name: frontend-app
build:
context: ./frontend
args:
- API=http://localhost:8080
- API=http://backend:8080
expose:
- "3000"
ports:
- "3000:3000"
networks:
- backend-network
depends_on:
- backend
environment:
- NODE_ENV=development
- CHOKIDAR_USEPOLLING=true


networks:
backend-network:
driver: bridge
3 changes: 2 additions & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# base image
FROM node:18.7.0
FROM node:18.4-alpine
ARG API
ENV API $API

Expand All @@ -15,5 +15,6 @@ COPY . .
# build
RUN --mount=type=cache,target=/usr/src/app/.next/cache npm run build

EXPOSE 3000
# start app
CMD ["npm", "run", "dev"]
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "API=http://localhost:8080 next dev",
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
Expand Down
Loading