gopkg.in/simversity/gottp.v3@v3.0.0-20160401065405-576cf030ca0e/README.md (about)

     1  [![Build Status](https://travis-ci.org/meson10/gottp.svg?branch=master)](https://travis-ci.org/meson10/gottp)
     2  
     3  gottp
     4  =====
     5  
     6  Gottp is not a regular front-end server to do user-facing CSS powered websites. It was designed using backend servers in mind and offers a variety of features like:
     7  
     8  * Background Workers
     9  * Call Aggregation using Non-Blocking or Blocking Pipes. [1]
    10  * Optionally Listens on Unix Domain socket.
    11  * In-built error traceback emails.
    12  * Optional data compression using zlib/gzip.
    13  * Pagination support.
    14  * Automatic listing of all exposed URLs
    15  
    16  [1] Much like Batch requests in Facebook Graph API (https://developers.facebook.com/docs/graph-api/making-multiple-requests)
    17  
    18  
    19  Installation
    20  =============
    21  
    22  Installation is as easy as:
    23  
    24  ```go
    25  go get gopkg.in/meson10/gottp.v3
    26  ```
    27  
    28  Getting Started
    29  ===============
    30  
    31  *A sample application named helloWorld is available inside the tests directory of your checkout*
    32  
    33  To start building a web service using gottp just create a new project with the following structure.
    34  
    35  * conf.go -> Configuration
    36  * main.go -> main Server engine
    37  * urls.go -> Register urls & corresponding handlers.
    38  * handlers.go -> handlers processing the request.
    39  
    40  conf.go
    41  -------
    42  
    43  This section is applicable only when you need to provide application based settings alongside those used by gottp.
    44  
    45  Configuration must implement the Configurer interface.
    46  
    47  Configurer requires two method Sets:
    48  
    49  * MakeConfig(string) which accepts the path of the .cfg file provided as an command line argument.
    50  * GetGottpConfig() which must return the Settings
    51  
    52  ```go
    53  type Configurer interface {
    54      MakeConfig(string)
    55          GetGottpConfig() *GottpSettings
    56  }
    57  ```
    58  
    59  A minimalist extended configuration looks like:
    60  
    61  ```go
    62  import "gopkg.in/meson10/gottp.v3/conf"
    63  
    64  type config struct {
    65      Custom struct {
    66          VarOne string
    67              VarTwo string
    68      }
    69      Gottp conf.GottpSettings
    70  }
    71  
    72  func (self *config) MakeConfig(configPath string) {
    73      if configPath != "" {
    74          conf.MakeConfig(configPath, self)
    75      }
    76  }
    77  
    78  func (self *config) GetGottpConfig() *conf.GottpSettings {
    79      return &self.Gottp
    80  }
    81  
    82  var settings config
    83  ```
    84  
    85  urls.go
    86  -------
    87  
    88  A sample urls.go looks like:
    89  
    90  ```go
    91  package main
    92  
    93  import "gopkg.in/meson10/gottp.v3"
    94  
    95  func init(){
    96      gottp.NewUrl("hello", "/hello/\\w{3,5}/?$", new(handlers.HelloMessage)),
    97  }
    98  ```
    99  
   100  This would match all urls that are like "/hello/world" or "/hello/greet" but NOT /hello/123 and not /hello/yester
   101  
   102  
   103  handlers.go
   104  -----------
   105  
   106  A sample handler looks like:
   107  
   108  ```go
   109  package handlers
   110  
   111  import "gopkg.in/meson10/gottp.v3"
   112  
   113  type HelloMessage struct {
   114    gottp.BaseHandler
   115  }
   116  
   117  func (self *HelloMessage) Get(req *gottp.Request) {
   118      req.Write("hello world")
   119  }
   120  ```
   121  
   122  main.go
   123  -------
   124  
   125  A sample main.go looks like:
   126  
   127  ```go
   128  package main
   129  
   130  import (
   131      "log"
   132      "gopkg.in/meson10/gottp.v3"
   133  )
   134  
   135  func main() {
   136    gottp.MakeServer(&settings)
   137  }
   138  
   139  ```
   140  
   141  Build & Run
   142  -----------
   143  
   144  ```go
   145  go install test && test
   146  ```
   147  
   148  Point your browser to http://127.0.0.1:8005/hello/check
   149  
   150  Should give you a JSON output:
   151  
   152  ```json
   153  {
   154      "data": "hello world",
   155          "message": "",
   156          "status": 200
   157  }
   158  ```
   159  
   160  Configuration
   161  -------------
   162  
   163  Gottp allows you to provide .cfg files via the command line which is as easy as ./binary -config=path_to_cfg
   164  
   165  Default gottp settings is a struct called GottpSettings
   166  
   167  ```go
   168  type GottpSettings struct {
   169      EmailHost     string //SMTP Host to send server Tracebacks.
   170          EmailPort     string //SMTP Port
   171          EmailUsername string //Username or Password to connect with SMTP
   172          EmailPassword string
   173          EmailSender   string   //Sender Name to be used for traceback.
   174          EmailFrom     string   //Verified sender email address like errors@example.com
   175          ErrorTo       []string //List of recipients for tracebacks.
   176          EmailDummy    bool     //Set to True, if Tracebacks should not be sent.
   177          Listen        string   //Address to Listen on default: 127.0.0.1:8005
   178  }
   179  ```
   180  
   181  .cfg sample
   182  -----------
   183  
   184  You can provide a simple configuration file in .cfg format to load the settings which would look like this:
   185  
   186  ```
   187  [custom]
   188  VarOne="one"
   189  VarTwo="two"
   190  
   191  [gottp]
   192  listen="/tmp/custom.sock"
   193  EmailHost="email-smtp.us-east-1.amazonaws.com"
   194  EmailPort="587"
   195  EmailPassword="TDVAGCWCTCTWCTCQ&&*!!*!*!*/NeURB5"
   196  EmailUsername="HelloWorldSample"
   197  EmailSender="My Gottp Server"
   198  EmailFrom="errors@example.com"
   199  ErrorTo="dev@example.com"
   200  EmailDummy=false
   201  ```
   202  
   203  URLs
   204  ----
   205  
   206  Urls are of type gottp.Url
   207  
   208  ```go
   209  type Url struct {
   210      name    string //shortname of the url
   211      url     string //provided regular pattern
   212      handler func(r *Request) //ReuqestHandler
   213      pattern *regexp.Regexp //Compiled Regular Expression
   214  }
   215  ```
   216  
   217  URLs can be constructed using gottp.NewUrl which accepts shortname, regular expression & Handler interface implementor respectively.
   218  
   219  
   220  Request Handler
   221  ---------------
   222  
   223  A request handler must implement the Handler Interface which must expose the following methods
   224  
   225  ```go
   226  type Handler interface {
   227  	Get(request *Request)
   228  	Put(request *Request)
   229  	Post(request *Request)
   230  	Delete(request *Request)
   231  	Head(request *Request)
   232  	Options(request *Request)
   233  	Patch(request *Request)
   234  }
   235  
   236  ```
   237  
   238  gottp.BaseHandler has most common HTTP requests as method sets. So, If a struct uses BaseHandler as an embedded type it is sufficient to qualify as a Handler. To expose a new HTTP method for a URL type, implement the HTTP method set in the handler struct. See the Example below:
   239  
   240  ```go
   241  type helloMessage struct {
   242  	gottp.BaseHandler
   243  }
   244  
   245  func (self *helloMessage) Get(req *gottp.Request) {
   246  	req.Write("hello world - GET")
   247  }
   248  
   249  func (self *helloMessage) Post(req *gottp.Request) {
   250  	req.Write("hello world - POST")
   251  }
   252  
   253  ```
   254  
   255  
   256  Request
   257  -------
   258  
   259  Request exposes a few method structs:
   260  ```go
   261  GetArguments() *utils.Q
   262  ```
   263  _
   264  Returns a map of all arguments passed to the request. This includes Body arguments in case of a PUT/POST request, url GET arguments and named arguments captured in URL regular expression. You can call this over as it handles caching internally.
   265  _
   266  ```go
   267  GetArgument(key string) interface{}
   268  ConvertArguments(dest interface{})
   269  ConvertArgument(key string, dest interface{})
   270  ```
   271  These methods come quite handy when you have to deal with the arguments. For a request with GET arguments that look like:
   272  
   273  ```go
   274  ?abc=world&param=1&check=0
   275  ```
   276  
   277  You can either fetch individual arguments like:
   278  
   279  ```go
   280  abcVar, _ := req.GetArgument("abc").(string)
   281  log.Println(abcVar)
   282  ```
   283  
   284  Or initialize a struct to convert all the arguments:
   285  
   286  ```go
   287  type params struct {
   288    abc string
   289    param int
   290    check int
   291  }
   292  
   293  getArgs := params{}
   294  req.ConvertArguments(&getArgs)
   295  log.Println(getArgs.abc)
   296  ```
   297  
   298  Available URLs
   299  ==============
   300  
   301  With backends powered by Gottp, consumers can simply access http://{host}:{port}/urls to fetch a json of all URLs exposed by the application. URLs are returned as a map of key: pattern where the key is the human-readable-identifier provided at the time of constructing URLS and the pattern is the URL regular expression pattern.
   302  
   303  This helps in preventing hard coding of endpoints where consumers can fetch the URLs at application start time and reconstruct URLs using the indentifiers.
   304  
   305  Sample Output:
   306  
   307  ```json
   308  {
   309    "data": {
   310      "hello": "/hello/\\w{3,5}/?$"
   311    },
   312    "message": "",
   313    "status": 200
   314  }
   315  ```
   316  
   317  Pipes
   318  =====
   319  
   320  All gottp based servers allow clubbing of requests to be executed together. This is a very handy feature that is used for clubbing calls that need to processed one after the other. It can save the network handshaking costs as the handshake is only performed once and this does not effect server performance as all gottp requests are performed as goroutines.
   321  
   322  Sequential Pipes
   323  ----------------
   324  
   325  Example usage of such calls is:
   326  
   327  1. Create a Comment
   328  2. Mark the parent notification as read
   329  3. Send out notification to the parent autho
   330  4. Churn the new activity feed as per edge rank.
   331  
   332  Using Pipes, call 1, 2, 3 & 4 can be combined into a single request to the server.
   333  
   334  All the requests would sequentially evaluated and data would be returned in the same order as requested.
   335  
   336  To submit a PIPE request issue a POST call on /pipe (available by default).
   337  
   338  Async Pipes
   339  -----------
   340  
   341  Async pipes behave the same way, except the requests are executed in parallel
   342  as go-routines. Async pipes are as fast as the slowest call in your request
   343  stack.
   344  
   345  Despite parallel execution they return the data in the same order as requested.
   346  
   347  To submit an Async PIPE request issue a POST call on /async-pipe (available by default)
   348  
   349  
   350  Pipe Request Object
   351  -------------------
   352  
   353  Requests submitted as PIPEs should again be a valid JSON dump of
   354  
   355  
   356  ```json
   357  {
   358      "stack": [
   359          {"url": "/url1", "method": "POST", "data": {"sample": "json"}},
   360          {"url": "/url2", "method": "GET", "data": {"get": "argument"}},
   361          ...
   362      ]
   363  }
   364  ```
   365  
   366  Error Reporting
   367  ===============
   368  
   369  Gottp can send error tracebacks if a request failed.
   370  This can be enabled by setting EmailDummy as false in the gottp section of cfg.
   371  
   372  A sample traceback email looks like this:
   373  
   374  ![Sample Traceback](http://test.simversity.com.s3.amazonaws.com/original_image/142052802943405598970493/emailtraceback.png)
   375  
   376  TODO: Expose a way to use custom email templates.
   377  
   378  Background Workers
   379  ==================
   380  
   381  Gottp now supports background workers. Background workers allow parallel background execution of long running processes.
   382  
   383  There are a few advantage of using gottp managed background worker over simply spawning a goroutine:
   384  
   385   * Gottp Workers are autoamtically recovered and re-spawned in case of Panic. You also get a fancy error traceback provided you have configured the email tracebacks, discussed earlier.
   386   * Gottp Workers do not quit abruptly when main thread receives an interrupt. This is beneficial to process cleanups and other vital exit routines.
   387   * Gottp Workers are provided with a default timeout of 10 seconds and a worker that seems to be taking forever is then terminated.
   388   * Gottp gracefully handles all process interrupts and signals the background worker accordingly.
   389  
   390  NOTE: At this moment, gottp only supports the option of one background worker.
   391  
   392  Usage:
   393  
   394  ```go
   395  import "gopkg.in/meson10/gottp.v3"
   396  
   397  func main() {
   398  	gottp.RunWorker(func(exitChan bool) {
   399  		log.Println("Do some background work here");
   400  	})
   401  }
   402  ```