Skip to content

Commit

Permalink
add nginx instance module where you can start and stop nginx
Browse files Browse the repository at this point in the history
sample test for instance
  • Loading branch information
Sehyo Chang committed Sep 1, 2017
1 parent 3102b5a commit acfc545
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ RUST_TOOL = gcr.io/$(GCLOUD_PROJECT)/ngx-rust-tool:${RUST_COMPILER_TAG}
export ROOT_DIR=${PWD}



darwin-build-nginx:
cd nginx/${DARWIN_NGINX}; \
./configure --prefix=${PWD}/nginx/install; \
make; \
make install


darwin-install-nginx: darwin-source darwin-configure


setup-nginx:
mkdir -p nginx

Expand Down
43 changes: 43 additions & 0 deletions config/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#load_module modules/ngx_http_rust_module.so;

#user nobody;
worker_processes 1;

error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;


# rust test;



server {
listen 8080;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}


}

}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

pub mod bindings;
pub mod nginx_http;

pub mod nginx;


83 changes: 83 additions & 0 deletions src/nginx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* harness to test nginx
*/

use std::process::Command;
use std::process::Output;
use std::io::Result;
use std::env;
use std::fs;

const NGINX_BIN: &str = "nginx/install/sbin/nginx";

pub struct Nginx {

pub install_path: String // install path
}


impl Nginx {

pub fn new(path: String) -> Nginx {
Nginx { install_path: path }
}

// create nginx with default
pub fn default() -> Nginx {
let path = env::current_dir().unwrap();
let nginx_bin_path = format!("{}/{}",path.display(),NGINX_BIN);
Nginx { install_path: nginx_bin_path}
}


pub fn cmd(&mut self, args: &[&str] ) -> Result<Output> {
let result = Command::new(&self.install_path)
.args(args)
.output();

match result {
Err(e) => {
return Err(e);
},

Ok(output) => {
println!("status: {}", output.status);
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
return Ok(output);
}
}

}

// complete stop the nginx binary
pub fn stop(&mut self) -> Result<Output> {
self.cmd(&["-s","stop"])
}

// start the nginx binary
pub fn start(&mut self) -> Result<Output> {
self.cmd(&[])
}


// make sure we stop existing nginx and start new master process
// intentinally ignore failure in stop
pub fn restart(&mut self) -> Result<Output> {

self.stop();
self.start()
}

// replace config with another config
pub fn replace_config(&mut self, path: &str) {

}


}





8 changes: 6 additions & 2 deletions tests/log_test.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
extern crate ngx_rust;


#[cfg(test)]
mod tests {

use std::env;
use ngx_rust::nginx::Nginx;

const NGINX_BIN: &str = "nginx/install/sbin/nginx";

#[test]
fn test() {

assert_eq!(1,1);
let mut nginx = Nginx::default();
let output = nginx.restart().expect("fail to start");

assert!(output.status.success());

}

Expand Down

0 comments on commit acfc545

Please sign in to comment.