Show HN: Building Pastebin for text using Nginx and Lua

0

At situations I take advantage of diversified companies for sharing textual instruct material or code snippets. Pastebin is a effectively known term for such companies (where you paste whatever you should share). The performance is moreover easy (largely) and that’s why I obvious to homicide a pastebin provider using absolute best NGINX.

NGINX and Lua

I truly had been keen on NGINX the 2d I learnt of it. In my earlier days (about 5 yrs ago), I was an Apache guy. But the 2d I grew to change into mindful of NGINX and its gigantic vitality, I never checked out any other webserver tool.

I most steadily share textual instruct material or code snippets online and that’s the reason, It used to be fun to homicide it with out using any “backend”. NGINX by myself used to be sufficient with its awesome Lua module which presents limitless probabilities. I desired to bear it easy so there isn’t great in it rather than the absolute best feature of storing undeniable-textual instruct material pastes.

NGINX Config

The final backend of this pastebin is internal of the NGINX config. The Lua code internal content_by_lua_block is all liable for saving the paste. The code is saving the pastes to the native filesystem but there are unending probabilities (for example, storing in a database fancy mysql or redis).

The next space block is liable for all /api/pastes requests. The directory to which the paste is saved must moreover be writeable by the webserver user.

# api for storing paste
space = /api/pastes {
	default_type "textual instruct material/undeniable";

		client_max_body_size 128good sufficient;
		client_body_buffer_size 128good sufficient;
			
	content_by_lua_block {
		ngx.req.read_body()
		
		native blueprint = ngx.req.get_method()

		if blueprint == "POST" then
			native args, err = ngx.req.get_post_args(1)

			if err == "truncated" then
				ngx.jabber("error: absolute best one POST parameter allowed.")
				ngx.exit(403)
			discontinuance

			if not args then
				ngx.jabber("error: didn't win put up args: ", err)
				ngx.exit(500)
			discontinuance
			
			native instruct material = args.instruct material

			if instruct material == nill then
				ngx.jabber("error: required parameter 'instruct material' not put of residing")
				ngx.exit(400)
			elseif instruct material == "" then
				ngx.jabber("error: required parameter 'instruct material' is empty")
				ngx.exit(400)
			discontinuance

			math.randomseed(os.clock()+os.time())
			native random = math.random
			native characteristic randid()
				native template ='xxxx4xxxxxxyxxxx'
				return string.gsub(template, '[xy]', characteristic (c)
					native v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
					return string.format('%x', v)
				discontinuance)
			discontinuance
			native title = randid()
			native file, err = io.launch("/var/www/nginx-pastebin/pastes/" .. title, "w")
			if file == nill then
				ngx.jabber("error: couldn't launch file: ", err)
			else 
				file:write(instruct material)
				file:finish()
			discontinuance
			
			ngx.jabber("https://", ngx.var.host, "/", title)

		elseif blueprint == "GET" then
			ngx.jabber("ship a put up quiz with 'instruct material=your_text_content'")
		else 
			ngx.jabber("error: not allowed")
			ngx.exit(405)
		discontinuance
	}
}

Here’s an example of how companies interesting easy (and even sophisticated?) logic can even be written using Lua internal NGINX.

ngx has many other APIs and likewise you would perhaps well win almost every NGINX-linked action for your Lua code (rewriting, redirects, caching, serving instruct material and much more).

Demo and Complete Code

I truly bear put of residing this up on pastebin.usamaejaz.com and the provision code has been pushed to github.com/usamaejaz/nginx-pastebin.

Read More

Leave A Reply

Your email address will not be published.