Skip to content

Commit 5f4e9f3

Browse files
authored
Merge pull request #1 from RustLangES/api-secure
Use bot API-KEY
2 parents 0b86903 + 3253a77 commit 5f4e9f3

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
uses: cloudflare/wrangler-action@v3
4141
env:
4242
ENDPOINT: ${{ secrets.ENDPOINT }}
43+
BOT_APIKEY: ${{ secrets.BOT_APIKEY }}
4344
with:
4445
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
4546
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Para construir y desplegar este proyecto, necesitarás lo siguiente:
2424
### Variables de Entorno
2525

2626
- `ENDPOINT`: URL del punto final al que se enviarán los datos del reto diario.
27+
- `BOT_APIKEY`: Solo es necesario para nuestro caso que tenemos limitado nuestro endpoint para usuarios permitidos
2728

2829
### Pruebas Locales
2930

@@ -36,3 +37,4 @@ Este proyecto está configurado para desplegar automáticamente utilizando los f
3637
- `CLOUDFLARE_ACCOUNT_ID`: ID de tu cuenta de Cloudflare.
3738
- `CLOUDFLARE_API_TOKEN`: Token de API de Cloudflare.
3839
- `ENDPOINT`: URL de la API a la que se enviarán los datos del reto diario.
40+
- `BOT_APIKEY`: Solo es necesario para nuestro caso que tenemos limitado nuestro endpoint para usuarios permitidos

README_EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ To build and deploy this project, you will need the following:
2424
### Environment Variables
2525

2626
- `ENDPOINT`: URL of the endpoint where the daily challenge data will be sent.
27+
- `BOT_APIKEY`: Only necessary for our case that we have limited our endpoint for allowed users.
2728

2829
### Local Testing
2930

@@ -36,3 +37,4 @@ This project is configured to deploy automatically using GitHub Actions workflow
3637
- `CLOUDFLARE_ACCOUNT_ID`: Your Cloudflare account ID.
3738
- `CLOUDFLARE_API_TOKEN`: Your Cloudflare API token.
3839
- `ENDPOINT`: URL of the API to which the daily challenge data will be sent.
40+
- `BOT_APIKEY`: Only necessary for our case that we have limited our endpoint for allowed users.

src/cangrebot.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ use worker::{console_debug, console_warn};
44

55
use crate::challenge::DailyChallenge;
66

7-
pub async fn set_daily(endpoint: String, day: i64, challenge: DailyChallenge, client: &Client) {
7+
pub async fn set_daily(
8+
endpoint: &str,
9+
apikey: &str,
10+
day: i64,
11+
challenge: DailyChallenge,
12+
client: &Client,
13+
) {
814
let req = json!({
915
"title": format!("Reto #{day} - {}", challenge.question.title),
1016
"message": challenge.to_string(),
@@ -14,6 +20,7 @@ pub async fn set_daily(endpoint: String, day: i64, challenge: DailyChallenge, cl
1420
let res = client
1521
.post(endpoint)
1622
.header("content-type", "application/json")
23+
.header("Authorization", apikey)
1724
.body(serde_json::to_string(&req).unwrap())
1825
.send()
1926
.await

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub async fn main(_e: ScheduledEvent, env: Env, _ctx: ScheduleContext) {
1818
std::panic::set_hook(Box::new(|info: &std::panic::PanicInfo| {
1919
console_error!("{info}")
2020
}));
21+
let bot_key = env
22+
.secret("BOT_APIKEY")
23+
.map(|e| e.to_string())
24+
.expect("Bot APIKEY Secret not found");
2125

2226
// calculate days
2327
// its used for enumerate daily challenges
@@ -53,5 +57,5 @@ pub async fn main(_e: ScheduledEvent, env: Env, _ctx: ScheduleContext) {
5357

5458
console_debug!("Challenge response: {challenge:?}");
5559

56-
set_daily(endpoint, days, challenge, &client).await;
60+
set_daily(&endpoint, &bot_key, days, challenge, &client).await;
5761
}

0 commit comments

Comments
 (0)