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

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

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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Cracking the Coding Interview

Cracking the Coding Interview

Gayle Laakmann McDowell / CareerCup / 2015-7-1 / USD 39.95

Cracking the Coding Interview, 6th Edition is here to help you through this process, teaching you what you need to know and enabling you to perform at your very best. I've coached and interviewed hund......一起来看看 《Cracking the Coding Interview》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具