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

Clean up imports and cache config #131

Merged
merged 2 commits into from
Apr 21, 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
31 changes: 18 additions & 13 deletions kevin.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
#!/usr/bin/env python3

# Import the required libraries
import re
# Standard Library Imports
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused import os.

- import os

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
# Standard Library Imports
# Standard Library Imports

import os
from utils.database import collection, all_vulns_collection
import re
from urllib.parse import unquote

# Third-Party Library Imports
from dotenv import load_dotenv
from flask import Flask, jsonify, render_template, request, send_from_directory
from flask_restful import Api, Resource
from flask_restful import Api
from flask_compress import Compress
from dotenv import load_dotenv
from gevent.pywsgi import WSGIServer

# Project-Specific Imports
from utils.database import collection, all_vulns_collection
from utils.cache_manager import cache, init_cache
from modules.reportgen import report_gen
from schema.api import (
cveLandResource,
cveNVDResource,
cveMitreResource,
VulnerabilityResource,
AllKevVulnerabilitiesResource,
RecentKevVulnerabilitiesResource,
cveLandResource,
cveNVDResource,
cveMitreResource,
VulnerabilityResource,
AllKevVulnerabilitiesResource,
RecentKevVulnerabilitiesResource,
RecentVulnerabilitiesByDaysResource
)
from schema.serializers import serialize_vulnerability,serialize_all_vulnerability
from gevent.pywsgi import WSGIServer
from schema.serializers import serialize_vulnerability, serialize_all_vulnerability


# Create a cache key for openai routes based on the query parameters
def cve_cache_key(*args, **kwargs):
Expand Down
49 changes: 26 additions & 23 deletions utils/cache_config.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# Import the os module to interact with the operating system
import os

# Import redis so we can configure a connection pool
from redis import ConnectionPool

# Import the load_dotenv function from the python-dotenv module
from dotenv import load_dotenv
# Call the load_dotenv function to load environment variables from a .env file

# Load environment variables from .env file
load_dotenv()

# Get the value of the REDIS_IP environment variable
# Define constants for configuration parameters
REDIS_IP = os.getenv("REDIS_IP")
pool = ConnectionPool(host=REDIS_IP, port=6379, max_connections=20, timeout=5)
REDIS_PORT = 6379
CACHE_DEFAULT_TIMEOUT = 600
CACHE_KEY_PREFIX = "kev_"
MAX_CONNECTIONS = 20
TIMEOUT = 5

def setup_cache_config(redis_ip):
# Create a connection pool for Redis
pool = ConnectionPool(host=redis_ip, port=REDIS_PORT, max_connections=MAX_CONNECTIONS, timeout=TIMEOUT)

# Define the cache configuration
cache_config = {
'CACHE_TYPE': 'flask_caching.backends.RedisCache',
'CACHE_DEFAULT_TIMEOUT': CACHE_DEFAULT_TIMEOUT,
'CACHE_REDIS_HOST': redis_ip,
'CACHE_REDIS_PORT': REDIS_PORT,
'CACHE_KEY_PREFIX': CACHE_KEY_PREFIX,
'CACHE_REDIS_CONNECTION_POOL': pool
}

return cache_config

# Define a dictionary to hold the configuration for the cache
cache_config = {
# Specify the type of cache to use (Redis in this case)
'CACHE_TYPE': 'flask_caching.backends.RedisCache',
# Specify the default timeout for the cache (in seconds)
'CACHE_DEFAULT_TIMEOUT': 600,
# Specify the host for the Redis server
'CACHE_REDIS_HOST': REDIS_IP,
# Specify the port for the Redis server
'CACHE_REDIS_PORT': '6379',
# Specify a prefix for all keys stored in the cache
'CACHE_KEY_PREFIX': 'kev_',
# Specify the Redis connection pool
'CACHE_REDIS_CONNECTION_POOL': pool
}
# Set up cache configuration
cache_config = setup_cache_config(REDIS_IP)
Loading