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

     1  ---
     2  meta:
     3    - name: "og:title"
     4      content: "Installation - Goyave"
     5    - name: "twitter:title"
     6      content: "Installation - Goyave"
     7    - name: "title"
     8      content: "Installation - Goyave"
     9  ---
    10  
    11  # Installation
    12  
    13  This guide will walk you through the installation process. The rest of the guide assumes you are using the template project, as it is the recommended option.
    14  
    15  ## Requirements
    16  
    17  - Go 1.13+
    18  - Go modules
    19  
    20  ## Template project
    21  
    22  You can bootstrap your project using the **[Goyave template project](https://github.com/System-Glitch/goyave-template)**. This project has a complete directory structure already set up for you.
    23  
    24  #### Linux / MacOS
    25  
    26  ```
    27  $ curl https://raw.githubusercontent.com/System-Glitch/goyave/master/install.sh | bash -s my-project
    28  ```
    29  
    30  #### Windows (Powershell)
    31  
    32  ```
    33  > & ([scriptblock]::Create((curl "https://raw.githubusercontent.com/System-Glitch/goyave/master/install.ps1").Content)) -projectName my-project
    34  ```
    35  
    36  ---
    37  
    38  Run `go run my-project` in your project's directory to start the server, then try to request the `hello` route.
    39  ```
    40  $ curl http://localhost:8080/hello
    41  Hi!
    42  ```
    43  
    44  There is also an `echo` route, with basic validation of query parameters.
    45  ```
    46  $ curl http://localhost:8080/echo?text=abc%20123
    47  abc 123
    48  ```
    49  
    50  ## From scratch
    51  
    52  ::: warning
    53  Installing your project from scratch is not recommended as you will likely not use the same directory structure as the template project. Respecting the standard [directory structure](./architecture-concepts.html#directory-structure) is important and helps keeping a consistent environment across the Goyave applications.
    54  :::
    55  
    56  If you prefer to setup your project from scratch, for example if you don't plan on using some of the framework's features or if you want to use a different directory structure, you can!
    57  
    58  In a terminal, run:
    59  ```
    60  $ mkdir myproject && cd myproject
    61  $ go mod init my-project
    62  $ go get -u github.com/System-Glitch/goyave/v2
    63  ```
    64  
    65  Now that your project directory is set up and the dependencies are installed, let's start with the program entry point, `kernel.go`:
    66  ``` go
    67  package main
    68  
    69  import (
    70      "my-project/http/route"
    71      "github.com/System-Glitch/goyave/v2"
    72  )
    73  
    74  func main() {
    75      if err := goyave.Start(route.Register); err != nil {
    76        os.Exit(err.(*goyave.Error).ExitCode)
    77      }
    78  }
    79  ```
    80  
    81  ::: tip
    82  `goyave.Start()` is blocking. You can run it in a goroutine if you want to process other things in the background. See the [multi-services](./advanced/multi-services.html) section for more details.
    83  :::
    84  
    85  Now we need to create the package in which we will register our routes. Create a new package `http/route`:
    86  ```
    87  $ mkdir http
    88  $ mkdir http/route
    89  ```
    90  
    91  Create `http/route/route.go`:
    92  ``` go
    93  package routes
    94  
    95  import "github.com/System-Glitch/goyave/v2"
    96  
    97  // Register all the routes
    98  func Register(router *goyave.Router) {
    99  	router.Get("GET", "/hello", hello)
   100  }
   101  
   102  // Handler function for the "/hello" route
   103  func hello(response *goyave.Response, request *goyave.Request) {
   104  	response.String(http.StatusOK, "Hi!")
   105  }
   106  ```
   107  
   108  Here we registered a very simple route displaying "Hi!". Learn more about routing [here](./basics/routing.html).
   109  
   110  ::: tip
   111  Your routes definitions should be separated from the handler functions. Handlers should be defined in a `http/controller` directory.
   112  :::
   113  
   114  Run your server and request your route:
   115  ```
   116  $ go run my-project
   117  
   118  # In another terminal:
   119  $ curl http://localhost:8080/hello
   120  Hi!
   121  ```
   122  
   123  You should also create a config file for your application. Learn more [here](./configuration.html).
   124  
   125  It is a good practice to ignore the actual config to prevent it being added to the version control system. Each developer may have different settings for their environment. To do so, add `config.json` to your `.gitignore`.