Generate a Go REST API from a PostgreSQL database with pagination, sorting, filtering, and auth

栏目: IT技术 · 发布时间: 5年前

Generate a Go REST API from a PostgreSQL database with pagination, sorting, filtering, and auth

DBCore

Code generation powered by your database.

  • Generate standardized REST APIs with:
    • GET (one, many), POST, DELETE, PUT endpoints
    • Filtering using a SQL-like DSL
    • Pagination, sorting
    • JWT-based authentication
  • Write your own templates to generate database-driven code for any language
  • MIT license

This software is in ALPHA development.

Star

Generating a Go API

In your project root folder create a dbcore.yml :

project: $project-name
database:
  dialect: postgres

  # Omit if localhost
  host: $host-or-ip

  # Omit if 5432
  port: $port

  database: $database-name
  username: $username
  password: $password

api:
  template: go
  outDir: go

  # e.g. "v1/", for URLs
  routerPrefix: router-prefix

  auth:
    enabled: true
    table: users
    # Column for username field
    username: username
    # Column for password field, bcrypt hash is stored
    password: password

  extra:
    repo: $your-repo

The dbcore.yml file controls code generation. You'll also need an app.yml file for runtime behavior:

database:
  dialect: postgres
  dsn: postgres://$username:$password@$host/$database?sslmode=disable

address: :9090

session:
  duration: 2hr
  secret: my-secret-signing-key

Clone the repo and run dotnet run $path-to-your-project within the repo root.

Then go to your project directory and run go run cmd/main.go to start the server.

API Specification

Authentication

When authentication is enabled, make a JSON POST request with {"username": "$your-username", "password": "$your-password"} to /$version/session/start to generate a token. It is valid for 2 hours by default.

Passwords are stored as BCrypt hashes.

You can store this token in the au cookie or you can submit it as bearer token by setting the Authorization header to BEARER $your-token .

Example

$ curl -X POST -d '{"username": "alex", "password": "alex"}' localhost:9090/v1/session/start
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTA3MjkyNjMsImlhdCI6MTU5MDcyMjA2MywibmJmIjoxNTkwNzIyMDYzLCJ1c2VybmFtZSI6InBoaWwifQ.4AAveeFRpXckn3cRFyCQew2V7jmcU4OOYH68wcv6afI"}

Get many rows from a table

Make a GET request to /$version/$table .

Query parameters

Parameter Definition Example
limit Number of rows to return limit=25
offset Number of rows to skip offset=0
sortColumn Column to sort on sortColumn=id
sortOrder Order to sort (one of asc or desc ) sortOrder=desc
filter SQL where filter to eliminate results filter=id>3

Example

$ curl 'localhost:9090/v1/users?limit=25&offset=0&sortColumn=id&sortOrder=desc&filter=id=1'
{
  "total": 1,
  "data": [
    {
      "id": 1,
      "username": "alex",
      "password": "<REDACTED>",
      "name": "Alex"
    },
  ]
}

Create a new row

Make a POST request to /$version/$table/new .

Example

$ curl -X POST -d '{"username": "alex", "password": "alex", "name": "Alex"}' localhost:9090/v1/users/new
{"id":1,"username":"alex","password":"<REDACTED>","name":"Alex"}

Get a row

Make a GET request to /$version/$table/$id .

This endpoint is only available if the table has a primary key.

Example

$ curl localhost:9090/v1/users/1
{"id":1,"username":"alex","password":"<REDACTED>","name":"Alex"}

Update a row

Make a PUT request to /$version/$table/$id .

This endpoint is only available if the table has a primary key.

Example

$ curl -X PUT -d '{"id": 1, "username": "alex", "password": "alex", "name": "Alex K"}' localhost:9090/v1/users/1
{"id":1,"username":"alex","password":"<REDACTED>","name":"Alex K"}

Delete a row

Make a DELETE request to /$version/$table/$id .

This endpoint is only available if the table has a primary key.

Example

$ curl -X DELETE localhost:9090/v1/users/1

以上所述就是小编给大家介绍的《Generate a Go REST API from a PostgreSQL database with pagination, sorting, filtering, and auth》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

敏捷软件开发

敏捷软件开发

Robert C. Martin / 邓辉 / 清华大学出版社 / 2003-09-01 / 59.00元

在本书中,享誉全球的软件开发专家和软件工程大师Robert C.Martin将向您展示如何解决软件开发人员、项目经理及软件项目领导们所面临的最棘手的问题。这本综合性、实用性的敏捷开发和极限编程方面的指南,是由敏捷开发的创始人之一所撰写的。一起来看看 《敏捷软件开发》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具