tanryberdi / westack-go

Strongly opinionated framework which allows you to quickly setup a REST API server in a few minutes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

westack-go

Introduction

westack-go is a strongly opinionated framework which allows you to quickly setup a REST API server in a few minutes.

Just define your models in json format and westack-go will setup and expose all basic CRUD methods for you

Technologies

westack-go uses technologies like gofiber and casbin for REST and authentication

Databases

It is only compatible with mongo.

Authentication

Define RBAC policies in your json models to restrict access to data.

Installing westack

go install github.com/fredyk/westack-go@v1.5.46

Initialize a new project

mkdir my-new-project
cd my-new-project
westack-go init .

Create a new model

# Usage: westack-go model add <model_name> <datasource_name>
#   <datasource_name> defaults to "db" when you run `westack-go init .`
 
westack-go model add Note db

Getting started

(Optional) Customize your models and datasources

User.json
{
  "name": "User",
  "base": "User",
  "public": true,
  "hidden": [
    "password"
  ],
  "properties": {
    "email": {
      "type": "string",
      "required": true
    },
    "password": {
      "type": "string",
      "required": true
    }
  },
  "relations": {
    "notes": {
      "type": "hasMany",
      "model": "note"
    }
  }
}
Role.json
{
  "name": "Note",
  "base": "PersistedModel",
  "public": true,
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "body": {
      "type": "string",
      "required": true
    }
  },
  "relations": {
    "user": {
      "type": "belongsTo",
      "model": "User"
    }
  },
  "casbin": {
    "policies": [
      "$everyone,*,*,deny",
      "$authenticated,*,create,allow",
      "$owner,*,*,allow"
    ]
  }
}
datasources.json
{
  "db": {
    "name": "db",
    "host": "localhost",
    "port": 27017,
    "database": "example_db",
    "password": "",
    "username": "",
    "connector": "mongodb"
  }
}
model-config.json
{
  "User": {
    "dataSource": "db"
  },
  "Note": {
    "dataSource": "db"
  }
}

Run

westack-go server start

Test it:

  1. Create a user
$ curl -X POST http://localhost:8023/api/v1/users -H 'Content-Type: application/json' -d '{"email":"exampleuser@example.com","password":"1234"}'
  1. Login
$ curl -X POST http://localhost:8023/api/v1/users/login -H 'Content-Type: application/json' -d '{"email":"exampleuser@example.com","password":"1234"}'

Response body: {"id":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjcmVhdGVkIjoxNjQ3MjUzMDczMTQ0LCJyb2xlcyI6WyJVU0VSIl0sInR0bCI6MTIwOTYwMDAwMCwidXNlcklkIjoiNjIyZjE2NDMzNzdjYTNmMWEzOTI0MWY0In0.sbl7QA2--X7MiPZ4DLRL2f5_z08VD5quItBDl2ybmGk","userId":"622f1643377ca3f1a39241f4"}
  1. Find user data
$ curl http://localhost:8023/api/v1/users/me -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjcmVhdGVkIjoxNjQ3MjUzMDczMTQ0LCJyb2xlcyI6WyJVU0VSIl0sInR0bCI6MTIwOTYwMDAwMCwidXNlcklkIjoiNjIyZjE2NDMzNzdjYTNmMWEzOTI0MWY0In0.sbl7QA2--X7MiPZ4DLRL2f5_z08VD5quItBDl2ybmGk'
 
Response body: {"email":"exampleuser@example.com","id":"622f1643377ca3f1a39241f4"}
  1. Create a note for the user
$ curl -X POST http://localhost:8023/api/v1/notes -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjcmVhdGVkIjoxNjQ3MjUzMDczMTQ0LCJyb2xlcyI6WyJVU0VSIl0sInR0bCI6MTIwOTYwMDAwMCwidXNlcklkIjoiNjIyZjE2NDMzNzdjYTNmMWEzOTI0MWY0In0.sbl7QA2--X7MiPZ4DLRL2f5_z08VD5quItBDl2ybmGk' -d '{"title":"Note 1","body":"This is my first note","userId":"622f1643377ca3f1a39241f4"}'
  1. Find again the user, now with their notes
$ curl 'http://localhost:8023/api/v1/users/me?filter=%7B"include":%5B%7B"relation":"notes"%7D%5D%7D' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjcmVhdGVkIjoxNjQ3MjUzMDczMTQ0LCJyb2xlcyI6WyJVU0VSIl0sInR0bCI6MTIwOTYwMDAwMCwidXNlcklkIjoiNjIyZjE2NDMzNzdjYTNmMWEzOTI0MWY0In0.sbl7QA2--X7MiPZ4DLRL2f5_z08VD5quItBDl2ybmGk'

Response body: {"email":"exampleuser@example.com","id":"622f1643377ca3f1a39241f4","notes":[{"title":"Note 1","body":"This is my first note","userId":"622f1643377ca3f1a39241f4","id":"622f1643377ca3f1a39241f5"}]}
  1. Find the single note
$ curl http://localhost:8023/api/v1/notes/622f1643377ca3f1a39241f5 -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjcmVhdGVkIjoxNjUwNDA2ODEzNDY3LCJyb2xlcyI6WyJVU0VSIl0sInR0bCI6MTIwOTYwMDAwMCwidXNlcklkIjoiNjI1ZjM1OTE0NzU5YWJiOGZhMmE1YzljIn0.hWeMlZrhTFAac4LXTSiSIQ7uy7VhAlg1L9DKG3QPTpg'

Response body: {"title":"Note 1","body":"This is my first note","userId":"622f1643377ca3f1a39241f4","id":"622f1643377ca3f1a39241f5"}
  1. Update the note
$ curl -X PATCH http://localhost:8023/api/v1/notes/622f1643377ca3f1a39241f5 -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjcmVhdGVkIjoxNjUwNDA2ODEzNDY3LCJyb2xlcyI6WyJVU0VSIl0sInR0bCI6MTIwOTYwMDAwMCwidXNlcklkIjoiNjI1ZjM1OTE0NzU5YWJiOGZhMmE1YzljIn0.hWeMlZrhTFAac4LXTSiSIQ7uy7VhAlg1L9DKG3QPTpg' -d '{"body":"I modified the note body"}'

Response body: {"title":"Note 1","body":"I modified the note body","userId":"622f1643377ca3f1a39241f4","id":"622f1643377ca3f1a39241f5"}

Change Log

  • v1.6.0

    • Added parameter strictSingleRelatedDocumentCheck in config.json, defaults to truein new projects, and false in existing ones.
    • "hasOne" and "belongsTo" relations are now checked after fetching documents from Mongo. If strictSingleRelatedDocumentCheck is true and the relation returns more than 1 document, an error is thrown. Otherwise, only the first document is used and a warning is logged.
    • Breaking changes:
      • model.Build() requires now parameter sameLevelCache *buildCache to be passed in. Can be generated with model.NewBuildCache()
      • model.Build() returns now error as second value, in addition to the instance. So it is now func (loadedModel *Model) Build(data wst.M, sameLevelCache *buildCache, baseContext *EventContext) (Instance, error)
  • v1.5.48

    • Breaking change: environment variables WST_ADMIN_USERNAME and WST_ADMIN_PWD are required to start the server

Contribute

Write to westack.team@gmail.com if you want to contribute to the project: D

You are also welcome on our official Discord

And of course... create as many pull requests as you want!

About

Strongly opinionated framework which allows you to quickly setup a REST API server in a few minutes

License:MIT License


Languages

Language:Go 100.0%