This commit is contained in:
Felix Ableitner 2019-11-15 18:10:56 +01:00
parent fb274df81f
commit 5417fa63ff
2 changed files with 45 additions and 55 deletions

View file

@ -7,10 +7,11 @@ use actix_files::NamedFile;
use actix_web::*; use actix_web::*;
use actix_web_actors::ws; use actix_web_actors::ws;
use lemmy_server::db::establish_connection; use lemmy_server::db::establish_connection;
use lemmy_server::nodeinfo;
use lemmy_server::websocket::server::*; use lemmy_server::websocket::server::*;
use std::env; use std::env;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use lemmy_server::nodeinfo; use actix_web::http::header::ContentType;
embed_migrations!(); embed_migrations!();
@ -190,6 +191,7 @@ fn main() {
// Start chat server actor in separate thread // Start chat server actor in separate thread
let server = ChatServer::default().start(); let server = ChatServer::default().start();
// Create Http server with websocket support // Create Http server with websocket support
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.data(server.clone()) .data(server.clone())
@ -199,6 +201,7 @@ fn main() {
// static resources // static resources
.service(actix_files::Files::new("/static", front_end_dir())) .service(actix_files::Files::new("/static", front_end_dir()))
.route("/nodeinfo/2.0.json", web::get().to(nodeinfo::node_info)) .route("/nodeinfo/2.0.json", web::get().to(nodeinfo::node_info))
.route("/.well-known/nodeinfo", web::get().to(nodeinfo::node_info_well_known))
}) })
.bind("0.0.0.0:8536") .bind("0.0.0.0:8536")
.unwrap() .unwrap()

View file

@ -1,60 +1,47 @@
use actix_web::web::Json;
use serde::Serialize;
use crate::db::establish_connection;
use crate::db::community_view::SiteView; use crate::db::community_view::SiteView;
use actix_web::*; use crate::db::establish_connection;
use failure::Error;
use crate::version; use crate::version;
use crate::Settings;
use actix_web::HttpResponse;
use actix_web::body::Body;
use serde_json::json;
#[derive(Serialize)] pub fn node_info_well_known() -> HttpResponse<Body> {
pub struct Software { let json = json!({
name: String, "links": {
version: String, "rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
"href": format!("https://{}/nodeinfo/2.0.json", Settings::get().hostname),
}
});
return HttpResponse::Ok()
.content_type("application/json")
.body(json.to_string());
} }
#[derive(Serialize)] pub fn node_info() -> HttpResponse<Body> {
#[serde(rename_all = "camelCase")] let conn = establish_connection();
pub struct Usage { let site_view = match SiteView::read(&conn) {
users: Users, Ok(site_view) => site_view,
local_posts: i64, Err(_e) => return HttpResponse::InternalServerError().finish(),
local_comments: i64, };
} let json = json!({
"version": "2.0",
#[derive(Serialize)] "software": {
pub struct Users { "name": "lemmy",
total: i64, "version": version::VERSION,
} },
"protocols": [],
#[derive(Serialize)] "usage": {
#[serde(rename_all = "camelCase")] "users": {
pub struct NodeInfo { "total": site_view.number_of_users
version: String, },
software: Software, "local_posts": site_view.number_of_posts,
protocols: [String; 0], "local_comments": site_view.number_of_comments,
usage: Usage, "open_registrations": true,
open_registrations: bool, }
} });
return HttpResponse::Ok()
pub fn node_info() -> Result<Json<NodeInfo>, Error> { .content_type("application/json")
let conn = establish_connection(); .body(json.to_string());
let site_view = match SiteView::read(&conn) {
Ok(site_view) => site_view,
Err(_e) => return Err(_e)?,
};
let json = Json(NodeInfo {
version: String::from("2.0"),
software: Software {
name: String::from("lemmy"),
version: String::from(version::VERSION),
},
protocols: [], // TODO: put 'activitypub' once that is implemented
usage: Usage {
users: Users {
total: site_view.number_of_users,
},
local_posts: site_view.number_of_posts,
local_comments: site_view.number_of_comments,
},
open_registrations: true });
return Ok(json);
} }