Skip to content

Commit

Permalink
docs: Address feedback
Browse files Browse the repository at this point in the history
- readme.md: Mention compatible platforms and the ones it has been tested with.
- create-gin-webapp.md:
  - Add instructions for installing cURL on Windows in the environment setup.
  - Explain the reason for initialising the project directory with the domain
    and username in the file path.
  - Add instructions for creation of project folder on Windows.
  - Minor rewording for `dep init`.
- deploy-to-elastic-beanstalk.md:
  - Remove section about buggy `.gitignore` beahavior after `eb init`.
  - Add command for opening app URL using `eb open`.
  - Reword section on loading $PORT from env.
  - Fix grammatical error.
  • Loading branch information
sudo-suhas committed Dec 7, 2017
1 parent 9e4333e commit c3b62e7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 34 deletions.
73 changes: 71 additions & 2 deletions docs/create-gin-webapp.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,82 @@ this - [`create-gin-webapp`][tag-create-gin-webapp].
* Install Go from here - https://golang.org/dl/
* Install `dep`, a Go dependency management tool using instructions described
here - [golang/dep#setup][dep-setup].
* If you are on Windows, you might not have `cURL` installed. Refer to this
Stack Overflow post to install it - https://stackoverflow.com/a/16216825. Note
that you would also need to setup TLS certificate verification as explained
here - https://superuser.com/a/442797. Since `cURL` is only used for a couple
of simple steps which could just as well be done manually, you can choose to
skip setting up `cURL`. One such example is
[copying a file from GitHub to our project](#implement-the-webapp).

### Initialise git repo

Create the project folder inside your Go workspace and initialise the git repo:
In Go, where we create the project folder is important. Because a package's
import path corresponds to its location inside the workspace. For this reason,
it is recommended to incude the hosting domain and user name in the project path
to ensure that the import path is unique.

<details>

<summary>Read more about import paths</summary>

https://golang.org/doc/code.html#ImportPaths

> If you keep your code in a source repository somewhere, then you should use
> the root of that source repository as your base path. For instance, if you
> have a GitHub account at github.com/user, that should be your base path.
>
> Note that you don't need to publish your code to a remote repository before
> you can build it. It's just a good habit to organize your code as if you will
> publish it someday. In practice you can choose any arbitrary path name, as
> long as it is unique to the standard library and greater Go ecosystem.
https://dave.cheney.net/2014/12/01/five-suggestions-for-setting-up-a-go-project

> In other languages it is quite common to ensure your package has a unique
> namespace by prefixing it with your company name, say `com.sun.misc.Unsafe`.
> If everyone only writes packages corresponding to domains that they control,
> then there is little possibility of a collision.
>
> In Go, the convention is to include the location of the source code in the
> package’s import path, ie
>
> ```
> $GOPATH/src/github.com/golang/glog
> ```
https://blog.golang.org/organizing-go-code
> #### Choose a good import path (make your package "go get"-able)
>
> Sometimes people set `GOPATH` to the root of their source repository and
> put their packages in directories relative to the repository root, such as
> `"src/my/package"`. On one hand, this keeps the import paths short
> (`"my/package"` instead of `"github.com/me/project/my/package"`), but on the
> other it breaks `go get` and forces users to re-set their `GOPATH` to use the
> package. Don't do this.
</details>
**Create the project folder inside your Go workspace:**
```shell
$ mkdir -p $GOPATH/src/github.com/<USERNAME>/go-beanstalk-gin && cd $_
```
_If you are on Windows, do this instead:_

```batch
λ md %gopath%\src\github.com\<USERNAME>\go-beanstalk-gin
λ cd %gopath%\src\github.com\<USERNAME>\go-beanstalk-gin
```

Note: These commands will create parent directories as needed.

**Initialise the git repo:**

```shell
$ git init
Initialized empty Git repository in E:/go/src/github.com/sudo-suhas/go-beanstalk-gin/.git/

Expand Down Expand Up @@ -70,7 +138,8 @@ For this demo, I'll use a basic starting template from `gin` examples:

### `dep init`

Initialise `dep` to be able to vendor the dependencies:
Initialise the project using `dep` which will parse dependencies, write manifest
and lock files, and vendor the dependencies:

```shell
$ dep init
Expand Down
45 changes: 13 additions & 32 deletions docs/deploy-to-elastic-beanstalk.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,31 +143,6 @@ It also adds the following to our `.gitignore`:
+
```

Oddly enough, although these rules should ignore the newly created file
`.elasticbeanstalk/config.yml`, it does not:

```shell
git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .gitignore
Untracked files:
(use "git add <file>..." to include in what will be committed)
.elasticbeanstalk/
no changes added to commit (use "git add" and/or "git commit -a")
```

However, if we make any changes to `.gitignore`, the file gets ignored. Maybe a
`git` bug?

### `eb create`

The next step is to create an environment for our application.
Expand Down Expand Up @@ -205,17 +180,23 @@ INFO: Added instance [i-XXXXXXXXXXXXXXXXX] to your environment.
INFO: Successfully launched environment: go-beanstalk-gin-dev
```

You should be able to see something like this after the application is deployed:
After the application has been successfully deployed, open the public URL of
your website in the browser:

```shell
$ eb open
```

You should be able to see something like this:

![eb sample app](images/eb-sample-app.jpg)

### Load port from environment

By default, Elastic Beanstalk configures the nginx proxy to forward requests to
our application on port `5000`. We can override the default port by setting the
`PORT` environment property to the port on which our main application listens.
So let us modify `application.go` to load the `port` from the environment and
default to `5000` otherwise:
By default, Elastic Beanstalk configures the `nginx` proxy to forward requests
to our application on port `5000`. It is possible to override the default port
by setting the environment property `$PORT`. So let us modify `application.go`
to load the `port` from the environment and default to `5000` otherwise:

```diff
@@ -1,6 +1,9 @@
Expand Down Expand Up @@ -273,7 +254,7 @@ didn't help either. Maybe because the folder structure did not exist.
The solution is to add a `Buildfile` to take control of the build process.
Docs - http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/go-buildfile.html.

The build process need to do the following:
The build process needs to do the following:

* Download a versioned release of `dep` if required.
* Setup a temporary Go workspace and move our files to a project under it.
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ This project demonstrates the deployment of a simple webapp built using
[`gin`][gin] to [Elastic Beanstalk][elastic-beanstalk]. Dependencies are
managed using [`dep`][dep].

This tutorial should be usable on Windows and *nix. Most of the tools, like the
[Go tool][go-tool] and [EB CLI][eb-cli] are cross platform compatible. For some
of the commands which aren't available on Windows, alternate commands have been
included. I successfully tested it on Windows 10 and Ubuntu.

## Docs

The steps have been divided and documented in the following stages:
Expand All @@ -20,4 +25,6 @@ MIT © [Suhas Karanth][sudo-suhas]
[gin]: https://github.com/gin-gonic/gin
[elastic-beanstalk]: https://aws.amazon.com/documentation/elastic-beanstalk/
[dep]: https://github.com/golang/dep
[go-tool]: https://golang.org/cmd/go/
[eb-cli]: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3.html
[sudo-suhas]: https://github.com/sudo-suhas

0 comments on commit c3b62e7

Please sign in to comment.