Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a separate app for my REST API or place it inside my working app?

I'm building simple gis system on geodjango.

The app displays a set of maps and I'm also attempting to provide a RESTFUL API for these maps.

I'm facing a decision whether to create a separate app for the API or to work inside my existing app.
The two apps are logically separate but they share the same models.

So what is considered better?

like image 922
adnanmuttaleb Avatar asked Sep 28 '17 11:09

adnanmuttaleb


2 Answers

@hspandher's answer is very solid and will allow for most of your needs to be implemented.

There is though another approach which may be a bit more complicated to achieve but gives you all the space you may need for experimentation and reusability potential:

Separate everything:

  1. Backend:

    Isolate your API from its visualization (see frontend below) and make it completely autonomous and self-contained.
    That can be achieved by separating your apps inside your Django project and expose the corresponding APIs which must be the only way for an external factor (ex. client, another app etc.) to "talk" with any one of your apps.

  2. Frontend:

    Assuming that you have your APIs exposed, you effectively separated the visualization from the logic and therefore you have many options on how to visualize your maps. For example, you can now build a React app which can make requests to your API and visualize the responses by using any of those tools: leaflet.js, D3.js, or anything that you like really.

Summary:

The benefits of this separation are:

  • Separation of logic and implementation.
  • Better maintainability.
  • Many tool and technology options to use.
  • Reusability.

As a side note, you can read about 12 factor method and think about using it in your implementation.

like image 60
John Moutafis Avatar answered Oct 31 '22 05:10

John Moutafis


Although a case can be made for either of the approaches, I think keeping the APIs inside their associated apps would be a better one. Since the code in APIs is going to depend on the models, or other utility methods anyway, keeping APIs in the same app would lead to more cohesive code. Besides the very ideology behind Django apps is that they can be isolated and reused.

There used to be a similar case with storing the templates. In the initial days of Django, people used to prefer to store all the templates altogether in the same global folder (with subdirectories by the names of the app), however, in recent times even Django has started discouraging the said approach in the favour of storing templates in the respective app itself.

like image 29
hspandher Avatar answered Oct 31 '22 05:10

hspandher