diff --git a/.travis.yml b/.travis.yml index 99a1a8c..8cb4a48 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ matrix: env: global: - - RELEASE_ARCHIVE_NAME=fly-${TRAVIS_OS_NAME}-x64.tar.gz + - DNS_RELEASE_FILENAME=fly-dns-${TRAVIS_OS_NAME}-x64.gz cache: cargo: true yarn: true @@ -61,18 +61,19 @@ script: - cargo build --release --bin create_snapshot - ls -lah target/release - target/release/create_snapshot v8env/dist/v8env.js v8env.bin -- cargo build --release +- cargo build --bin dns --release before_deploy: -- strip target/release/server && strip target/release/dns -- tar czf ${RELEASE_ARCHIVE_NAME} -C target/release server dns +- strip target/release/dns +- cp target/release/dns fly-dns +- gzip -c fly-dns > ${DNS_RELEASE_FILENAME} deploy: provider: releases overwrite: true api_key: secure: VsCHyGpPc02F3LiK+nqMDSPSEKmzgKyAhwVJQJ2mLjso74N1NGRsCDgKIQAOoLzcJRVNZFrpnlbOxKoyTGg1x8b6MnpPmqR8j/LVdKoud6Nhp8Ufw1I/9XwPpyjAhSNKnQq+1h1DpbWbob4bS5BR23PRbIySPUx570W3G8y3IritqorvpM4Qn12fyetWoZxRxWt9QAnrEV2Pj8ULR1C2jBtQPNAVLKm5FcDPBxzQcqXRZjARQtdjNU5tOB1bEm4zg8QNl/hDHi+5WZ/FQ/gZR28cAdhAb8GTC8UU7hTP5TTt2G1UimI27RPxyAyc/eJ9JU2ixRGzjDPvv6hk+KkGGjvuJccKHqR+bgHJ8lS8gPPmpvRMM9uVTAsI/pPA51OgveaHknMCCefiDYGgMq2cIW3UmUMHaB1m9SmBrIqCfEOwPp8f4KEtO55B9myuvqOYvlL4pJrcSUucjNGkm7guqCR4u3L3D0cIkTyk3fTbZyaeAxkSlcYvcff8BpRuFI8YiTybtLxqy0+KoDNoXr/d34/dwFyQNeRPN7H4EWGkzFeHpbfm+aCXGtqF8fMj/tOT7yrOAwy2rvD9/Gx8nzdY7j+DMIXB9HAR3y5Xrpiba0nedV4jww2s1dKuf96Iac3CZrO1qOXvNMKPCVSrp/rmQ5r6REfTQDe6NBONPvjjTX0= - file: ${RELEASE_ARCHIVE_NAME} + file: ${DNS_RELEASE_FILENAME} skip_cleanup: true on: repo: superfly/fly.rs diff --git a/Cargo.lock b/Cargo.lock index 2fcee31..88ac702 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -372,20 +372,17 @@ dependencies = [ "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.10 (registry+https://github.com/rust-lang/crates.io-index)", - "hyper-tls 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper-tls 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy-static-include 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", "libfly 0.1.0", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2_redis 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2_sqlite 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "rusqlite 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", "sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "sourcemap 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -395,7 +392,6 @@ dependencies = [ "tokio-io-pool 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-signal 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-udp 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns 0.15.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns-proto 0.5.0-alpha.3 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns-server 0.15.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -529,13 +525,13 @@ dependencies = [ [[package]] name = "hyper-tls" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.10 (registry+https://github.com/rust-lang/crates.io-index)", - "native-tls 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "native-tls 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -745,14 +741,15 @@ dependencies = [ [[package]] name = "native-tls" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl 0.10.12 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.9.36 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.39 (registry+https://github.com/rust-lang/crates.io-index)", "schannel 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", "security-framework 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "security-framework-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -818,7 +815,7 @@ dependencies = [ [[package]] name = "openssl" -version = "0.10.12" +version = "0.10.15" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -826,7 +823,7 @@ dependencies = [ "foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", - "openssl-sys 0.9.36 (registry+https://github.com/rust-lang/crates.io-index)", + "openssl-sys 0.9.39 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -836,7 +833,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "openssl-sys" -version = "0.9.36" +version = "0.9.39" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1956,7 +1953,7 @@ dependencies = [ "checksum httparse 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b6288d7db100340ca12873fd4d08ad1b8f206a9457798dfb17c018a33fee540" "checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e" "checksum hyper 0.12.10 (registry+https://github.com/rust-lang/crates.io-index)" = "529d00e4c998cced1a15ffd53bbe203917b39ed6071281c16184ab0014ca6ff3" -"checksum hyper-tls 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "caaee4dea92794a9e697038bd401e264307d1f22c883dbcb6f6618ba0d3b3bd3" +"checksum hyper-tls 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "32cd73f14ad370d3b4d4b7dce08f69b81536c82e39fcc89731930fe5788cd661" "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum if_chain 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4bac95d9aa0624e7b78187d6fb8ab012b41d9f6f54b1bcb61e61c4845f8357ec" "checksum indexmap 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08173ba1e906efb6538785a8844dd496f5d34f0a2d88038e95195172fc667220" @@ -1982,7 +1979,7 @@ dependencies = [ "checksum mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "71646331f2619b1026cc302f87a2b8b648d5c6dd6937846a16cc8ce0f347f432" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" -"checksum native-tls 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8b0a7bd714e83db15676d31caf968ad7318e9cc35f93c85a90231c8f22867549" +"checksum native-tls 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ff8e08de0070bbf4c31f452ea2a70db092f36f6f2e4d897adf5674477d488fb2" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum nibble_vec 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c8d77f3db4bce033f4d04db08079b2ef1c3d02b44e86f25d08886fafa7756ffa" "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" @@ -1991,9 +1988,9 @@ dependencies = [ "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" -"checksum openssl 0.10.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5e2e79eede055813a3ac52fb3915caf8e1c9da2dec1587871aec9f6f7b48508d" +"checksum openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)" = "5e1309181cdcbdb51bc3b6bedb33dfac2a83b3d585033d3f6d9e22e8c1928613" "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" -"checksum openssl-sys 0.9.36 (registry+https://github.com/rust-lang/crates.io-index)" = "409d77eeb492a1aebd6eb322b2ee72ff7c7496b4434d98b3bf8be038755de65e" +"checksum openssl-sys 0.9.39 (registry+https://github.com/rust-lang/crates.io-index)" = "278c1ad40a89aa1e741a1eed089a2f60b18fab8089c3139b542140fc7d674106" "checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" "checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5" "checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" diff --git a/Cargo.toml b/Cargo.toml index aec710b..ec52493 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,11 +12,8 @@ log = "*" env_logger = "*" lazy_static = "1.1" lazy-static-include = "1.2" -toml = "0.4" libc = "*" flatbuffers = { path = "./third_party/flatbuffers/rust/flatbuffers" } -serde = "*" -serde_derive = "1.0" hyper = "0.12" hyper-tls = "0.3" http = "0.1" @@ -36,7 +33,6 @@ trust-dns = "0.15.0-alpha.2" trust-dns-proto = "0.5.0-alpha.3" tokio-fs = "0.1" tokio-codec = "0.1" -num_cpus = "1" glob = "0.2" clap = "2.32" diff --git a/README.md b/README.md index 2053de9..fbd3ea2 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,70 @@ -# Fly Edge Runtime +# Fly DNS Apps -## A multi-tenant, v8 based runtime for building Edge Apps +## Handling DNS requests with JavaScript -This is the next generation version of [fly](superfly/fly), and replaces the Node / isolated-vm portions of the runtime with a native Rust + v8 binary. It's much faster. And much more concurrent. Plus it's Rust so it's more fun. +This is a programmable DNS server. You can write JavaScript to handle DNS queries any way you want. ## Installation +### MacOS and Linux + +[Download the latest release](/superfly/fly.rs/releases) for your platform, ungzip and put the binary somewhere + +### Windows + +Unavailable. Relevant issue: [#9](/superfly/fly.rs/issues/9) + +## Usage + +``` +fly-dns --port 8053 relative/path/to/file.js +``` + +## Examples + +### Simple proxy + +```javascript +// Handle an event for a DNS request +addEventListener("resolv", event => { + event.respondWith( // this function responds to the DNS request event + resolv( // the resolv function resolves DNS queries + event.request.queries[0] // picks the first DNSQuery in the request + ) + ) +}) +``` + +### Static response + +```javascript +addEventListener("resolv", event => { + event.respondWith(function () { // can respond with a function + return { + authoritative: true, // hopefully you know what you're doing + answers: [ // list of DNS answers + { + name: event.request.queries[0].name, // name of the DNS entry + rrType: DNSRecordType.A, // record type + ttl: 300, // time-to-live for the client + data: "127.0.0.1" // data for the record + } + ] + } + }) +}) +``` + +## Fly & Deno + +The Fly runtime was originally derived from [deno](denoland/deno) and shares some of the same message passing semantics. It has diverged quite a bit, but when possible we'll be contributing code back to deno. + +There's an issue: [#5](/superfly/fly.rs/issues/5) + +## Development + +### Setup + - `wget -qO- https://github.com/superfly/libv8/releases/download/7.1.321/v8-osx-x64.tar.gz | tar xvz -C libfly` - `git submodule update --init` - `cd third_party/flatbuffers` @@ -17,77 +76,10 @@ This is the next generation version of [fly](superfly/fly), and replaces the Nod - `yarn install` - `rollup -c` - `cd ..` -- `cargo run --bin server` +- `cargo run --bin dns` -## Running v8env tests +### Running v8env tests ``` cargo run --bin test "v8env/tests/**/*.spec.js" -``` - -## Fly & Deno - -The Fly runtime was originally derived from [deno](denoland/deno) and shares some of the same message passing semantics. It has diverged quite a bit, but when possible we'll be contributing code back to deno. - -## TODO - -- [x] Send `print` (all `console.x` calls) back into Rust to handle in various ways - - [x] Send errors to stderr - - [x] Use envlogger (`debug!`, `info!`, etc. macros) for messages - - [ ] Allow sending to graylog or something external -- [ ] Feature-parity - - [ ] Image API - - [ ] Cache - - [ ] Expire (set ttl) - - [ ] TTL (get ttl) - - [ ] Tags / purge - - [ ] global.purgeTag / del - - [ ] Testing -- [ ] Runtime - - [ ] Lifecycle management - - [ ] Gracefully "replace" if running out of heap - - [ ] Handle promise rejection (trash the runtime? just log?) - - [ ] Handle uncaught error (trash? log?) -- [ ] Builder - - [ ] TypeScript support - - [ ] HTTP imports! - - [ ] Source maps - - [ ] Handle source maps in the rust hook -- [ ] CI builds + releases - - [x] Mac - - [x] Linux - - [ ] Windows -- HTTP - - [ ] Actually use the config hostnames and correct app - - [x] Spawn multiple runtime instances for the same app (n cpus? configurable?) - - [ ] Add `Server` header for Fly and current version (maybe?) - - [ ] Fetch request bodies -- [ ] TLS - - [ ] Explore handling TLS handshakes and responding - - [ ] JS API - - [ ] Rust serialization -- [ ] TCP - - [ ] Explore handling raw TCP packets (decrypted) and responding - - [ ] JS API - - [ ] Rust serialization -- [ ] UDP - - [ ] Explore handling UDP packets and responding - - [ ] JS API - - [ ] Rust serialization -- [x] DNS - - [x] Explore handling DNS requests and responding - - [x] JS API - - [x] Rust server - - [x] Rust serialization - - [ ] DNSSEC - - [ ] DNS over TLS -- [ ] Observability - - [ ] Exception reporting (via Sentry probably) - - [ ] Metrics (prometheus) -- Stability / Resilience - - [ ] do not use `unwrap` (that will panic and exit the process). Solution is to handle them and return or print proper errors - - [x] Get rid of all warnings - - [ ] Tests! -- [ ] Optimizations - - [ ] Flatbuffers can be build without allocations, by using `addX` like we do in javascript - - [ ] Reuse ArrayBuffer in certain cases \ No newline at end of file +``` \ No newline at end of file diff --git a/fly.toml b/fly.toml deleted file mode 100644 index 247f5b0..0000000 --- a/fly.toml +++ /dev/null @@ -1,10 +0,0 @@ -port = 8888 - -[apps] - [apps.hello-world] - filename = "hello-world.js" - scale = 1 - -[hostnames] - [hostnames."helloworld.test"] - app = "hello-world" \ No newline at end of file diff --git a/src/bin/dns/main.rs b/src/bin/dns/main.rs index be87501..9e6c88f 100644 --- a/src/bin/dns/main.rs +++ b/src/bin/dns/main.rs @@ -16,10 +16,7 @@ use trust_dns_proto::op::header::Header; use trust_dns_proto::rr::{Record, RrsetRecords}; use trust_dns_server::authority::authority::LookupRecords; -// use dns::rr::{LowerName, Name}; - use futures::sync::oneshot; -// use std::sync::mpsc::RecvError; use std::io; use trust_dns_server::server::{Request, RequestHandler, ResponseHandler, ServerFuture}; @@ -52,6 +49,7 @@ extern crate clap; fn main() { let env = Env::default().filter_or("LOG_LEVEL", "info"); env_logger::init_from_env(env); + debug!("V8 version: {}", libfly::version()); let matches = clap::App::new("fly-dns") .version("0.0.1-alpha") @@ -62,7 +60,7 @@ fn main() { .long("port") .takes_value(true), ).arg( - clap::Arg::with_name("INPUT") + clap::Arg::with_name("input") .help("Sets the input file to use") .required(true) .index(1), @@ -70,7 +68,7 @@ fn main() { let runtime = { let rt = Runtime::new(None); - rt.eval_file(matches.value_of("INPUT").unwrap()); + rt.eval_file(matches.value_of("input").unwrap()); rt }; @@ -85,8 +83,7 @@ fn main() { let server = ServerFuture::new(handler); let udp_socket = UdpSocket::bind(&addr).expect(&format!("udp bind failed: {}", addr)); - info!("listening for udp on {:?}", udp_socket); - info!("V8 version: {}", libfly::version()); + info!("Listener bound on address: {}", addr); let main_el = tokio::runtime::Runtime::new().unwrap(); unsafe { @@ -178,15 +175,6 @@ impl RequestHandler for DnsHandler { }, ); - // let guard = RUNTIMES.read().unwrap(); - // let rtsv = guard.values().next().unwrap(); - - // let idx = { - // let map = REQ_PER_APP.read().unwrap(); - // let counter = map.values().next().unwrap(); - // counter.fetch_add(1, Ordering::Relaxed) % rtsv.len() - // }; - let rt = &self.runtime; let rtptr = rt.ptr; diff --git a/src/bin/server/main.rs b/src/bin/server/main.rs index 0fbc26b..f59b54c 100644 --- a/src/bin/server/main.rs +++ b/src/bin/server/main.rs @@ -1,13 +1,11 @@ #[macro_use] extern crate log; -// #[macro_use] -// extern crate lazy_static; +extern crate clap; extern crate env_logger; extern crate fly; extern crate tokio; -extern crate toml; extern crate libfly; @@ -23,22 +21,13 @@ extern crate futures; use futures::sync::oneshot; use std::sync::mpsc::RecvError; -use std::fs::File; -use std::io::Read; - use tokio::prelude::*; -use tokio::timer::Interval; - -use std::time::Duration; use fly::runtime::*; use fly::utils::*; use env_logger::Env; -use config::*; -use fly::config; - extern crate flatbuffers; use flatbuffers::FlatBufferBuilder; @@ -46,24 +35,11 @@ use std::alloc::System; #[global_allocator] static A: System = System; -#[macro_use] -extern crate lazy_static; -extern crate num_cpus; - -use std::collections::HashMap; -use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; -use std::sync::RwLock; - -lazy_static! { - static ref NCPUS: usize = num_cpus::get(); - static ref REQ_PER_APP: RwLock> = RwLock::new(HashMap::new()); - pub static ref RUNTIMES: RwLock>>> = - RwLock::new(HashMap::new()); -} +use std::sync::atomic::Ordering; -pub struct FlyServer { - // config: Config, -} +pub static mut RUNTIME: Option> = None; + +pub struct FlyServer; extern crate libc; @@ -148,16 +124,7 @@ impl Service for FlyServer { }, ); - let guard = RUNTIMES.read().unwrap(); - let rtsv = guard.values().next().unwrap(); - - let idx = { - let map = REQ_PER_APP.read().unwrap(); - let counter = map.values().next().unwrap(); - counter.fetch_add(1, Ordering::Relaxed) % *NCPUS - }; - - let rt = &rtsv[idx]; + let rt = unsafe { RUNTIME.as_ref().unwrap() }; let rtptr = rt.ptr; let to_send = fly_buf_from( @@ -255,75 +222,53 @@ impl Service for FlyServer { fn main() { let env = Env::default().filter_or("LOG_LEVEL", "info"); - info!("V8 version: {}", libfly::version()); - env_logger::init_from_env(env); + let matches = clap::App::new("fly-http") + .version("0.0.1-alpha") + .about("Fly HTTP server") + .arg( + clap::Arg::with_name("port") + .short("p") + .long("port") + .takes_value(true), + ).arg( + clap::Arg::with_name("bind") + .short("b") + .long("bind") + .takes_value(true), + ).arg( + clap::Arg::with_name("input") + .help("Sets the input file to use") + .required(true) + .index(1), + ).get_matches(); + + info!("V8 version: {}", libfly::version()); + let mut main_el = tokio::runtime::Runtime::new().unwrap(); unsafe { EVENT_LOOP_HANDLE = Some(main_el.executor()); }; - let mut file = File::open("fly.toml").unwrap(); - let mut contents = String::new(); - file.read_to_string(&mut contents).unwrap(); - let conf: Config = toml::from_str(&contents).unwrap(); - - debug!("toml: {:?}", conf); - - for (name, app) in conf.apps.unwrap().iter() { - { - let mut rts = RUNTIMES.write().unwrap(); - let mut rtsv: Vec> = vec![]; - let filename = app.filename.as_str(); - for _i in 0..*NCPUS { - let rt = Runtime::new(Some(name.to_string())); - rt.eval_file(filename); - rtsv.push(rt); - } - rts.insert(name.to_string(), rtsv); - REQ_PER_APP - .write() - .unwrap() - .insert(name.to_string(), ATOMIC_USIZE_INIT); - }; - } + let runtime = { + let rt = Runtime::new(None); + rt.eval_file(matches.value_of("input").unwrap()); + rt + }; + unsafe { + RUNTIME = Some(runtime); + }; - let task = Interval::new_interval(Duration::from_secs(5)) - .for_each(move |_| { - match RUNTIMES.read() { - Ok(_rtsv) => { - // for (key, rts) in rtsv.iter() { - // for rt in rts.iter() { - // let stats = rt.heap_statistics(); - // info!( - // "[heap stats for {0}] used: {1:.2}MB | total: {2:.2}MB | alloc: {3:.2}MB | malloc: {4:.2}MB | peak malloc: {5:.2}MB", - // key, - // stats.used_heap_size as f64 / (1024_f64 * 1024_f64), - // stats.total_heap_size as f64 / (1024_f64 * 1024_f64), - // stats.externally_allocated as f64 / (1024_f64 * 1024_f64), - // stats.malloced_memory as f64 / (1024_f64 * 1024_f64), - // stats.peak_malloced_memory as f64 / (1024_f64 * 1024_f64), - // ); - // } - // } - } - Err(e) => error!("error locking runtimes: {}", e), - }; - Ok(()) - }).map_err(|e| panic!("interval errored; err={:?}", e)); - - main_el.spawn(task); - - let bind = match conf.bind { + let bind = match matches.value_of("bind") { Some(b) => b, - None => "127.0.0.1".to_string(), + None => "127.0.0.1", }; - let port = match conf.port { - Some(p) => p, + let port: u16 = match matches.value_of("port") { + Some(pstr) => pstr.parse::().unwrap(), None => 8080, }; - let addr = format!("{}:{}", bind, port).parse().unwrap(); // ([127, 0, 0, 1], conf.port.unwrap()).into(); + let addr = format!("{}:{}", bind, port).parse().unwrap(); info!("Listening on {}", addr); let server = Server::bind(&addr) diff --git a/src/config.rs b/src/config.rs deleted file mode 100644 index f8650fe..0000000 --- a/src/config.rs +++ /dev/null @@ -1,14 +0,0 @@ -use std::collections::HashMap; - -#[derive(Debug, Deserialize)] -pub struct Config { - pub port: Option, - pub bind: Option, - pub apps: Option>, -} - -#[derive(Debug, Deserialize)] -pub struct App { - pub filename: String, - pub scale: Option, -} diff --git a/src/lib.rs b/src/lib.rs index a860bab..03f4f7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,14 +15,10 @@ extern crate lazy_static; #[macro_use] extern crate lazy_static_include; -#[macro_use] -extern crate serde_derive; - use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT}; pub static NEXT_EVENT_ID: AtomicUsize = ATOMIC_USIZE_INIT; -pub mod config; pub mod errors; pub mod msg; pub mod ops;