Skip to content

Instantly share code, notes, and snippets.

@oroce
Created March 7, 2014 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oroce/9415506 to your computer and use it in GitHub Desktop.
Save oroce/9415506 to your computer and use it in GitHub Desktop.
upload files from your grunt to nginx without backend or upstream
grunt.registerMultiTask("upload", function(){
var async = require("async");
var request = require("request");
var path = require("path");
var fs = require("fs");
var done = this.async();
var files = [];
var url = this.data.url;
var fns = [];
this.files.forEach(function( files ){
files.src.forEach(function(file){
var fullName = path.join(files.cwd, file);
fns.push(function(cb){
var req = request.post({
url: url + file,
headers: {
"Content-Length": fs.statSync(fullName).size
}
},cb);
fs.createReadStream(fullName)
.pipe(req)
.on("error",cb);
});
});
});
async.parallelLimit(fns, this.data.limit||5, done);
});
/* if you have a directory structure like this:
|public
|--images
|-----image1.jpg
|--v/v1.2.3
|----main.js
|----stylesheets
|------style.css
*/
grunt.initConfig({
upload: {
build: {
files: [{
expand: false,
src: [ "images/*", "v<%=pkg.version%>/**/*" ],
cwd: "public",
filter: "isFile"
}],
url: "http://127.0.0.1:8000/your-project/"
}
}
});
server {
listen 8000;
location / {
limit_except POST { deny all; }
client_body_buffer_size 10M;
lua_need_request_body on;
content_by_lua '
local file
local filename
filename = "/var/www/cdn" .. ngx.var.uri
os.execute("mkdir -p `dirname " .. filename .. "`")
file = io.open(filename,"w")
file:write(ngx.req.get_body_data())
file:close()
ngx.status = ngx.HTTP_OK
';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment