github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/docs/extension.md (about)

     1  # Extension
     2  
     3  You can add additional logic using Gohan Extension.
     4  Extensions has properties:
     5  
     6  - id identity of the code
     7  - code contents of a code
     8  - code_type javascript, go and Gohan script (DSL) are supported
     9  - URL placement of code. Currently, file://, http:// and https:// schemes are supported
    10  - path resource path to execute code
    11  
    12  Example Code
    13  
    14  ```
    15    extensions:
    16    - code: console.log(Object.keys(context));
    17      id: test
    18      path: /v2.0/.*
    19  ```
    20  
    21  ## Event
    22  
    23  ### pre_list
    24  
    25    list event before DB operation
    26  
    27    context.response contains response data.
    28    You can also update response here
    29  
    30    Note you can skip DB operation if you set context response in here
    31  
    32  ### pre_list_in_transaction
    33  
    34    Same as pre_list but executed in the DB transaction context.transaction contains transaction object for DB operation
    35  
    36    id : request id
    37  
    38    context.response contains response data.
    39    You can also update response here
    40  
    41  ### post_list_in_transaction
    42  
    43    Same as post_list but executed in the DB transaction context.transaction contains transaction object for DB operation
    44  
    45    id : request id
    46    context.response contains response data.
    47    You can also update response here
    48  
    49  ### post_list
    50  
    51    list event after DB operation.
    52  
    53    context.response contains response data.
    54    You can also update response here
    55  
    56  ### pre_show
    57  
    58    show event before DB access
    59  
    60    id : request id
    61    context.response contains response data.
    62    You can also update response here
    63  
    64    Note you can skip DB operation if you set context response in here
    65  
    66  ### pre_show_in_transaction
    67  
    68    Same as pre_show but executed in the DB transaction context.transaction contains transaction object for DB operation
    69  
    70    id : request id
    71    context.response contains response data.
    72    You can also update response here
    73  
    74  ### post_show_in_transaction
    75  
    76    Same as post_show but executed in the DB transaction context.transaction contains transaction object for DB operation
    77  
    78    id : request id
    79    context.response contains response data.
    80    You can also update response here
    81  
    82  ### post_show
    83  
    84    show event after DB operation
    85  
    86    id : request id
    87    context.response contains response data.
    88    You can also update response here
    89  
    90  ### pre_create
    91  
    92    executed before creation
    93    Mainly used for validation purpose
    94  
    95    context.resource contains user input data
    96  
    97    Note you can skip DB operation if you set context response in here
    98  
    99  ### pre_create_in_transaction
   100  
   101    Same as pre_create but executed in the DB transaction context.transaction contains transaction object for DB operation
   102  
   103  ### post_create_in_transaction
   104  
   105    after creation in the transaction
   106  
   107  ### post_create
   108  
   109    after create
   110    context.response contains response data.
   111    context.transaction contains transaction object for db operation
   112  
   113  ### pre_update
   114  
   115    executed before an update
   116    Mainly used for validation purpose
   117  
   118    context.resource contains user input data
   119  
   120    Note you can skip DB operation if you set context response in here
   121  
   122  ### pre_update_in_transaction
   123  
   124    same as pre_update but executed in the db transaction
   125    context.transaction contains transaction object for db operation
   126  
   127  ### post_update_in_transaction
   128  
   129    after creation in transaction
   130  
   131  ### post_update
   132  
   133    after update
   134    context.response contains response data.
   135    context.transaction contains transaction object for db operation
   136  
   137  ### pre_delete
   138  
   139    executed before delete
   140    Mainly used for validation purpose
   141  
   142    context.id contains resource id we are trying to delete
   143    context.transaction contains transaction object for db operation
   144  
   145  ### pre_delete_in_transaction
   146  
   147    same as pre_delete but executed in the db transaction
   148    context.transaction contains transaction object for db operation
   149  
   150  ### post_delete_in_transaction
   151  
   152    after creation in transaction
   153  
   154  ### post_delete
   155  
   156    after delete
   157  
   158  ### pre_state_update_in_transaction
   159  
   160    executed before a state update triggered by a backend event
   161  
   162    context.resource contains the resource associated with the update,
   163    context.state contains the state changes,
   164    context.config_version contains the current config version
   165  
   166  ### post_state_update_in_transaction
   167  
   168    as above, but after the state update
   169  
   170  ### pre_monitoring_update_in_transaction
   171  
   172    executed before a monitoring update triggered by a backend event
   173  
   174    context.resource contains the resource associated with the update,
   175    context.monitoring contains the new monitoring information
   176  
   177  ### post_monitoring_update_in_transaction
   178  
   179    as above, but after the monitoring update
   180  
   181  ### notification
   182  
   183    executed when you receive amqp/snmp/cron notification
   184  
   185  
   186  # Go extension
   187  
   188  You can implement Gohan extension by native go.
   189  You can use "go" for code_type and specify your callback id in code.
   190  Also, you can register go struct & call it from javascript.
   191  
   192  ```
   193    extensions:
   194    - code: exampleapp_callback
   195      code_type: go
   196      id: example
   197      path: .*
   198  ```
   199  
   200  ```
   201    //Register go callback
   202    extension.RegisterGoCallback("exampleapp_callback",
   203    	func(event string, context map[string]interface{}) error {
   204    		fmt.Printf("callback on %s : %v", event, context)
   205    		return nil
   206    })
   207   ```
   208  
   209  We have exampleapp with comments in exampleapp directory.
   210  You can also, import github.com/cloudwan/server module and
   211  have your own RunServer method to have whole custom route written in go.