(1) RESTful design principles
RESTful style: restfu design principle, which was put forward by Roy Felding (in his "web-based software Architecture" thesis in the fifth chapter). The core principle of rest is to split your API into logical resources. These resources are manipulated via HTTP (GET, post,put,delete).
But for now, there are only two general operations: GET, POST.
The simplest application of this design principle is to design an API based on object rather than a page. At the very beginning, what data is needed on one page of the app, and what data is returned by the API. As a result, as the app's UI continues to change, the data needs to be constantly changed, the API is continually modified, and when the API changes affect the previous version, you can only write a new API version, and finally make the API a lot of _v2,v3 such a sign, nightmare!
Later in the site reconstruction process, based on object to design the API, but according to object design, there is another problem, a large object may contain a lot of small object, is an API return all small object, or divided into multiple API return? Think about the business and technology, bandwidth, and so on.
(2) Naming the API
One of the principles, look at the API name to know what this API is doing. In the entrepreneurial team, generally only one or two people in charge of the backstage, when you are responsible for dozens of or even hundreds of APIs, you know can not "look at the name of the API" is what kind of pain.
(3) security, see http://blog.csdn.net/newjueqi/article/details/18887571
(4) API return data
App Client language Java and object-c are strongly typed languages, so how to deal with null values is particularly important, unreasonable design can easily cause the app to flash back.
From the background point of view, the data returned in the API, the correct value and the type of the null value must be the same, for example, the User name field is "Realname": "XXX", if the user name is empty, you should return "Realname": "". If the return value is an array, the empty data returns an empty array, and the null value is absolutely forbidden.
For the client, a global function must be used to process the return data of all the APIs, and a mechanism is required: for a client to need data, if the API is missing, the client automatically complements and gives the default value. This mechanism greatly reduces the app's flash-back in our practice.
At the same time, when designing a database, a reasonable design must be that all fields have default values and should not allow null values. Null is an infinite problem in a large number of languages and databases. For this database design principle, I did not understand before, now after a year of API design, finally understand.
If the client is PHP, there is a problem, PHP arrays and dictionaries are array, but in Java and object-c is not the same, this problem must be noted.
(5) Processing of pictures
In different versions of the app, a variety of different sizes of mobile phones, the same picture display may not be the same size, if you need to return the original image each time, and then processing on the client, it is a huge waste of network resources. And if the background processing good pictures to return, it is a challenge, how to effectively save and crop a variety of image size it
For example, the first avatar only needs to return the size of the 60*60, later in the new version needs to return to 70*70, and a new version, need to return to 80*80, each time to add a new size, how to record on the database. This problem at the beginning of the API did not consider, and later had to use an extreme method, did not add new image size, in the database to add a new field, save and create a new image size, the results of the last database's Avatar field has "Avatar", "avatar_60_60", " Avatar_70_70 "," avatar_80_80 ", this extremely abusive design.
Finally, we used this strategy for the picture:
(1) The client locally caches the picture, only if there is no appropriate picture, to go to the server fetch.
(2) When the client needs a certain size picture, the client tells the server the size of the picture, the server is dynamically generated and cached.
For example, if the client needs the size of the 80*80 of the picture (Http://www.baidu.com/img/bdlogo.gif), then the path of the picture is prefixed with the width and height of the parameter (similar to the CDN mechanism) Http://www.baidu.com/img/bdlogo.gif? W=80&H=80, the server generates 80*80 dimensions and returns.
Using the image processing mechanism, as long as there is a field in the database to save the original picture, the other size is the client to tell the server dynamic generation. In the future no matter what size of the picture, the database does not need to record, the database only the original image on the line.
(6) The prompt information returned
In the most scientific case, the server returns only the information code, and the specific text prompt is determined by the client.
If the text information is returned by the server, the minimum is to distinguish 2 kinds of information: Prompt the user's information, prompting the client programmer's information. The difference between the two:
1. Prompt the user information is to let the customer know, prompt the client programmer's information does not need to let the customer know.
2. Prompt the user's information text is friendly, the customer does not need the professional foundation to know is what, prompts the client programmer the information is very specialized, for example tells the client to pass down which parameter? Which parameter has a problem and so on.
(7) Online API documentation and testing
Our website's API online test document is both an online API document and an on-line test tool that is extremely easy to communicate and test. Every time a client programmer feels that something is wrong with an API, we are talking about it on this online tool. The client programmer likes this thing most.
The last illustration of the greedy (this figure is the old version of the API, has been deprecated):
The colleague responsible for this function wrote a blog detailing this online API test document with Demo:
http://amuropikin.iteye.com/blog/1701537
----------------------------------------------------------------------------------------------------------
Personally, in the small entrepreneurial team, especially in the application of products, in the framework of the background, need to focus on solving their own business problems, not to take the time to solve the problems that the third party has solved, simply speaking, is able to use third-party services in the service of third parties. Based on this principle, the following system architecture is available:
1. APNs: Because in APNs, invalid tokens cause the connection APNs connection to fail and the APNs information is lost. The solution is to maintain the send queue, and when the APNS server returns the wrong token, resend the message after the error token. Third-party push is very good to achieve this technical solution, we chose the Baidu Cloud push send.
2. E-mail: To consider the resend of the message failed to send a problem, so no longer on the server to build SendMail service sent, the choice of mail service provider Mailgun. Mailgun also offers a free amount of 10,000 emails per account per month, ideal for start-up teams.
3. Coreseek: A full-text search engine based on Sphinx. In the previously described LBS module, and the retrieval of user nickname, shops and other search functions need to use.
4. Redis: A key-value database that supports a variety of data structures and has a wide range of uses in LBS modules, performance optimization, and more.
5. HTTPSQS: Lightweight Message Queuing.
6. XMPP: With open source OpenFire, when Web services need to communicate to OpenFire, there are two scenarios:
(1) Real-time requirements, such as registering a user at the chat server, then directly connected to the chat server.
(2) If other non-real-time requirements, such as sending an update notification to the app via a chat server, are processed in the queue.
7. Monitoring, the use of monitoring treasure and cloud server to provide monitoring data to meet the current needs.
Reference:
http://blog.csdn.net/newjueqi/article/category/1743543
App Backend architecture design (GO)