-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpodrs.rs
846 lines (778 loc) · 25.4 KB
/
gpodrs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
use anyhow::anyhow;
use base64::engine::general_purpose::STANDARD as BASE64;
use base64::Engine;
use log;
use serde::{Deserialize, Serialize};
use serde_json;
use std::time::{Duration, SystemTime as Time};
use tide::{Request, Response};
// https://github.com/bohwaz/micro-gpodder-server
// https://github.com/ahgamut/rust-ape-example
fn default<T: std::default::Default>() -> T {
T::default()
}
fn now() -> u64 {
Time::now()
.duration_since(Time::UNIX_EPOCH)
.unwrap()
.as_secs()
}
mod timestamp {
use std::ffi::{c_char, c_long};
use std::time::{Duration, SystemTime as Time};
const ISO_UTC_FMT: *const i8 = "%FT%T\0".as_ptr().cast();
#[repr(C)]
#[allow(non_camel_case_types)]
struct tm {
tm_sec: i32,
tm_min: i32,
tm_hour: i32,
tm_mday: i32,
tm_mon: i32,
tm_year: i32,
tm_wday: i32,
tm_yday: i32,
tm_isdst: i32,
tm_gmtoff: i64,
tm_zone: *const c_char,
}
extern "C" {
static timezone: c_long;
fn tzset();
fn strftime(buf: *mut c_char, max: isize, format: *const c_char, tm: *const tm) -> isize;
fn strptime(s: *const c_char, format: *const c_char, tm: *mut tm) -> *const c_char;
fn gmtime(timep: *const c_long) -> *mut tm;
fn mktime(tm: *mut tm) -> c_long;
}
pub fn init() {
std::env::set_var("TZ", "UTC");
unsafe { tzset() };
assert_eq!(
unsafe { timezone },
0,
"timezone should be set to 0 seconds from UTC"
);
}
pub fn serialize<S: serde::Serializer>(t: &Time, s: S) -> Result<S::Ok, S::Error> {
let epoch_time = t.duration_since(Time::UNIX_EPOCH).unwrap();
let epoch_secs = epoch_time.as_secs();
let mut ret = String::from("1994-05-06T07:08:09Z_");
unsafe {
let tm = gmtime(&(epoch_secs as i64));
let len = strftime(
ret.as_mut_ptr() as *mut i8,
ret.capacity() as isize,
ISO_UTC_FMT,
tm,
);
ret.truncate(len as usize);
}
s.serialize_str(&ret)
}
struct TimestampVisitor;
impl<'de> serde::de::Visitor<'de> for TimestampVisitor {
type Value = Time;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("ISO timestamp")
}
fn visit_str<E: serde::de::Error>(self, s: &str) -> Result<Self::Value, E> {
if s.is_empty() {
return Ok(Time::UNIX_EPOCH);
}
let c_s = format!("{s}\0");
let c_s_len = c_s.len();
let c_s: *const i8 = c_s.as_ptr().cast();
unsafe {
let mut tm: tm = std::mem::zeroed();
let ret = strptime(c_s, ISO_UTC_FMT, &mut tm);
if ret.is_null() || ret.offset_from(c_s) + 1 != c_s_len as isize {
log::error!(
"strptime({:?}, ISO_FMT, ...) = {:?} doesn't match expected {:?}+{:x}",
c_s,
ret,
c_s,
c_s_len
);
return Err(E::invalid_value(serde::de::Unexpected::Str(s), &self));
}
let epoch_secs = mktime(&mut tm);
if epoch_secs == -1 {
log::error!("error calling mktime");
return Err(E::invalid_value(serde::de::Unexpected::Str(s), &self));
}
Ok(Time::UNIX_EPOCH
.checked_add(Duration::from_secs(epoch_secs as u64))
.unwrap())
}
}
fn visit_u64<E: serde::de::Error>(self, n: u64) -> Result<Self::Value, E> {
Ok(Time::UNIX_EPOCH
.checked_add(Duration::from_secs(n))
.unwrap())
}
}
pub fn deserialize<'de, D: serde::Deserializer<'de>>(dsr: D) -> Result<Time, D::Error> {
dsr.deserialize_str(TimestampVisitor)
}
}
#[derive(Clone, Default, PartialEq, Serialize, Deserialize)]
struct Podcast {
url: String,
#[serde(skip_serializing_if = "String::is_empty", default)]
title: String,
#[serde(skip_serializing_if = "String::is_empty", default)]
author: String,
#[serde(skip_serializing_if = "String::is_empty", default)]
description: String,
#[serde(skip_serializing_if = "String::is_empty", default)]
website: String,
#[serde(skip_serializing_if = "String::is_empty", default)]
logo_url: String,
}
fn minus_opt<'de, D: serde::Deserializer<'de>>(dsr: D) -> Result<Option<u64>, D::Error> {
struct MinusOptVisitor;
impl<'de> serde::de::Visitor<'de> for MinusOptVisitor {
type Value = Option<u64>;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "u64 or -1")
}
fn visit_unit<E>(self) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(None)
}
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Some(v))
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if v == -1 {
Ok(None)
} else {
Err(E::invalid_value(serde::de::Unexpected::Signed(v), &self))
}
}
}
dsr.deserialize_any(MinusOptVisitor)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(tag = "action", rename_all = "lowercase")]
enum Action {
Add,
Remove,
Download,
Delete,
Play {
#[serde(deserialize_with = "minus_opt")]
started: Option<u64>,
#[serde(deserialize_with = "minus_opt")]
position: Option<u64>,
#[serde(deserialize_with = "minus_opt")]
total: Option<u64>,
},
New,
Flattr,
}
#[derive(Clone, PartialEq, Eq, Ord, Serialize, Deserialize)]
struct Event {
podcast: String,
#[serde(default)]
episode: String,
device: String,
#[serde(flatten)]
action: Action,
#[serde(with = "timestamp")]
timestamp: Time,
}
impl std::cmp::PartialOrd for Event {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
(
self.timestamp,
&self.podcast,
&self.episode,
&self.action,
&self.device,
)
.partial_cmp(&(
other.timestamp,
&other.podcast,
&other.episode,
&other.action,
&other.device,
))
}
}
#[derive(Default, Clone, PartialEq, Serialize, Deserialize)]
struct Device {
#[serde(default)]
id: String,
#[serde(default)]
caption: String,
#[serde(default)]
r#type: String,
#[serde(default)]
subscriptions: u64,
}
#[derive(Default, Clone, Serialize, Deserialize, PartialEq)]
struct UserData {
username: String,
password: String,
podcasts: Vec<Podcast>,
events: Vec<Event>,
devices: Vec<Device>,
devsubs: Vec<DevSubDiff>,
}
fn userdata<R>(f: impl FnOnce(&mut UserData) -> tide::Result<R>) -> tide::Result<R> {
use std::io::{Seek, SeekFrom};
let mut cfg = std::env::var("GPODRS_CONFIG_DIR").unwrap_or("./".to_string());
if !cfg.ends_with('/') {
cfg.push('/');
}
cfg.push_str("shua.json");
log::info!("user config: {cfg}");
let mut datafile = std::fs::File::options()
.read(true)
.write(true)
.create(false)
.open(cfg)?;
let userdata: UserData = serde_json::from_reader(&datafile)?;
let mut data2 = userdata.clone();
let ret = f(&mut data2)?;
if userdata != data2 {
datafile.seek(SeekFrom::Start(0))?;
serde_json::to_writer_pretty(&datafile, &data2)?;
let flen = datafile.stream_position()?;
datafile.set_len(flen)?;
}
Ok(ret)
}
async fn todo(mut req: Request<()>) -> tide::Result {
let body: String = req.body_string().await?;
log::info!("TODO: {body:?}");
Ok(Response::builder(501).body("not implemented yet").build())
}
// auth
async fn auth_login(mut req: Request<()>) -> tide::Result {
let path_username = req.param("username")?;
if let Some(sess_username) = req.session().get::<String>("username") {
// check sessionid
if sess_username != path_username {
return Err(tide::Error::new(
400,
anyhow!("session username is not valid for authenticated user"),
));
}
return Ok("".into());
}
// normal auth flow
let auth_hdr = req
.header("Authorization")
.map(|hdrs| hdrs.last())
.ok_or(tide::Error::new(
401,
anyhow!("authorization header not present"),
))?;
if !auth_hdr.as_str().starts_with("Basic ") {
return Err(tide::Error::new(
401,
anyhow!("authorize header is not Basic"),
));
}
let auth_hdr = BASE64
.decode(&auth_hdr.as_str()["Basic ".len()..])
.map_err(|e| tide::Error::new(401, e))?;
let auth_hdr = String::from_utf8(auth_hdr).map_err(|e| tide::Error::new(401, e))?;
let (auth_username, auth_password) = (auth_hdr
.find(":")
.map(|i| auth_hdr.split_at(i))
.map(|(u, p)| (u, &p[1..])))
.ok_or(tide::Error::new(
401,
anyhow!("authorize header is not valid basic auth"),
))?;
if auth_username != path_username {
return Err(tide::Error::new(
401,
anyhow!("login username does not match path resource"),
));
}
let auth_password_hash = {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(auth_password);
let hash = hasher.finalize();
let mut ret = String::new();
for b in hash {
ret.push_str(&format!("{b:02x}"));
}
ret
};
log::info!("password hash: {auth_password_hash}");
match userdata(|userdata| {
Ok(path_username == ""
|| userdata.username != auth_username
|| userdata.password != auth_password_hash)
}) {
Err(err) => {
if let Some(ioerr) = err.downcast_ref::<std::io::Error>() {
if ioerr.kind() == std::io::ErrorKind::NotFound {
log::error!("{ioerr}");
return Err(tide::Error::new(
401,
anyhow!("user {auth_username} does not exist"),
));
}
}
return Err(err);
}
Ok(false) => {}
Ok(true) => {
return Err(tide::Error::new(
401,
anyhow!("unable to authenticate: {auth_username:?} {auth_password:?}"),
));
}
}
req.session_mut().insert("username", auth_username)?;
Ok("".into())
}
async fn auth_logout(mut req: Request<()>) -> tide::Result {
let path_username = req.param("username")?;
if let Some(sess_username) = req.session().get::<String>("username") {
if path_username != sess_username {
return Err(tide::Error::new(
400,
anyhow!("session user does not match path resource"),
));
}
}
req.session_mut().remove("username");
Ok("".into())
}
fn split_suffix(s: &str) -> (&str, &str) {
match s.rfind('.') {
Some(i) => (&s[..i], &s[i + 1..]),
None => (s, ""),
}
}
fn assert_format(f: &str) -> tide::Result<()> {
if f != "json" {
Err(tide::Error::new(400, anyhow!("no format specified")))
} else {
Ok(())
}
}
// devices
async fn list_devices(req: Request<()>) -> tide::Result {
let path_username = req.param("username")?;
let (path_username, format) = split_suffix(path_username);
assert_format(format)?;
assert_auth(&req, Some(path_username))?;
let body_json = userdata(|userdata| Ok(serde_json::to_string(&userdata.devices)?))?;
Ok(Response::builder(200)
.body(body_json)
.content_type("application/json")
.build())
}
async fn update_device(mut req: Request<()>) -> tide::Result {
let path_username = req.param("username")?;
assert_auth(&req, Some(path_username))?;
let path_deviceid = req.param("deviceid")?;
let (path_deviceid, format) = split_suffix(&path_deviceid);
let path_deviceid = path_deviceid.to_string();
assert_format(format)?;
let mut device: Device = req.body_json().await?;
device.id = path_deviceid;
if device.id != "" {
userdata(|userdata| {
if let Some(dev) = userdata.devices.iter_mut().find(|d| d.id == device.id) {
if device.caption != "" {
dev.caption = device.caption;
}
if device.r#type != "" {
dev.r#type = device.r#type;
}
if device.subscriptions != 0 {
dev.subscriptions = device.subscriptions;
}
} else {
userdata.devices.push(device);
}
Ok(())
})?;
}
Ok("".into())
}
// subscriptions
async fn get_subscriptions(req: Request<()>) -> tide::Result {
let (path_username, _path_deviceid, format) =
match (req.param("username")?, req.param("deviceid")) {
(u, Ok(d)) => {
let (d, f) = split_suffix(d);
(u, Some(d), f)
}
(u, Err(_)) => {
let (u, f) = split_suffix(u);
(u, None, f)
}
};
assert_format(format)?;
assert_auth(&req, Some(path_username))?;
let subs = userdata(|userdata| Ok(serde_json::to_string(&userdata.podcasts)?))?;
Ok(Response::builder(200)
.content_type("application/json")
.body(subs)
.build())
}
fn assert_auth<T>(req: &Request<T>, path_username: Option<&str>) -> tide::Result<()> {
match (req.session().get::<String>("username"), path_username) {
(Some(sess_username), Some(path_username)) => {
if sess_username != path_username {
Err(tide::Error::new(
401,
anyhow!("authenticated user does not have access to requested user's data"),
))
} else {
Ok(())
}
}
(Some(_sess_username), None) => Ok(()),
(None, _) => Err(tide::Error::new(
401,
anyhow!("request is not authenticated"),
)),
}
}
fn sanitize_urls<'s>(urls: impl Iterator<Item = &'s mut String>) -> Vec<[String; 2]> {
let mut ret = vec![];
for u in urls {
if let Some(i) = u.find('?') {
let orig = u.clone();
u.truncate(i);
*u = u.trim().to_string();
ret.push([orig, u.clone()]);
}
}
ret
}
async fn put_subscriptions(mut req: Request<()>) -> tide::Result {
let path_username = req.param("username")?;
assert_auth(&req, Some(path_username))?;
let path_deviceid = req.param("deviceid")?;
let (path_deviceid, format) = split_suffix(path_deviceid);
assert_format(format)?;
let path_deviceid = path_deviceid.to_string();
let mut subs: Vec<Podcast> = req.body_json().await?;
sanitize_urls(subs.iter_mut().map(|s| &mut s.url));
userdata(|userdata| {
userdata.devsubs.push(DevSubDiff {
deviceid: path_deviceid,
diff: SubDiff {
add: subs.iter().map(|p| p.url.clone()).collect(),
remove: userdata.podcasts.iter().map(|p| p.url.clone()).collect(),
timestamp: now(),
},
});
userdata.podcasts = subs;
Ok(())
})?;
Ok("".into())
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
struct SubDiff {
#[serde(default)]
add: Vec<String>,
#[serde(default)]
remove: Vec<String>,
#[serde(default)]
timestamp: u64,
}
#[derive(Clone, PartialEq, Eq, Deserialize, Serialize)]
struct DevSubDiff {
deviceid: String,
#[serde(flatten)]
diff: SubDiff,
}
impl PartialOrd for DevSubDiff {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
(
self.diff.timestamp,
&self.deviceid,
&self.diff.add,
&self.diff.remove,
)
.partial_cmp(&(
other.diff.timestamp,
&other.deviceid,
&other.diff.add,
&other.diff.remove,
))
}
}
impl Ord for DevSubDiff {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).unwrap_or(std::cmp::Ordering::Equal)
}
}
#[derive(Serialize)]
struct UpdateUrls {
timestamp: u64,
update_urls: Vec<[String; 2]>,
}
async fn update_subscriptions(mut req: Request<()>) -> tide::Result {
let path_username = req.param("username")?;
let path_deviceid = req.param("deviceid")?;
assert_auth(&req, Some(path_username))?;
let (path_deviceid, format) = split_suffix(path_deviceid);
assert_format(format)?;
let path_deviceid = path_deviceid.to_string();
let mut diff: SubDiff = req.body_json().await?;
println!("body: {diff:?}");
let surls = sanitize_urls(diff.add.iter_mut().chain(diff.remove.iter_mut()));
if diff.add.len() == 0 && diff.remove.len() == 0 {
return Ok("".into());
}
if diff.add.iter().any(|u| diff.remove.contains(u)) {}
userdata(|userdata| {
let mut curdiff = DevSubDiff {
deviceid: path_deviceid,
diff: diff.clone(),
};
curdiff.diff.timestamp = now();
userdata.devsubs.push(curdiff);
userdata.podcasts.extend(diff.add.iter().map(|url| Podcast {
url: url.clone(),
..default()
}));
for url in diff.remove {
if let Some((i, _)) = (userdata.podcasts.iter().enumerate())
.filter(|(_, p)| p.url == url)
.next()
{
userdata.podcasts.swap_remove(i);
}
}
Ok(())
})?;
let body_json = serde_json::to_string(&UpdateUrls {
timestamp: now(),
update_urls: surls,
})?;
Ok(Response::builder(200)
.body(body_json)
.content_type("application/json")
.build())
}
async fn get_sub_changes(req: Request<()>) -> tide::Result {
let path_username = req.param("username")?;
assert_auth(&req, Some(path_username))?;
let path_deviceid = req.param("deviceid")?;
let (path_deviceid, format) = split_suffix(path_deviceid);
assert_format(format)?;
let mut since = 0;
for (k, v) in req.url().query_pairs() {
match k.as_ref() {
"since" => {
let epoch_secs = u64::from_str_radix(&v, 10)?;
since = epoch_secs;
}
_ => {}
}
}
let body_json = userdata(|userdata| {
let subdiff = userdata
.devsubs
.iter()
.filter(|devsub| devsub.diff.timestamp > since && devsub.deviceid == path_deviceid)
.map(|devsub| &devsub.diff)
.fold(
SubDiff {
timestamp: now(),
add: vec![],
remove: vec![],
},
|mut acc, diff| {
acc.add.extend(diff.add.iter().cloned());
acc.remove.extend(diff.remove.iter().cloned());
acc
},
);
Ok(serde_json::to_string(&subdiff)?)
})?;
Ok(Response::builder(200)
.body(body_json)
.content_type("application/json")
.build())
}
// Events
#[derive(Default, Serialize)]
struct EpisodeActions {
actions: Vec<Action>,
timestamp: u64,
}
async fn get_events(req: Request<()>) -> tide::Result {
let path_username = req.param("username")?;
let (path_username, format) = split_suffix(path_username);
assert_format(format)?;
assert_auth(&req, Some(path_username))?;
let mut since = 0;
let mut podcast = String::new();
let mut _aggregated = true;
for (k, v) in req.url().query_pairs() {
match k.as_ref() {
"since" => since = u64::from_str_radix(&v, 10)?,
"podcast" => podcast = v.to_string(),
"aggregated" => _aggregated = v.as_ref() == "true",
_ => {}
}
}
let since = Time::UNIX_EPOCH
.checked_add(Duration::from_secs(since))
.unwrap();
let body_json = userdata(|userdata| {
let evts = userdata
.events
.iter()
.filter(|evt| podcast == "" || evt.podcast == podcast)
.filter(|evt| evt.timestamp >= since);
let epacts = evts.fold(EpisodeActions::default(), |mut acc, evt| {
acc.actions.push(evt.action.clone());
acc.timestamp = evt
.timestamp
.duration_since(Time::UNIX_EPOCH)
.unwrap()
.as_secs();
acc
});
Ok(serde_json::to_string(&epacts)?)
})?;
Ok(Response::builder(200)
.body(body_json)
.content_type("application/json")
.build())
}
async fn post_events(mut req: Request<()>) -> tide::Result {
let path_username = req.param("username")?;
let (path_username, format) = split_suffix(path_username);
assert_format(format)?;
assert_auth(&req, Some(path_username))?;
let mut evts: Vec<Event> = match req.body_json().await {
Ok(evts) => evts,
Err(err) => {
log::debug!("unable to deserialize: {}", req.body_string().await?);
Err(err)?
}
};
let surls = sanitize_urls(evts.iter_mut().map(|evt| &mut evt.podcast));
userdata(|userdata| {
userdata.events.extend(evts);
userdata.events.sort();
Ok(())
})?;
let body_json = serde_json::to_string(&UpdateUrls {
timestamp: now(),
update_urls: surls,
})?;
Ok(Response::builder(200)
.body(body_json)
.content_type("application/json")
.build())
}
struct DebugPrintMiddleware;
#[tide::utils::async_trait]
impl<State: Clone + Send + Sync + 'static> tide::Middleware<State> for DebugPrintMiddleware {
async fn handle(
&self,
mut request: Request<State>,
next: tide::Next<'_, State>,
) -> tide::Result {
// if log::log_enabled!(log::Level::Debug) {
let req_body = request.body_string().await?;
let res = next.run(request).await;
log::info!("request body: {req_body}");
Ok(res)
// } else {
// Ok(next.run(request).await)
// }
}
}
#[async_std::main]
async fn main() {
timestamp::init();
femme::start();
let mut app = tide::new();
app.with(tide::log::LogMiddleware::new());
app.with(tide::sessions::SessionMiddleware::new(
tide::sessions::MemoryStore::new(),
std::env::var("GPODRS_SESSION_SECRET")
.expect("GPODRS_SESSION_SECRET must be set")
.as_bytes(),
));
// app.with(DebugPrintMiddleware);
// https://gpoddernet.readthedocs.io/en/latest/api/reference/clientconfig.html
app.at("/clientconfig.json").get(todo);
// https://gpoddernet.readthedocs.io/en/latest/api/reference/auth.html
app.at("/api/2/auth/:username/login.json").post(auth_login);
app.at("/api/2/auth/:username/logout.json")
.post(auth_logout);
// https://gpoddernet.readthedocs.io/en/latest/api/reference/devices.html
app.at("/api/2/devices/:username/:deviceid")
.post(update_device);
app.at("/api/2/devices/:username").get(list_devices);
app.at("/api/2/devices/:username/:deviceid.json").get(todo);
// https://gpoddernet.readthedocs.io/en/latest/api/reference/subscriptions.html
// all have format suffixes
app.at("/subscriptions/:username/:deviceid")
.get(get_subscriptions);
app.at("/subscriptions/:username").get(get_subscriptions);
app.at("/subscriptions/:username/:deviceid")
.put(put_subscriptions);
app.at("/api/2/subscriptions/:username/:deviceid")
.post(update_subscriptions);
app.at("/api/2/subscriptions/:username/:deviceid")
.get(get_sub_changes);
// https://gpoddernet.readthedocs.io/en/latest/api/reference/sync.html
app.at("/api/2/sync-devices/:username.json").get(todo);
app.at("/api/2/sync-devices/:username.json").post(todo);
// https://gpoddernet.readthedocs.io/en/latest/api/reference/events.html
app.at("/api/2/episodes/:username").post(post_events);
app.at("/api/2/episodes/:username").get(get_events);
let listen_addr = std::env::var("GPODRS_ADDR");
let listen_addr = listen_addr
.as_ref()
.map(|s| s.as_str())
.unwrap_or("localhost:3005");
app.listen(listen_addr).await.expect("listen");
}
#[test]
fn test_minus_opt() {
let act: Result<Action, _> =
serde_json::from_str(r#"{"action": "play", "started": -1, "position": -1, "total": -1}"#)
.map_err(|err| err.to_string());
assert_eq!(
Ok(Action::Play {
started: None,
position: None,
total: None
}),
act
);
let act: Result<Action, _> = serde_json::from_str(
r#"{"action": "play", "started": null, "position": null, "total": null}"#,
)
.map_err(|err| err.to_string());
assert_eq!(
Ok(Action::Play {
started: None,
position: None,
total: None
}),
act
);
}