]> Untitled Git - lemmy.git/blob - server/src/main.rs
basic, working rss feeds
[lemmy.git] / server / src / main.rs
1 extern crate lemmy_server;
2 #[macro_use]
3 extern crate diesel_migrations;
4
5 use actix::prelude::*;
6 use actix_files::NamedFile;
7 use actix_web::*;
8 use actix_web_actors::ws;
9 use lemmy_server::db::establish_connection;
10 <<<<<<< HEAD
11 use lemmy_server::nodeinfo;
12 =======
13 use lemmy_server::feeds;
14 >>>>>>> Implement RSS feeds (fixes #118)
15 use lemmy_server::websocket::server::*;
16 use std::env;
17 use std::time::{Duration, Instant};
18
19 embed_migrations!();
20
21 /// How often heartbeat pings are sent
22 const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
23 /// How long before lack of client response causes a timeout
24 const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
25
26 /// Entry point for our route
27 fn chat_route(
28   req: HttpRequest,
29   stream: web::Payload,
30   chat_server: web::Data<Addr<ChatServer>>,
31 ) -> Result<HttpResponse, Error> {
32   ws::start(
33     WSSession {
34       cs_addr: chat_server.get_ref().to_owned(),
35       id: 0,
36       hb: Instant::now(),
37       ip: req
38         .connection_info()
39         .remote()
40         .unwrap_or("127.0.0.1:12345")
41         .split(":")
42         .next()
43         .unwrap_or("127.0.0.1")
44         .to_string(),
45     },
46     &req,
47     stream,
48   )
49 }
50
51 struct WSSession {
52   cs_addr: Addr<ChatServer>,
53   /// unique session id
54   id: usize,
55   ip: String,
56   /// Client must send ping at least once per 10 seconds (CLIENT_TIMEOUT),
57   /// otherwise we drop connection.
58   hb: Instant,
59 }
60
61 impl Actor for WSSession {
62   type Context = ws::WebsocketContext<Self>;
63
64   /// Method is called on actor start.
65   /// We register ws session with ChatServer
66   fn started(&mut self, ctx: &mut Self::Context) {
67     // we'll start heartbeat process on session start.
68     self.hb(ctx);
69
70     // register self in chat server. `AsyncContext::wait` register
71     // future within context, but context waits until this future resolves
72     // before processing any other events.
73     // across all routes within application
74     let addr = ctx.address();
75     self
76       .cs_addr
77       .send(Connect {
78         addr: addr.recipient(),
79         ip: self.ip.to_owned(),
80       })
81       .into_actor(self)
82       .then(|res, act, ctx| {
83         match res {
84           Ok(res) => act.id = res,
85           // something is wrong with chat server
86           _ => ctx.stop(),
87         }
88         fut::ok(())
89       })
90       .wait(ctx);
91   }
92
93   fn stopping(&mut self, _ctx: &mut Self::Context) -> Running {
94     // notify chat server
95     self.cs_addr.do_send(Disconnect {
96       id: self.id,
97       ip: self.ip.to_owned(),
98     });
99     Running::Stop
100   }
101 }
102
103 /// Handle messages from chat server, we simply send it to peer websocket
104 /// These are room messages, IE sent to others in the room
105 impl Handler<WSMessage> for WSSession {
106   type Result = ();
107
108   fn handle(&mut self, msg: WSMessage, ctx: &mut Self::Context) {
109     // println!("id: {} msg: {}", self.id, msg.0);
110     ctx.text(msg.0);
111   }
112 }
113
114 /// WebSocket message handler
115 impl StreamHandler<ws::Message, ws::ProtocolError> for WSSession {
116   fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
117     // println!("WEBSOCKET MESSAGE: {:?} from id: {}", msg, self.id);
118     match msg {
119       ws::Message::Ping(msg) => {
120         self.hb = Instant::now();
121         ctx.pong(&msg);
122       }
123       ws::Message::Pong(_) => {
124         self.hb = Instant::now();
125       }
126       ws::Message::Text(text) => {
127         let m = text.trim().to_owned();
128         println!("WEBSOCKET MESSAGE: {:?} from id: {}", &m, self.id);
129
130         self
131           .cs_addr
132           .send(StandardMessage {
133             id: self.id,
134             msg: m,
135           })
136           .into_actor(self)
137           .then(|res, _, ctx| {
138             match res {
139               Ok(res) => ctx.text(res),
140               Err(e) => {
141                 eprintln!("{}", &e);
142               }
143             }
144             fut::ok(())
145           })
146           .wait(ctx);
147       }
148       ws::Message::Binary(_bin) => println!("Unexpected binary"),
149       ws::Message::Close(_) => {
150         ctx.stop();
151       }
152       _ => {}
153     }
154   }
155 }
156
157 impl WSSession {
158   /// helper method that sends ping to client every second.
159   ///
160   /// also this method checks heartbeats from client
161   fn hb(&self, ctx: &mut ws::WebsocketContext<Self>) {
162     ctx.run_interval(HEARTBEAT_INTERVAL, |act, ctx| {
163       // check client heartbeats
164       if Instant::now().duration_since(act.hb) > CLIENT_TIMEOUT {
165         // heartbeat timed out
166         println!("Websocket Client heartbeat failed, disconnecting!");
167
168         // notify chat server
169         act.cs_addr.do_send(Disconnect {
170           id: act.id,
171           ip: act.ip.to_owned(),
172         });
173
174         // stop actor
175         ctx.stop();
176
177         // don't try to send a ping
178         return;
179       }
180
181       ctx.ping("");
182     });
183   }
184 }
185
186 fn main() {
187   let _ = env_logger::init();
188   let sys = actix::System::new("lemmy");
189
190   // Run the migrations from code
191   let conn = establish_connection();
192   embedded_migrations::run(&conn).unwrap();
193
194   // Start chat server actor in separate thread
195   let server = ChatServer::default().start();
196   // Create Http server with websocket support
197
198   HttpServer::new(move || {
199     App::new()
200       .data(server.clone())
201       .service(web::resource("/api/v1/ws").to(chat_route))
202       //            .service(web::resource("/api/v1/rest").route(web::post().to(||{})))
203       .service(web::resource("/").to(index))
204       .route("/nodeinfo/2.0.json", web::get().to(nodeinfo::node_info))
205       .route(
206         "/.well-known/nodeinfo",
207         web::get().to(nodeinfo::node_info_well_known),
208       )
209       .route("/feeds/{type}/{name}.xml", web::get().to(feeds::get_feed))
210       // static resources
211       .service(actix_files::Files::new("/static", front_end_dir()))
212   })
213   .bind("0.0.0.0:8536")
214   .unwrap()
215   .start();
216
217   println!("Started http server: 0.0.0.0:8536");
218   let _ = sys.run();
219 }
220
221 fn index() -> Result<NamedFile, actix_web::error::Error> {
222   Ok(NamedFile::open(front_end_dir() + "/index.html")?)
223 }
224
225 fn front_end_dir() -> String {
226   env::var("LEMMY_FRONT_END_DIR").unwrap_or("../ui/dist".to_string())
227 }