Efficient backend API development template illuminant

Time:2020-7-6
  • outline
  • General introduction
    • Supplementary notes
      • Q. With the graphql interface automatically generated by PRISMA, why still need rest interface and websocket interface?
      • Q. Why add a layer of reverse proxy through illuminant instead of directly exposing the graphql interface of PRISMA?
      • Q. Since the graphql interface generated by PRISMA is already very powerful, why do you need gorose ORM to connect to the database
  • Introduction to illuminant code structure
  • Problem solved

outline

After adopting the development mode of separating the front end and the back end, the burden of back-end development is lightened, but at the same time, the burden of communication between front-end and back-end development is increased

Recently, I summarized a set of back-end development framework based on my long-term project practice, mainly based on golang and PRISMA

  1. It’s very simple in itself. It’s more about combining various technologies
  2. Making full use of the graphql interface generated by PRISMA service, almost all curd operations are automated
  3. In addition to connecting to the database through PRISMA, you can also use golang to directly connect to the database, using gorose, the orm
  4. It provides the basic JWT authentication function. The authority and business association are relatively tight, which are removed from the framework
  5. For example API (graphql / rest) provided by various types of interfaces, it is only necessary to refer to the existing interfaces to add restful interfaces
  6. Upload / download provides interface examples, which can also be used to dock with other storage services
  7. An example of websocket interface is provided
  8. The whole golang project is actually a template. Change the name after git clone and configure your PRISMA schema, you can use it directly

To make good use of this project, we need to have a certain understanding of the project name of prisma-v1illuminantOpen source gitee.com : https://gitee.com/wangyubin/illuminant Welcome to try!

General introduction

illuminant-overview

  1. The core isilluminantengineering
  2. Among them, the download / upload API can directly dock to the disk or to the storage service
  3. The graphql interface automatically generated by PRISMA is connected through reverse proxy
  4. Direct connection to MySQL database through gorose ORM
  5. Three types of interfaces can be provided
    • Graphql: PRISMA auto generation
    • Rest: as a supplement to the automatic generation of interfaces, such interfaces are not required in general projects
    • Websocket: if there are real-time push or similar requirements, relevant interfaces can be implemented

Supplementary notes

Q. With the graphql interface automatically generated by PRISMA, why still need rest interface and websocket interface?

In fact, the interface generated automatically by PRISMA can meet all kinds of curd requirements
However, for some statistical analysis requirements, or the needs of back-end analysis and calculation, you can customize rest API as an extension of graphql API

Q. Why add a layer of reverse proxy through illuminant instead of directly exposing the graphql interface of PRISMA?

After reading the code, we know that the reverse proxy in illuminant directly forwards the client’s request to PRISMA without any special operation
But directly exposing the interface generated by PRISMA is not conducive to the subsequent management. At least it is difficult to do a simple user name / password authentication for the interface, because you can’t change the source code of PRISMA to support your authentication mechanism

After adding a layer, you can control requests from clients more flexibly in illuminant. It is not impossible to add permissions later,
However, Prisma’s graphql service can be regarded as one that provides rich graphql interfacesORM

Q. Since the graphql interface generated by PRISMA is already very powerful, why do you need gorose ORM to connect to the database

PRISMA’s graphql is really powerful and can fulfill almost all requirements
However, it still has some defects, such as the support for transactions is not very good, and it does not support running SQL directly
Therefore, the purpose of leaving this gap in illuminant is to be more flexible in dealing with unknown needs

Introduction to illuminant code structure

illuminant 架构

  1. Purple dotted line part: it is the entrance of the back end
    • main.go:It mainly initializes the configuration file, logger and so on
    • route.go:Based on the gin framework, including some API examples (login, upload / download, graphql, curd of todo, etc.)
    • middleware:There is only one JWT certified middleware for the time being
  2. Red dotted line: the part related to PRISMA. In fact, the whole illuminant framework mainly wants to use PRISMA to reduce the development of API
    • prisma:PRISMA configuration, including PRISMA service definition( prisma.yml ), database model definition( datamodel.prisma ), and the docker that starts the service- compose.yml
    • prisma-client:According to the PRISMA golang client automatically generated by the database, in addition to the reverse proxy, Prisma can also be accessed through this client
  3. Blue dotted line: parallel with PRISMA, another way to customize the API
    • controller:Processing requests and return values
    • service:Handle the actual business and connect the controller with the model
    • model:Define orm of database
  4. Yellow dotted line: common modules

Problem solved

Illuminant is intended to be a template for back-end development. It is used to make a quick attempt at the early stage of the project, focusing on improving the back-endEfficiency of developmentGo ahead,
Using PRISMA, we can reduce the burden of the back end as much as possible, so that the project can be used as soon as possible and verify the requirements as soon as possible

If you want to use as a production environment of the system, in fact, it is not bad
I have tried the implementation of permission part in another project, but it has not been integrated into illuminant

Recommended Today

Lua language novice simple tutorial

1、 Foreword Lua is a lightweight and compact scripting language, which is written in standard C language and open in the form of source code. Its design purpose is to be embedded in the application, so as to provide flexible expansion and customization functions for the application. Lua can be applied in game development, independent […]