github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/docs_src/src/guide/upgrade-guide.md (about)

     1  ---
     2  meta:
     3    - name: "og:title"
     4      content: "Upgrade guide - Goyave"
     5    - name: "twitter:title"
     6      content: "Upgrade guide - Goyave"
     7    - name: "title"
     8      content: "Upgrade guide - Goyave"
     9  ---
    10  
    11  # Upgrade Guide
    12  
    13  Although Goyave is developed with backwards compatibility, breaking changes can happen, especially in the project's early days. This guide will help you to upgrade your applications using older versions of the framework. Bear in mind that if you are several versions behind, you will have to follow the instructions for each in-between versions.
    14  
    15  [[toc]]
    16  
    17  ## v2.x.x to v3.0.0
    18  
    19  ### Routing changes
    20  
    21  Routing has been improved by changing how validation and route-specific middleware are registered. The signature of the router functions have been simplified by removing the validation and middleware parameters from `Route()`, `Get()`, `Post()`, etc. This is now done through two new chainable methods on the `Route`:
    22  
    23  ```go
    24  router.Post("/echo", hello.Echo, hellorequest.Echo)
    25  
    26  // Becomes
    27  router.Post("/echo", hello.Echo).Validate(hello.EchoRequest)
    28  ```
    29  
    30  ```go
    31  router.Post("/echo", hello.Echo, nil, middleware.Trim, middleware.Gzip())
    32  
    33  // Becomes
    34  router.Post("/echo", hello.Echo).Middleware(middleware.Trim, middleware.Gzip())
    35  ```
    36  
    37  ```go
    38  router.Post("/echo", hello.Echo, hellorequest.Echo, middleware.Trim)
    39  
    40  // Becomes
    41  router.Post("/echo", hello.Echo).Validate(hello.EchoRequest).Middleware(middleware.Trim)
    42  ```
    43  
    44  ### Convention changes
    45  
    46  This release brought changes to the conventions. Although your applications can still work with the old ones, it's recommended to make the change.
    47  
    48  - Move `validation.go` and `placeholders.go` to a new `http/validation` package. Don't forget to change the `package` instruction in these files.
    49  - In `kernel.go`, import your `http/validation` package instead of `http/request`.
    50  - Validation rule sets are now located in a `request.go` file in the same package as the controller. So if you had `http/request/productrequest/product.go`, take the content of that file and move it to `http/controller/product/request.go`. Rule sets are now named after the name of the controller handler they will be used with, and end with `Request`. For example, a rule set for the `Store` handler will be named `StoreRequest`. If a rule set can be used for multiple handlers, consider using a name suited for all of them. The rules for a store operation are often the same for update operations, so instead of duplicating the set, create one unique set called `UpsertRequest`. You will likely just have to add `Request` at the end of the name of your sets.
    51  - Update your route definition by changing the rule sets you use.
    52  ```go
    53  router.Post("/echo", hello.Echo, hellorequest.Echo)
    54  
    55  // Becomes
    56  router.Post("/echo", hello.Echo).Validate(hello.EchoRequest)
    57  ```
    58  
    59  ### Validation changes
    60  
    61  Although the validation changes are internally huge, there is only a tiny amount of code to change to update your application. You will have to update all your handlers accessing the `request.Rules` field. This field is no longer a `validation.RuleSet` and has been changed to `*validation.Rules`, which will be easier to use, as the rules are already parsed. Refer to the [alternative validation syntax](./basics/validation.html#alternative-syntax) documentation for more details about this new structure.
    62  
    63  - The following rules now pass if the validated data type is not supported: `greater_than`, `greater_than_equal`, `lower_than`, `lower_than_equal`, `size`.
    64  
    65  ### Configuration changes
    66  
    67  The new configuration system does things very differently internally, but should not require too many changes to make your project compatible. First, you will have to update your configuration files. Here is an example of configuration file containing all the core entries:
    68  
    69  ```json
    70  {
    71    "app": {
    72      "name": "goyave_template",
    73      "environment": "localhost",
    74      "debug": true,
    75      "defaultLanguage": "en-US"
    76    },
    77    "server": {
    78      "host": "127.0.0.1",
    79      "maintenance": false,
    80      "protocol": "http",
    81      "domain": "",
    82      "port": 8080,
    83      "httpsPort": 8081,
    84      "timeout": 10,
    85      "maxUploadSize": 10,
    86      "tls": {
    87        "cert": "/path/to/cert",
    88        "key": "/path/to/key"
    89      },
    90    },
    91    "database": {
    92      "connection": "mysql",
    93      "host": "127.0.0.1",
    94      "port": 3306,
    95      "name": "goyave",
    96      "username": "root",
    97      "password": "root",
    98      "options": "charset=utf8&parseTime=true&loc=Local",
    99      "maxOpenConnections": 20,
   100      "maxIdleConnections": 20,
   101      "maxLifetime": 300,
   102      "autoMigrate": false
   103    }
   104  }
   105  ```
   106  
   107  If you were using any of the configuration entries above in your code, you should update the keys used in the calls of `config.Get()`, `config.GetString()`, `config.Bool()` and `config.Has()`. Keys are now **dot-separated** paths. For example, to access the database `host` entry, the key is `database.host`.
   108  
   109  For more information, refer to the [configuration reference](./configuration.html#configuration-reference).
   110  
   111  If you are using the `auth` package (basic auth, JWT), you will need to update your configuration entries too.
   112  
   113  - `authUsername` becomes `auth.basic.username`
   114  - `authPassword` becomes `auth.basic.password`
   115  - `jwtExpiry` becomes `auth.jwt.expiry`
   116  - `jwtSecret` becomes `auth.jwt.secret`
   117  
   118  ```json
   119  {
   120    ...
   121    "auth": {
   122      "jwt": {
   123        "expiry": 300,
   124        "secret": "jwt-secret"
   125      },
   126      "basic": {
   127        "username": "admin",
   128        "password": "admin"
   129      }
   130    }
   131  }
   132  ```
   133  
   134  Finally, `config.Register()` function has changed signature. See the [configuration documentation](./configuration.html#custom-config-entries) for more details on how to migrate.
   135  
   136  ### Minor changes
   137  
   138  - Recovery middleware now correctly handles panics with a `nil` value. You may have to update your custom status handler for the HTTP `500` error code.
   139  - Log `Formatter` now receive the length of the response (in bytes) instead of the full body.
   140    - `log.Formatter` is now `func(now time.Time, response *goyave.Response, request *goyave.Request, length int) string`.
   141    - If you were just using `len(body)`, just replace it with `length`.
   142    - If you were using the content of the body in your logger, you will have to implement a [chained writer](./basics/responses.html#chained-writers).
   143  
   144  ## v1.0.0 to v2.0.0
   145  
   146  This first update comes with refactoring and package renaming to better fit the Go conventions.
   147  
   148  - `goyave.Request.URL()` has been renamed to `goyave.Request.URI()`.
   149      - `goyave.Request.URL()` is now a data accessor for URL fields.
   150  - The `helpers` package has been renamed to `helper`.
   151      - The `filesystem` package thus has a different path: `github.com/System-Glitch/goyave/v2/helper/filesystem`.
   152  
   153  ::: tip
   154  Because this version contains breaking changes. Goyave had to move to `v2.0.0`. You need to change the path of your imports to upgrade.
   155  
   156  Change `github.com/System-Glitch/goyave` to `github.com/System-Glitch/goyave/v2`.
   157  :::