Skip to content

Commit

Permalink
chore: update configuration files and improve caching mechanism
Browse files Browse the repository at this point in the history
- Added cache directory to the application configuration for better cache management.
- Updated Docker Compose and production YAML files to include a cache volume for improved performance.
- Enhanced the .gitignore file to exclude cache files.
- Refactored the Restricted component in the frontend to improve user authorization handling and loading state management.

These changes aim to streamline the application setup and enhance user experience by ensuring proper caching and authorization checks.
  • Loading branch information
jhagberg committed Jan 2, 2025
1 parent 2a67678 commit 1cdbd00
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 144 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ s3data/*

# Logs
logs/*

#
cache/*
6 changes: 5 additions & 1 deletion app/herdbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
SESSION_TYPE="filesystem",
DEBUG=True, # some Flask specific configs
CACHE_TYPE="FileSystemCache",
CACHE_DIR="/tmp",
CACHE_DIR="/var/cache/herdbook",
CACHE_DEFAULT_TIMEOUT=300,
)

Expand Down Expand Up @@ -1176,6 +1176,10 @@ def verify_certificate(i_number):
@APP.route("/api/herd/<h_id>/yearlyreport", methods=["GET", "POST"])
@login_required
def herd_yearly_report(h_id):
"""Get or create yearly report for a herd."""
if not user.can_edit(resource_type="herd", resource_id=h_id):
return {"status": "error", "message": "Not authorized"}

user_id = session.get("user_id", None)
user = da.fetch_user_info(user_id)

Expand Down
171 changes: 87 additions & 84 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,90 +1,93 @@
---
version: "3.4"
services:
database:
restart: always
image: postgres:12
container_name: herdbook-db
env_file:
- ./.docker/database-variables.env
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- ./postgres-data:/var/lib/postgresql/data
herdbook-frontend:
image: herdbook_frontend
container_name: herdbook_frontend
build:
context: ./
dockerfile: .docker/frontend
cache_from:
- ghcr.io/nbisweden/herdbook_frontend
command: bash -c "rm -rf /home/node/app/node_modules/* && npm run start"
volumes:
- ./frontend:/home/node/app/
- exclude:/home/node/app/node_modules
environment:
- NODE_OPTIONS="--max-old-space-size=2048"
main:
image: herdbook_main
build:
context: ./
dockerfile: .docker/main
cache_from:
- ghcr.io/nbisweden/herdbook_main
container_name: herdbook-main
env_file:
- ./.docker/database-variables.env
- ./.docker/r-api-variables.env
- ./.docker/main-api-variables.env
ports:
- 8080:8080
- 8443:8443
volumes:
- type: bind
source: ./app
target: /api_src
- type: bind
source: ./config
target: /config
- type: bind
source: ./logs
target: /logs
depends_on:
- database
- herdbook-frontend
r-api:
container_name: r-api
build:
context: ./
dockerfile: .docker/r-api
cache_from:
- ghcr.io/nbisweden/herdbook_r-api
env_file:
- ./.docker/database-variables.env
- ./.docker/r-api-variables.env
image: herdbook_r-api
volumes:
- type: bind
source: ./R
target: /code
s3backend:
command: -c 'echo s3user:x:$$(stat -c %u /data):0:s3 user:/:/bin/sh >> /etc/passwd && su s3user -c "mkdir -p /data/$${S3_BUCKET} && minio server /data"'
entrypoint: /bin/sh
container_name: s3backend
environment:
- MINIO_ACCESS_KEY=accesskeytest
- MINIO_SECRET_KEY=secretkeytest
env_file:
- ./.docker/main-api-variables.env
volumes:
- ./s3data:/data
healthcheck:
test: ["CMD", "curl", "-fkq", "http://localhost:9000/minio/health/live"]
interval: 5s
timeout: 20s
retries: 3
image: minio/minio:RELEASE.2021-04-18T19-26-29Z
database:
restart: always
image: postgres:12
container_name: herdbook-db
env_file:
- ./.docker/database-variables.env
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- ./postgres-data:/var/lib/postgresql/data
herdbook-frontend:
image: herdbook_frontend
container_name: herdbook_frontend
build:
context: ./
dockerfile: .docker/frontend
cache_from:
- ghcr.io/nbisweden/herdbook_frontend
command: bash -c "rm -rf /home/node/app/node_modules/* && npm run start"
volumes:
- ./frontend:/home/node/app/
- exclude:/home/node/app/node_modules
environment:
- NODE_OPTIONS="--max-old-space-size=2048"
main:
image: herdbook_main
build:
context: ./
dockerfile: .docker/main
cache_from:
- ghcr.io/nbisweden/herdbook_main
container_name: herdbook-main
env_file:
- ./.docker/database-variables.env
- ./.docker/r-api-variables.env
- ./.docker/main-api-variables.env
ports:
- 8080:8080
- 8443:8443
volumes:
- type: bind
source: ./app
target: /api_src
- type: bind
source: ./config
target: /config
- type: bind
source: ./logs
target: /logs
- type: bind
source: ./cache
target: /var/cache/herdbook
depends_on:
- database
- herdbook-frontend
r-api:
container_name: r-api
build:
context: ./
dockerfile: .docker/r-api
cache_from:
- ghcr.io/nbisweden/herdbook_r-api
env_file:
- ./.docker/database-variables.env
- ./.docker/r-api-variables.env
image: herdbook_r-api
volumes:
- type: bind
source: ./R
target: /code
s3backend:
command: -c 'echo s3user:x:$$(stat -c %u /data):0:s3 user:/:/bin/sh >> /etc/passwd && su s3user -c "mkdir -p /data/$${S3_BUCKET} && minio server /data"'
entrypoint: /bin/sh
container_name: s3backend
environment:
- MINIO_ACCESS_KEY=accesskeytest
- MINIO_SECRET_KEY=secretkeytest
env_file:
- ./.docker/main-api-variables.env
volumes:
- ./s3data:/data
healthcheck:
test: ["CMD", "curl", "-fkq", "http://localhost:9000/minio/health/live"]
interval: 5s
timeout: 20s
retries: 3
image: minio/minio:RELEASE.2021-04-18T19-26-29Z

volumes:
exclude: null
45 changes: 28 additions & 17 deletions frontend/src/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,37 @@ const StyledMenuItem = withStyles((theme) => ({
},
}))(MenuItem);

function Restricted(props: { children: React.ReactElement }) {
/*
If user reloads page we do not have any usercontext yet.
User will always be null even if user is logged in in backend
This will check with backend if api/user returns data then the user is logged in
and we can proceed the user to the restricted component. If not then redirect to Google login.
If user is clicking the link the usercontext is already loaded and we can assume the user is
logged in.
*/
interface RestrictedProps {
children: React.ReactElement;
}

