I made it to the bottom and thought the article was cut off. Turns out, it really is that simple. (Sans the sql injection and auth stuff). Fantastic.
Simple API with Nginx and PostgreSQL
31–40 of 74 posts
Re: Simple API with Nginx and PostgreSQL
#32very cool, add in some lua scripting and you can get some pretty solid single purpose endpoints: http://wiki.nginx.org/HttpLuaModule maybe not perfect for a whole app but maybe depending on your goals.
Can you recommend a public repo which demonstrates use of the HttpLuaModule? [Edited to add:] Another comment mentions the Lapis framework which gives [me] a good starting point. @subs: thanks!
This helped me setting up nginx/lua/redis setup on Ubuntu https://github.com/ehazlett/nginx-rt-log/blob/master/readme....
Re: Simple API with Nginx and PostgreSQL
#33Re: Simple API with Nginx and PostgreSQL
#34This article gives a good example of why you SHOULD consider using an existing framework to create a REST API. I see no concern about authentication, authorisation, scalability, protection from SQL injection attacks, nor making the output easily parseable by third-party applications. None of these are issues you can simply say, "I'll deal with that later when it becomes a problem." They are reasons why an existing fr…
"protection from SQL injection attacks" In the module he's using, there is a postgres_escape function.. so there's no reason he couldn't have used it. http://labs.frickle.com/nginx_ngx_postgres/README
It's unnecessary anyway. It would be way better would be if the parameters were bound as named parameters. Ex:
location ~ /articles/(?\d+) {
postgres_pass database;
rds_json on;
postgres_query HEAD GET "SELECT * FROM articles WHERE id = $id";
postgres_rewrite HEAD GET no_rows 410;
}
See how $id has no quotes around it? The DB driver should parse the parameter and bind it as a string in that position. If you need to use it as a different data type (ex: integer) then you can do an explicit type conversion. That's how you prevent SQL injection.Re: Simple API with Nginx and PostgreSQL
#35Can this be done with redis, or memcache?
Re: Simple API with Nginx and PostgreSQL
#36I still like the idea of nginx being able to communicate with different datastores. For example, let's say you want to serve a file but it requires authentication, you can pass some signed request via query string and cross check it in session in redis/postgres wherever.
Re: Simple API with Nginx and PostgreSQL
#37Re: Simple API with Nginx and PostgreSQL
#38Pretty neat, what about SQL injections?
This example is not secured against SQL injections but you can do it easily. Look at the documentation here: https://github.com/FRiCKLE/ngx_postgres/
Re: Simple API with Nginx and PostgreSQL
#39Re: Simple API with Nginx and PostgreSQL
#40Can this be done with redis, or memcache?