Created
March 7, 2014 17:10
-
-
Save oroce/9415506 to your computer and use it in GitHub Desktop.
upload files from your grunt to nginx without backend or upstream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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/" | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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