const Restricted: React.FC<RestrictedProps> = (props) => {
const [isAuthorized, setIsAuthorized] = useState<boolean | null>(null);
const { user } = useUserContext();

if (user == null) {
get("/api/user").then((data) => {
return data
? props.children
: (window.location.href = "/api/login/google");
});
useEffect(() => {
const checkAuth = async () => {
if (user === null) {
const data = await get("/api/user");
if (!data) {
window.location.href = "/api/login/google";
} else {
setIsAuthorized(true);
}
} else {
setIsAuthorized(true);
}
};

checkAuth();
}, [user]);

if (isAuthorized === null) {
return <div>Loading...</div>;
}
return props.children;
}

return <>{props.children}</>;
};

export function Navigation() {
const { logout } = useUserContext();
Expand Down
87 changes: 45 additions & 42 deletions production.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
---
version: "3.4"
services:
database:
restart: always
image: postgres:12
container_name: herdbook-db
env_file:
- ./.docker/database-variables.env
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- ./postgres-data:/var/lib/postgresql/data
main:
image: ghcr.io/nbisweden/herdbook_main:${TAG:?TAG-not-set}
container_name: herdbook-main
env_file:
- ./.docker/database-variables.env
- ./.docker/r-api-variables.env
- ./.docker/main-api-variables.env
ports:
- 80:8080
- 443:8443
- 9090:9090
volumes:
- type: bind
source: ./app
target: /api_src
- type: bind
source: ./config
target: /config
- type: bind
source: ./logs
target: /logs
depends_on:
- database
- r-api
restart: always
database:
restart: always
image: postgres:12
container_name: herdbook-db
env_file:
- ./.docker/database-variables.env
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- ./postgres-data:/var/lib/postgresql/data
main:
image: ghcr.io/nbisweden/herdbook_main:${TAG:?TAG-not-set}
container_name: herdbook-main
env_file:
- ./.docker/database-variables.env
- ./.docker/r-api-variables.env
- ./.docker/main-api-variables.env
ports:
- 80:8080
- 443:8443
- 9090:9090
volumes:
- type: bind
source: ./app
target: /api_src
- type: bind
source: ./config
target: /config
- type: bind
source: ./logs
target: /logs
- type: bind
source: ./cache
target: /var/cache/herdbook
depends_on:
- database
- r-api
restart: always

r-api:
image: ghcr.io/nbisweden/herdbook_r-api:${TAG:?TAG-not-set}
container_name: r-api
env_file:
- ./.docker/database-variables.env
- ./.docker/r-api-variables.env
restart: always
r-api:
image: ghcr.io/nbisweden/herdbook_r-api:${TAG:?TAG-not-set}
container_name: r-api
env_file:
- ./.docker/database-variables.env
- ./.docker/r-api-variables.env
restart: always

0 comments on commit 1cdbd00

Please sign in to comment.