Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Architecture: API as core for a website & mobile-app

I have different questions about a full architecture idea. I hope someone with great experience could help me out because I am pretty much getting stuck in all possibilities.

I'm planning to rewrite a community website. Our customer wants to make use of native mobile apps in the future. So I will need to take this into account. Because of this I have decided to create a 100% REST API architecture based on the PHP framework Kohana. I have choosen Kohana because this makes scaling the internal API to a other server very easily without much extra effort. (Kohana threats internal url requests not as HTTP so there isn't much overhead in the beginning and can scale to HTTP with some minor code changes).

At first the API will be private, but later on we might make it public to let more services connect to us easily.

De basic REST structure is as follow:

  1. /cats
  2. /cats/1
  3. /cats/1/custom

'custom' could be 'childs' for instance.

same goes for:

  1. /ads
  2. /bids
  3. /users
  4. /banners
  5. etc..

These are perfect entities for the API because the mobile app will definitely use all this functionality.

So we can conclude the core of the website is REST. So basically I want to make the website a client of the API just like the native app in the future. This way maintenance seems much easier.

What worries me though is the fact that there is much more than this (managing uploaded files, invoicing, automails for invoicing, automails for ads and so on). Uploading files needs to go through the website to the API. Is this common practice? If I do not do this, the website would do upload logic, which makes the site no client anymore and the app itself. Hence the mobile app can't even upload and both API and website need to know the upload folder (duplicate logic).

I thought of creating the following modules:

  1. community-api
  2. community-website

Api seems to be the core then. But.... what about cronjobs etc? Actually they should not be part of the website, as this is just a 'client'. I feel they should interact directly with the model or API. So basically the API gets more like a gateway to the core and thought I need this:

  1. community-core
    • Models
    • Cronjobs
    • Auto Mailings (part of cronjobs)
      • Invoices etc
  2. community-api
    • Interact with models in core through HTTP
  3. community-website
    • Website
    • Admin

The core cronjobs are a exception to the REST structure. They are the only one that can change data without going through the api. At least that was my idea because they belong in the core and API is on top of the core.

But by design that seems just wrong. Manipulating should only go through the API!

Alternative:

  1. community-core
    • Models
  2. community-api
    • Interact with models in core through HTTP
  3. community business
    • Cronjobs
    • Auto Mailings (part of cronjobs)
      • Invoices etc
  4. community-website
    • Website
    • Admin

This look better by design to me. Mindmap illustration
(source: mauserrifle.nl)

Main Questions

1)

Should cronjobs manipulate through the API or Core models?

2)

My invoice cronjob needs a template pretty much the style of main website of course. But if my cronjob is part of business or core it won't have knowledge of my main website. What makes sense to solve this?

3)

My website will be using mustache as a template engine. (both php and javascript can parse these templates). I thought using the api directly for ajax calls but then realized:

The site gets data from api, formats timestamps to dates (Y-m-d) for the template and then renders. If I let javascript call the api directly, javascript must have logic too (formatting). This is duplicate code! Feels like the only solution is calling the website for ajax (which calls the api and formats) and returns the formatted json. Am I right?

But.... simple calls like deleting a ad can go through the api directly (e.g. DELETE: /ads/1

I get a mix of calls....

Any better solution for this?

4)

Overall: Is my architecture too complex? Any alternatives I should consider?

I would love to hear your feedback!

like image 424
mauserrifle Avatar asked Feb 26 '12 13:02

mauserrifle


People also ask

What is the architecture of web API?

This is an architectural pattern used for exchanging data over a distributed environment. In Rest, there is something called Client and Server, and the data can be exchanged between the client and server over a distributed environment. Distributed environment means the client can be on any platform like Java, .

Is web API a core?

In simple words, we can say that a web API is an application programming interface for a web application or web server. It uses HTTP protocol to communicate between clients and websites to have data access. Asp.net Core web API is a cross-platform web API.

What is API and why we are using API with .NET Core?

To put it in simple terms, API is some kind of interface which has a set of functions that allow programmers to access specific features or data of an application, operating system or other services.

What is clean architecture in web API?

Clean architecture puts the business logic and application model at the center of the application. Instead of having business logic depend on data access or other infrastructure concerns, this dependency is inverted: infrastructure and implementation details depend on the Application Core.


1 Answers

Once I've heard that a good way to develop a web application is to develop an API-Centric Web Application. The thing is, to me, if you couple the main service to the public API, building an API-Centric application, you lose the whole point of developing a public API at all.

like image 100
Daniel Ribeiro Avatar answered Oct 16 '22 13:10

Daniel Ribeiro