Skip to content

Commit

Permalink
Merge pull request #210 from ninoseki/update-readme
Browse files Browse the repository at this point in the history
Fix Redis issues
  • Loading branch information
ninoseki authored Feb 4, 2024
2 parents 084682a + 65ec1cd commit 80b645b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ Alternatively, you can deploy the application on Heroku.

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/ninoseki/eml_analyzer)

## Configuration

Configuration can be done via environment variables.

Alternatively you can set values through `.env` file. Values in `.env` file will be automatically loaded.

| Key | Desc. | Default |
| ---------------------- | ----------------------------------------------- | ----------- |
| `INQUEST_API_KEY` | InQuest API key | - |
| `REDIS_EXPIRE` | Redis cache expiration time (in seconds) | 3600 |
| `REDIS_KEY_PREFIX` | Redis key prefix | `analysis` |
| `REDIS_URL` | Redis URL | - |
| `SPAMASSASSIN_HOST` | SpamAssassin host | `127.0.0.1` |
| `SPAMASSASSIN_PORT` | SpamAssassin port | 783 |
| `SPAMASSASSIN_TIMEOUT` | SpamAssassin timeout (in seconds) | 10 |
| `URLSCAN_API_KEY` | urlscan.io API Key | - |
| `VIRUSTOTAL_API_KEY` | VirusTotal API Key | - |
| `ASYNC_MAX_AT_ONCE` | Max number of concurrently running lookup tasks | `None` |
| `ASYNC_MAX_PER_SECOND` | Max number of tasks spawned per second | `None` |

## ToDo

- [x] Support MSG format.
Expand Down
8 changes: 3 additions & 5 deletions backend/api/endpoints/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ def cache_response(
redis: Redis,
response: schemas.Response,
expire: int = settings.REDIS_EXPIRE,
field: str = settings.REDIS_FIELD,
key_prefix: str = settings.REDIS_KEY_PREFIX,
):
redis.hset(name=field, key=response.id, value=response.model_dump_json())

if expire > 0:
redis.expire(name=response.id, time=expire)
ex = expire if expire > 0 else None
redis.set(f"{key_prefix}:{response.id}", value=response.model_dump_json(), ex=ex)


@router.post(
Expand Down
6 changes: 5 additions & 1 deletion backend/api/endpoints/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ async def cache_keys(optional_redis: deps.OptionalRedis) -> list[str]:
detail="Redis cache is not enabled",
)

return optional_redis.hkeys(settings.REDIS_FIELD) # type: ignore
byte_keys: list[bytes] = optional_redis.keys(f"{settings.REDIS_KEY_PREFIX}:*") # type: ignore
return [
byte_key.decode().removeprefix(f"{settings.REDIS_KEY_PREFIX}:")
for byte_key in byte_keys
]
2 changes: 1 addition & 1 deletion backend/api/endpoints/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def lookup(id: str, *, optional_redis: deps.OptionalRedis) -> schemas.Resp
detail="Redis cache is not enabled",
)

got: bytes | None = optional_redis.hget(key=id, name=settings.REDIS_FIELD) # type: ignore
got: bytes | None = optional_redis.get(f"{settings.REDIS_KEY_PREFIX}:{id}") # type: ignore
if got is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
Expand Down
2 changes: 1 addition & 1 deletion backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Redis
REDIS_URL: DatabaseURL | None = config("REDIS_URL", cast=DatabaseURL, default=None)
REDIS_EXPIRE: int = config("REDIS_EXPIRE", cast=int, default=3600)
REDIS_FIELD: str = config("REDIS_FIELD", cast=str, default="analysis")
REDIS_KEY_PREFIX: str = config("REDIS_KEY_PREFIX", cast=str, default="analysis")

# 3rd party API keys
VIRUSTOTAL_API_KEY: Secret | None = config(
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/components/Cache.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Loading v-if="getCacheKeysTask.isRunning"></Loading>
<ErrorMessage :error="getCacheKeysTask.last?.error" v-if="getCacheKeysTask.isError" />
<div class="block" v-if="getCacheKeysTask.last?.value && !getCacheKeysTask.last.isRunning">
<div class="buttons">
<div class="buttons" v-if="getCacheKeysTask.last.value.length > 0">
<router-link
class="button is-link is-light"
:to="{ name: 'Lookup', params: { id: key } }"
Expand All @@ -13,6 +13,9 @@
>{{ key }}</router-link
>
</div>
<article class="message is-info" v-else>
<div class="message-body">There is no cache.</div>
</article>
</div>
</div>
</template>
Expand Down

0 comments on commit 80b645b

Please sign in to comment.