@@ -6,10 +6,16 @@ import { stringify } from "jsr:@std/csv/stringify";
6
6
7
7
const kv = await Deno . openKv ( Deno . env . get ( "KV_STORE" ) || undefined ) ;
8
8
9
- Deno . cron ( "Download wind data" , "*/10 * * * *" , async ( ) => {
9
+ Deno . cron (
10
+ "Download wind data" ,
11
+ `*/${ Deno . env . get ( "CRON_INTERVAL" ) || "1" } * * * *` ,
12
+ async ( ) => {
13
+ await downloadNextStationWindData ( ) ;
14
+ }
15
+ ) ;
16
+ if ( Deno . env . get ( "INITIAL_DOWNLOAD" ) === "true" ) {
10
17
await downloadNextStationWindData ( ) ;
11
- } ) ;
12
- await downloadNextStationWindData ( ) ;
18
+ }
13
19
14
20
async function downloadNextStationWindData ( ) {
15
21
const trackedStations =
@@ -74,12 +80,36 @@ async function downloadWindData(stationId: number) {
74
80
75
81
const router = new Router ( ) ;
76
82
83
+ router . post ( "/login" , async ( ctx ) => {
84
+ const body = await ctx . request . body . text ( ) ;
85
+ const formData = new URLSearchParams ( body ) ;
86
+ const password = formData . get ( "password" ) ;
87
+
88
+ if ( password === ( Deno . env . get ( "PASSWORD" ) || "" ) ) {
89
+ ctx . response . status = 200 ;
90
+ } else {
91
+ ctx . response . status = 401 ;
92
+ ctx . response . body = "Incorrect password" ;
93
+ }
94
+ } ) ;
95
+
77
96
router . get ( "/tracked-stations" , async ( ctx ) => {
78
97
const trackedStations = ( await kv . get < number [ ] > ( [ "trackedStations" ] ) ) . value ;
79
98
ctx . response . body = trackedStations ?? [ ] ;
80
99
} ) ;
81
100
82
101
router . post ( "/tracked-stations" , async ( ctx ) => {
102
+ if (
103
+ ( ctx . request . headers . get ( "Authorization" ) ?? "" ) !==
104
+ ( Deno . env . get ( "PASSWORD" ) ?? "" )
105
+ ) {
106
+ ctx . response . status = 401 ;
107
+ ctx . response . body = {
108
+ msg : "Unauthorized" ,
109
+ } ;
110
+ return ;
111
+ }
112
+
83
113
try {
84
114
const body = await ctx . request . body . text ( ) ;
85
115
const formData = new URLSearchParams ( body ) ;
@@ -96,11 +126,11 @@ router.post("/tracked-stations", async (ctx) => {
96
126
ctx . response . body = { msg : "Saved Successfully" , trackedStations } ;
97
127
} else {
98
128
ctx . response . status = 500 ;
99
- ctx . response . body = "Failed to save tracked stations" ;
129
+ ctx . response . body = { msg : "Failed to save tracked stations" } ;
100
130
}
101
131
} catch ( err ) {
102
132
ctx . response . status = 500 ;
103
- ctx . response . body = "Failed to save tracked stations" ;
133
+ ctx . response . body = { msg : "Failed to save tracked stations" } ;
104
134
}
105
135
} ) ;
106
136
@@ -155,7 +185,7 @@ router.get("/wind-history/:stationId/csv", async (ctx) => {
155
185
} ) ;
156
186
157
187
const app = new Application ( ) ;
158
- app . use ( oakCors ( { origin : "*" } ) ) ;
188
+ app . use ( oakCors ( { origin : Deno . env . get ( "ORIGIN" ) || "*" } ) ) ;
159
189
app . use ( router . routes ( ) ) ;
160
190
app . use ( router . allowedMethods ( ) ) ;
161
191
0 commit comments