1
- use actix_web:: { web, App , HttpServer , Responder } ;
2
- use chrono:: Local ;
1
+ use actix_cors:: Cors ;
2
+ use actix_files:: Files ;
3
+ use actix_web:: {
4
+ get, http,
5
+ web:: { self , ServiceConfig } ,
6
+ Responder ,
7
+ } ;
3
8
use config:: { ENDPOINT , ENV } ;
4
- use env_logger:: Builder ;
5
- use log:: LevelFilter ;
6
- use std:: io:: Write ;
9
+ use shuttle_actix_web:: ShuttleActixWeb ;
7
10
8
11
mod fetching;
9
12
13
+ #[ get( "/github" ) ]
10
14
async fn github ( ) -> impl Responder {
11
15
web:: Json (
12
16
fetching:: github:: fetch_newest ( ENV . username . github , 10 )
@@ -15,6 +19,7 @@ async fn github() -> impl Responder {
15
19
)
16
20
}
17
21
22
+ #[ get( "/lastfm" ) ]
18
23
async fn lastfm ( ) -> impl Responder {
19
24
web:: Json (
20
25
fetching:: lastfm:: fetch_newest ( ENV . username . lastfm , ENV . key . lastfm , 10 )
@@ -23,6 +28,7 @@ async fn lastfm() -> impl Responder {
23
28
)
24
29
}
25
30
31
+ #[ get( "/goodreads" ) ]
26
32
async fn goodreads ( ) -> impl Responder {
27
33
web:: Json (
28
34
fetching:: goodreads:: fetch_newest ( ENV . link . goodreads , 10 )
@@ -31,6 +37,7 @@ async fn goodreads() -> impl Responder {
31
37
)
32
38
}
33
39
40
+ #[ get( "/letterboxd" ) ]
34
41
async fn letterboxd ( ) -> impl Responder {
35
42
web:: Json (
36
43
fetching:: letterboxd:: fetch_newest ( ENV . username . letterboxd , 4 )
@@ -39,32 +46,26 @@ async fn letterboxd() -> impl Responder {
39
46
)
40
47
}
41
48
42
- #[ actix_web:: main]
43
- async fn main ( ) -> std:: io:: Result < ( ) > {
44
- Builder :: new ( )
45
- . format ( |buf, record| {
46
- writeln ! (
47
- buf,
48
- "{} [{}] - {}" ,
49
- Local :: now( ) . format( "%Y-%m-%dT%H:%M:%S" ) ,
50
- record. level( ) ,
51
- record. args( )
52
- )
53
- } )
54
- . filter ( None , LevelFilter :: Info )
55
- . init ( ) ;
49
+ #[ allow( clippy:: unused_async) ]
50
+ #[ shuttle_runtime:: main]
51
+ async fn main ( ) -> ShuttleActixWeb < impl FnOnce ( & mut ServiceConfig ) + Send + Clone + ' static > {
52
+ let config = move |cfg : & mut ServiceConfig | {
53
+ let cors = Cors :: default ( )
54
+ . allowed_origin ( ENDPOINT . base )
55
+ . allowed_methods ( vec ! [ "GET" , "POST" ] )
56
+ . allowed_headers ( vec ! [ http:: header:: CONTENT_TYPE ] )
57
+ . max_age ( 3600 ) ;
56
58
57
- log:: info!( "Server opened on {}" , ENDPOINT . base) ;
59
+ cfg. service (
60
+ web:: scope ( "/api" )
61
+ . wrap ( cors)
62
+ . service ( github)
63
+ . service ( lastfm)
64
+ . service ( goodreads)
65
+ . service ( letterboxd) ,
66
+ ) ;
67
+ cfg. service ( Files :: new ( "/" , "frontend/dist" ) . index_file ( "index.html" ) ) ;
68
+ } ;
58
69
59
- HttpServer :: new ( || {
60
- App :: new ( )
61
- . route ( ENDPOINT . github , web:: get ( ) . to ( github) )
62
- . route ( ENDPOINT . lastfm , web:: get ( ) . to ( lastfm) )
63
- . route ( ENDPOINT . goodreads , web:: get ( ) . to ( goodreads) )
64
- . route ( ENDPOINT . letterboxd , web:: get ( ) . to ( letterboxd) )
65
- . service ( actix_files:: Files :: new ( "/" , "../frontend/dist" ) . index_file ( "index.html" ) )
66
- } )
67
- . bind ( ENDPOINT . base ) ?
68
- . run ( )
69
- . await
70
+ Ok ( config. into ( ) )
70
71
}
0 commit comments