Live data from Hacker News

Simple API with Nginx and PostgreSQL

rny.io

51–60 of 74 posts

Re: Simple API with Nginx and PostgreSQL

#52

This 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…

Edit: he should have escaped the $title and $body.

Re: Simple API with Nginx and PostgreSQL

#54

On an unrelated note, timestamptz is advisable over timestamp.

Timestamptz is one amazing reason to use Postgres over MySQL frankly. Proper time + timezone storage is quite useful.

Postgresql doesn't offer proper timezone storage, or in fact any timezone storage. Timestamptz is just a UTC timestamp that is converted to/from your connection-specified timezone. You completely lose the "what timezone was this originally specified in" data.

Re: Simple API with Nginx and PostgreSQL

#55
It was a bad move not include protection against SQL injection to begin with. I was thinking about keeping it as simple as possible and just show a simplified example. But I think this was a bad thing to do so I have updated the example so it now uses postgres_escape.

And yes, this approach should only be used for very simple APIs. If you're building something bigger, use a framework.

Re: Simple API with Nginx and PostgreSQL

#56
post #54

Earlier quoted context omitted.

Timestamptz is one amazing reason to use Postgres over MySQL frankly. Proper time + timezone storage is quite useful.

Postgresql doesn't offer proper timezone storage, or in fact any timezone storage. Timestamptz is just a UTC timestamp that is converted to/from your connection-specified timezone. You completely lose the "what timezone was this originally specified in" data.

And see this StackOverflow question: http://stackoverflow.com/questions/9571392/ignoring-timezone...

Re: Simple API with Nginx and PostgreSQL

#57
post #49

Great for a quick little hack, but I wouldn't want to maintain that conf file once the project grows.

Well, this is for a very simple REST API and the post acknowledges that much. I mean if this does become a "project", I'm sure it's time to move it to a proper framework, but as you say for a quick hack, it's a pretty nice way to do it (with proper sanitization, of course). I'd also venture that this is an order of magnitude faster and more responsive than a framework so there are definitely some plusses and minuses…

yep agreed. if you're comfortable with nginx and just want to test something quickly, this is many times better than downloading a framework, setting up, routing, etc.

Re: Simple API with Nginx and PostgreSQL

#58

Doesn't couchdb accomplish this? (out of box REST API with little config)

Exactly. Couchdb is preferable to this for so many reasons. The author is effectively using the Nginx configuration language for server-side scripting. The result ends up looking a lot like a bad PHP crud app. With couchdb, the REST API is already built in.

Re: Simple API with Nginx and PostgreSQL

#59
post #58

Doesn't couchdb accomplish this? (out of box REST API with little config)

Exactly. Couchdb is preferable to this for so many reasons. The author is effectively using the Nginx configuration language for server-side scripting. The result ends up looking a lot like a bad PHP crud app. With couchdb, the REST API is already built in.

Except if you already have a postgres-based system and you want to build a simple REST api around some small piece of it.

Re: Simple API with Nginx and PostgreSQL

#60

Is this a "look how cool/flexible nginx is!" article or a real proposal about how to do API's?

Hopefully the former. It would be difficult to build anything non-trivial with it. However, I suppose you could build a basic counter or key-value system over HTTP with it.

It is by definition difficult to build anything non-trivial regardless.

This looks like a very nice way to build small simple REST APIs especially if you're already using nginx and postgres anyway.

Post reply on HN