Browse Source

Logging to redis.

master
Ashley 1 year ago
parent
commit
07e56501bc
7 changed files with 140 additions and 10 deletions
  1. 44
    0
      Cargo.lock
  2. 3
    1
      Cargo.toml
  3. 6
    2
      Settings.toml
  4. 1
    0
      src/controller.rs
  5. 60
    0
      src/logger.rs
  6. 24
    5
      src/main.rs
  7. 2
    2
      templates/index.html

+ 44
- 0
Cargo.lock View File

@@ -117,6 +117,11 @@ dependencies = [
]

[[package]]
name = "ascii"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "askama"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -257,6 +262,18 @@ dependencies = [
]

[[package]]
name = "combine"
version = "3.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ascii 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "config"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -362,6 +379,11 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "either"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "encoding"
version = "0.2.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1031,6 +1053,22 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "redis"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bytes 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
"combine 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
"sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-codec 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-executor 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-tcp 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "redox_syscall"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1198,12 +1236,14 @@ version = "0.1.0"
dependencies = [
"actix-web 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"askama 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"config 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"log4rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
"redis 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)",
]

@@ -1679,6 +1719,7 @@ dependencies = [
"checksum aho-corasick 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c1c6d463cbe7ed28720b5b489e7c083eeb8f90d08be2a0d6bb9e1ffea9ce1afa"
"checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5"
"checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef"
"checksum ascii 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ae7d751998c189c1d4468cf0a39bb2eae052a9c58d50ebb3b9591ee3813ad50"
"checksum askama 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c796fa924bb44233b38ae23020bfb229244e61910648d5f7bdc3659fcdf0572e"
"checksum askama_derive 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "57968dab3a9512b88af88012896e5e1d353e314afd5d190a98d0d4762c8f0a11"
"checksum askama_shared 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a180253955cc7bc732fdd18fad8aa9f04a4891fc7aa9c6ce525c128a1de3a146"
@@ -1695,6 +1736,7 @@ dependencies = [
"checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3"
"checksum chrono 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e48d85528df61dc964aa43c5f6ca681a19cfa74939b2348d204bd08a981f2fb0"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum combine 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e67be864b6450c26fdb9242dee53a46fb9648d0b1a65521a6a1947b54fa011e"
"checksum config 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b5379dd8b3e7f488a31107d2c9586ce2ddbee2bc839201b3b38dbdf550351c1e"
"checksum cookie 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1465f8134efa296b4c19db34d909637cb2bf0f7aaf21299e23e18fa29ac557cf"
"checksum crossbeam 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "24ce9782d4d5c53674646a6a4c1863a21a8fc0cb649b3c94dfc16e45071dea19"
@@ -1706,6 +1748,7 @@ dependencies = [
"checksum crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "677d453a17e8bd2b913fa38e8b9cf04bcdbb5be790aa294f2389661d72036015"
"checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850"
"checksum dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6d301140eb411af13d3115f9a562c85cc6b541ade9dfa314132244aaee7489dd"
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
"checksum encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec"
"checksum encoding-index-japanese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91"
"checksum encoding-index-korean 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81"
@@ -1787,6 +1830,7 @@ dependencies = [
"checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd"
"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c"
"checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2"
"checksum redis 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2dde3e551c6f072b3c1feab259e2d85e5795dfb4a4e0dd1510f3a1269f3befcf"
"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1"
"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
"checksum regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5bbbea44c5490a1e84357ff28b7d518b4619a159fed5d25f6c1de2d19cc42814"

+ 3
- 1
Cargo.toml View File

@@ -16,4 +16,6 @@ num = "0.2.0"
config = "0.9"
log4rs = "0.8"
log = "0.4.4"
askama = "0.7"
askama = "0.7"
chrono = "0.4"
redis = "*"

+ 6
- 2
Settings.toml View File

@@ -1,6 +1,10 @@
base_http_url="http://cdn.shibe.online/"
base_https_url="https://cdn.shibe.online/"
base_http_url = "http://cdn.shibe.online/"
base_https_url = "https://cdn.shibe.online/"
port = 6767
log_level = "info"
redis_url = "redis://127.0.0.1/"
log_channel = "shibe_log"
log_console = true

[endpoints]
shibes = "content/shibes/*"

+ 1
- 0
src/controller.rs View File

@@ -76,6 +76,7 @@ fn ok(content: String) -> HttpResponse

fn error(mut res : HttpResponseBuilder, message: &'static str) -> HttpResponse
{
error!("sending error response: {}", message);
res
.content_encoding(ContentEncoding::Auto)
.content_type("application/json")

+ 60
- 0
src/logger.rs View File

@@ -0,0 +1,60 @@
extern crate log;

use log::{Log, Level, Metadata, Record, SetLoggerError};
use redis::{Commands, Connection, RedisResult};
use std::sync::Mutex;
use chrono::Utc;

struct Logger
{
level: Level,
conn: Mutex<Connection>,
console: bool,
channel: String,
}

impl Log for Logger
{
fn enabled(&self, metadata: &Metadata) -> bool
{
metadata.level() <= self.level
}

fn log(&self, record: &Record)
{
if self.enabled(record.metadata()) && self.console
{
let msg = json!({
"time": Utc::now().to_rfc3339(),
"level": record.level().to_string(),
"module": record.module_path().unwrap_or_default(),
"line": record.line(),
"args": record.args()
});

let conn = &*self.conn.lock().unwrap();
//let res : RedisResult<()> = conn.publish(&self.channel, msg.to_string());
let res : RedisResult<isize> = conn.lpush(&self.channel, msg.to_string());
res.ok();

println!(
"{} {:<5} [{}] {}",
Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
record.level().to_string(),
record.module_path().unwrap_or_default(),
record.args());
}
}

fn flush(&self)
{
}
}

pub fn init(conn: Connection, channel: String, console: bool, level: Level) -> Result<(), SetLoggerError>
{
let logger = Logger { conn: Mutex::new(conn), console, level, channel };
log::set_boxed_logger(Box::new(logger))?;
log::set_max_level(level.to_level_filter());
Ok(())
}

+ 24
- 5
src/main.rs View File

@@ -1,12 +1,15 @@
extern crate actix_web;
#[macro_use] extern crate askama;
extern crate chrono;
extern crate config;
#[macro_use] extern crate log;
extern crate log4rs;
extern crate rand;
extern crate redis;
#[macro_use] extern crate serde_json;
mod controller;
mod images;
mod rand_iter;
mod logger;

use actix_web::{server, App, HttpResponse, fs};
use actix_web::http::Method;
@@ -38,7 +41,7 @@ fn create_app(
let state = AppState { endpoints: endpoints, settings: settings };

App::with_state(state)
.middleware(Logger::default())
.middleware(Logger::new("%{X-Real-IP}i \"%r\" %s %b \"%{User-Agent}i\" %Dms"))
.resource(
"/",
|r| r.method(Method::GET).f(Controller::index))
@@ -53,15 +56,31 @@ fn create_app(

fn main()
{
log4rs::init_file("log4rs.yaml", Default::default()).unwrap();
info!("starting server");

let mut settings_file = Config::default();

settings_file
.merge(config::File::with_name("Settings")).unwrap()
.merge(config::Environment::with_prefix("SHIBE")).unwrap();

let default_level = "trace";
let level =
settings_file
.get_str("log_level")
.unwrap_or(default_level.to_owned())
.parse::<log::Level>()
.unwrap();

let console_enabled = settings_file.get_bool("log_console").unwrap();
let conn_str = settings_file.get_str("redis_url").unwrap();
let channel = settings_file.get_str("log_channel").unwrap();
let client = redis::Client::open(conn_str.as_str()).unwrap();
let conn = client.get_connection().unwrap();

logger::init(conn, channel, console_enabled, level).unwrap();

info!("connected to redis on {}", conn_str);
info!("starting server");

let settings = AppParams {
http_url: settings_file.get_str("base_http_url").unwrap(),
https_url: settings_file.get_str("base_https_url").unwrap(),

+ 2
- 2
templates/index.html View File

@@ -12,7 +12,7 @@
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="shibe.online - the shiba inu api" />
<meta name="twitter:description" content="shibes as a service" />
<meta name="twitter:image" content="{{ dog }}" />
<meta name="twitter:image" content="{{ dog|safe }}" />
<meta name="description" content="shibes as a service" />
</head>
<body>
@@ -90,7 +90,7 @@ http://shibe.online/api/shibes?count=[1-100]&urls=[true/false]&httpsUrls=[true/f
That's it! Have a nice day. Here's a shibe:
</p>
<p class="shibe__image__container">
<img class="shibe__image" src="{{ dog }}" alt="A shibe." />
<img class="shibe__image" src="{{ dog|safe }}" alt="A shibe." />
</p>
<p>
Service by @commie@cybre.space.

Loading…
Cancel
Save