.formatter.exs (91, 2023-11-07)
Dockerfile (1766, 2023-11-07)
LICENSE (18092, 2023-11-07)
assets/ (0, 2023-11-07)
assets/css/ (0, 2023-11-07)
assets/css/app.css (2097, 2023-11-07)
assets/css/phoenix.css (10861, 2023-11-07)
assets/js/ (0, 2023-11-07)
assets/js/app.js (1742, 2023-11-07)
assets/vendor/ (0, 2023-11-07)
assets/vendor/topbar.js (5087, 2023-11-07)
config/ (0, 2023-11-07)
config/config.exs (1242, 2023-11-07)
config/dev.exs (2164, 2023-11-07)
config/prod.exs (1738, 2023-11-07)
config/runtime.exs (1774, 2023-11-07)
config/test.exs (530, 2023-11-07)
fly.toml (727, 2023-11-07)
lib/ (0, 2023-11-07)
lib/app.ex (243, 2023-11-07)
lib/app/ (0, 2023-11-07)
lib/app/application.ex (950, 2023-11-07)
lib/app_web.ex (2378, 2023-11-07)
lib/app_web/ (0, 2023-11-07)
lib/app_web/controllers/ (0, 2023-11-07)
lib/app_web/controllers/init_controller.ex (964, 2023-11-07)
lib/app_web/controllers/page_controller.ex (493, 2023-11-07)
lib/app_web/endpoint.ex (1337, 2023-11-07)
lib/app_web/gettext.ex (699, 2023-11-07)
lib/app_web/router.ex (572, 2023-11-07)
lib/app_web/telemetry.ex (1390, 2023-11-07)
lib/app_web/templates/ (0, 2023-11-07)
lib/app_web/templates/init/ (0, 2023-11-07)
lib/app_web/templates/init/index.html.eex (1162, 2023-11-07)
... ...
A fully functional **demo** app
showing interaction between an
**`Elixir`** (**`Phoenix`**) App
and **`Gogs`** server
using the
[`gogs`](https://github.com/dwyl/gogs) package.
**_Step-by-step_ tutorial** showing you how to do it yourself!
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/dwyl/gogs-demo/ci.yml?label=build&style=flat-square&branch=main)
[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/gogs-demo/main.svg?style=flat-square)](https://codecov.io/github/dwyl/gogs-demo?branch=main)
[![Hex.pm](https://img.shields.io/hexpm/v/gogs?color=brightgreen&style=flat-square)](https://hex.pm/packages/gogs)
[![docs](https://img.shields.io/badge/docs-maintained-brightgreen?style=flat-square)](https://hexdocs.pm/gogs/api-reference.html)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](https://github.com/dwyl/gogs-demo/issues)
[![HitCount](https://hits.dwyl.com/dwyl/gogs-demo.svg)](https://hits.dwyl.com/dwyl/gogs-demo)
# _Why_?
As software engineers we are
[**_always learning_ `new` things**](https://twitter.com/nelsonic/status/913811339709173760).
When we learn,
we love having _detailed docs and **examples**_
that explain _exactly_ how to get up-and-running.
**`@dwyl`** we _write_ examples because we want them _ourselves_.
**_Comprehensive_ docs/tutorials**
are a _gift_ to our future selves and teammates.
They allow us to get up-to-speed
or remember what's going on
especially when returning to a project after a while.
We constantly refer back to them
and update them when required.
If you find them useful, please the repo to let us know.
# _What_?
This project is a _barebones_ demonstration
of using
[`gogs`](https://github.com/dwyl/gogs)
in any **`Phoenix`** App.
It's intended to be beginner-friendly
and focus on showcasing **_one_ thing**.
It can be used as the basis for another app
or you can borrow chunks of setup/code.
# _Who_?
This demo is intended for people of all Elixir/Phoenix skill levels.
Following all the steps in this example should take around **`10 minutes`**.
If you get stuck, please _don't suffer_ in silence!
**Get help** by opening an issue:
[github.com/dwyl/**gogs-demo/issues**](https://github.com/dwyl/gogs-demo/issues)
# _How?_
### 0. Prerequisites
_Before_ you start,
make sure you have the following installed:
1. `Elixir`: https://elixir-lang.org/install.html
New to `Elixir`? see:
[github.com/dwyl/**learn-elixir**](https://github.com/dwyl/learn-elixir)
2. `Phoenix`: https://hexdocs.pm/phoenix/installation.html
New to `Phoenix`? see:
[github.com/dwyl/**learn-phoenix-framework**](https://github.com/dwyl/learn-phoenix-framework)
### 1. Create a New `Phoenix` App
For this example,
we are creating a _basic_ **`Phoenix`** App
without the live dashboard or mailer (email)
or `Ecto` (Postgres database)
because we don't need those components
in order to showcase the `gogs` package.
```sh
mix phx.new app --no-ecto --no-dashboard --no-mailer
```
When prompted to install dependencies:
```sh
Fetch and install dependencies? [Yn]
```
Type `y` and hit the `[Enter]` key to install.
You should see something like this:
```sh
* running mix deps.get
* running cd assets && npm install && node node_modules/webpack/bin/webpack.js
* running mix deps.compile
```
#### Checkpoint: Working `Phoenix` App
Change into the directory of your newly created `Phoenix` app
```sh
cd app
```
And start the app:
```sh
mix setup
mix phx.server
```
You should see output similar to the following:
```sh
Generated app app
[info] Running AppWeb.Endpoint with cowboy 2.9.0 at 127.0.0.1:4000 (http)
[info] Access AppWeb.Endpoint at http://localhost:4000
[debug] Downloading esbuild from https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-0.14.29.tgz
[watch] build finished, watching for changes...
```
That confirms the app is working.
Open your web browser to the URL: http://localhost:4000
You should see the default **`Phoenix`** home page:
So far so good.
#### 1.1 Clear out `page` template
Before we continue,
let's do a clear out of the `page` template:
`lib/app_web/templates/page/index.html.heex`
Open the file and delete the contents so it's completely empty.
With the `Phoenix` server running (`mix phx.server`),
the page should refresh and now look like this:
![phoenix-app-clean-slate](https://user-images.githubusercontent.com/194400/168068678-60b8eab1-ee7c-49a7-81d8-c8ecc22eb123.png)
#### 1.2 Fix the Failing Test
If you run the tests after the previous step:
```sh
mix test
```
You will see output similar to the following:
```sh
1) test GET / (AppWeb.PageControllerTest)
test/app_web/controllers/page_controller_test.exs:4
Assertion with =~ failed
code: assert html_response(conn, 200) =~ "Welcome to Phoenix!"
left: "\n\n \n
\n \n
\n
App · Phoenix Framework etc."
right: "Welcome to Phoenix!"
stacktrace:
test/app_web/controllers/page_controller_test.exs:6: (test)
Finished in 0.1 seconds (0.08s async, 0.07s sync)
3 tests, 1 failure
```
This is because we removed the block of text that the test expects to be on the page.
Easy enough to fix by updating the assertion in the test.
Open the `test/app_web/controllers/page_controller_test.exs` file
and replace the line:
```elixir
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
```
With the following:
```elixir
assert html_response(conn, 200) =~ "Get Started"
```
Once you save the file and re-run the tests `mix test`,
they should pass:
```sh
...
Finished in 0.1 seconds (0.08s async, 0.06s sync)
3 tests, 0 failures
```
With that out-of-the way,
let's crack on with the actual demo!
### 2. Add `gogs` to `deps`
Open the
[`mix.exs`]()
file,
locate the `defp deps do` section and add the following line:
```elixir
{:gogs, "~> 1.0.1"},
```
Once you've saved your `mix.exs` file,
e.g:
[`mix.exs#L55-L56`](https://github.com/dwyl/gogs-demo/blob/58c6bf0f7b96a370f6a90408526f6e335014025a/mix.exs#L55-L56)
run:
```sh
mix deps.get
```
With the dependency installed,
we can now setup.
### 3. Setup: Environment Variables
To get the **`gogs`** package working in your **`Phoenix`** App,
you will need **4 environment variables**.
See:
[**`.env_sample`**](https://github.com/dwyl/gogs/blob/main/.env_sample)
for a sample.
1. `GOGS_URL` - the domain where your Gogs Server is deployed,
without the protocol,
e.g: `gogs-server.fly.dev`
2. `GOGS_ACCESS_TOKEN` - the REST API Access Token
See:
[gogs-server#connect-via-rest-api-https](https://github.com/dwyl/gogs-server#connect-via-rest-api-https)
3. `GOGS_SSH_PORT` - The TCP port allocated to SSH on your Gogs Server,
in our case it's: `10022`.
4. `GOGS_SSH_PRIVATE_KEY_PATH` - absolute path to the `id_rsa` file
on your `localhost` or `Phoenix` server instance.
> If you're new to Environment Variables
> Please see:
> [github.com/dwyl/**learn-environment-variables**](https://github.com/dwyl/learn-environment-variables)
#### Context: `Gogs` Server on Fly.io
In _our_ case our **`Gogs`** Server instance
is deployed to [fly.io](https://fly.io/)
at:
[gogs-server.fly.dev](https://gogs-server.fly.dev/)
To understand how this was deployed,
please see:
[github.com/dwyl/**gogs-server**](https://github.com/dwyl/gogs-server)
### 4. Create Function to Interact with Gogs Repo
As noted in the first step above,
the homepage of our app
is the default `Phoenix` homepage.
In this section we're going to change that!
Open the `lib/app_web/controllers/page_controller.ex` file.
You should see the following:
```elixir
defmodule AppWeb.PageController do
use AppWeb, :controller
def index(conn, _params) do
render(conn, "index.html")
end
end
```
Inside the file,
replace the `index/2` function with the following:
```elixir
def index(conn, _params) do
org_name = "demo-org"
repo_name = "hello-world"
file_name = "README.md"
{:ok, %{body: raw_html}} =
Gogs.remote_render_markdown_html(org_name, repo_name, file_name)
render(conn, "index.html", html: raw_html)
end
```
### 5. Update the Template to Display the Text
Open the file:
`lib/app_web/templates/page/index.html.heex`
Insert the following line:
```html
<%= raw(@html) %>
```
Now you will see the `Markdown` rendered in the template:
![rendered-markdown](https://user-images.githubusercontent.com/194400/168069048-15dd5b50-235c-4cbe-9f3e-fb306933e17c.png)
#### Recap!
At this point we have demonstrated
rendering a Markdown (`README.md`)
file hosted on a `Gogs` server
in a `Phoenix` app using the `gogs` package.
This is already cool,
but it doesn't even scratch the surface of what's possible!
At this point in the journey,
you can either chose
## 6. _Deploy_ to Fly.io
The `Dockerfile`, `fly.toml` and `config/runtime.exs` files
can be used to deploy to Fly.io:
https://gogs-demo.fly.dev
![image](https://user-images.githubusercontent.com/194400/168398126-05c147ef-7fcc-4867-8a05-c89669027674.png)