Skip to content

Alpha version for a template for writing mu.semte.ch services in Elixir using plug_cowboy 2.1.

Notifications You must be signed in to change notification settings

langens-jonathan/elixir-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elixirtemplate

Alpha version for a template for writing mu.semte.ch services in Elixir using plug_cowboy 2.1.

Beware

This repository contains an alpha version for a template. This is not endorsed by mu.semte.ch!

TODOS

The following items still need to be done to make this service a proper microservice template: 3. SPARQL Escaping

Nice to have

  1. Live Reload

Docker Compose

To add an service based on this template to a mu.semte.ch app you can add the following snippet to your docker compose file. This snippet assumes you have created a new folder called my-new-service and have at the very least added an app.ex file in that folder as outlined in 'creating a new microservice' below.

  newService:
    flowofcontrol/elixir-template:alpha
    volumes:
      - ./my-new-service:/app/project

Creating a new microservice

Create a new folder. Add the following Dockerfile:

FROM flowofcontrol/elixir-template:alpha
LABEL maintainer="[email protected]"
ADD . /app/project/

Create your microservice in app.ex:

defmodule Service.Router do
  use Plug.Router

  plug :match
  plug :dispatch

  get "/" do
    send_resp(conn, 200, "Welcome")
  end

  match _ do
    send_resp(conn, 404, "Oops!")
  end
end

Querying

SELECT

SELECT queries can be done in the following way:

"SELECT DISTINCT(?predicate) WHERE {?s ?predicate ?type}"
|> Support.query

UPDATE

UPDATE queries can be done in the following way:

"INSERT DATA {
  GRAPH <#{default_graph()}> {
  <http://example.com/example/1> rdf:type <http://example.com/Example> .
  }
}"
|> Support.update

UUID's

To generate a uuid there is a helper function:

Support.generate_uuid()

Dependencies

If you want to add dependencies you will need to provide a complete mix.exs file for this project. Create a mix.exs file with the following content:

defmodule Elixirtemplate.MixProject do
  use Mix.Project

  def project do
    [
      app: :elixirtemplate,
      version: "0.1.0",
      elixir: "~> 1.8",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger],
      mod: {Elixirtemplate.Application, []}
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:plug_cowboy, "~> 2.1"},
      {:poison, "~> 4.0"},
      {:uuid, "~> 1.1"},
      {:httpoison, "~> 1.5"}
    ]
  end
end

Custom Modules

If you want to create your own custom modules just add a folder called lib/ in the root folder for the microservice. Then you can create modules here an import/use them in your app.ex as normal.

Custom Module Example

For instance if I wanted to create a module called TestModule then I would do:

mkdir lib
cd lib
touch TestModule.ex

And then add the following in the TestModule.ex file:

defmodule TestModule do
  def test_function() do
    "..."
  end
end

After this I could have a route in app.ex such as:

  get "/testmodule" do
    send_resp(conn, 200, TestModule.test_function())
  end

Advanced use

If you have dependencies that you want compiled before the service is launched with docker-compose you can use the following Dockerfile:

FROM flowofcontrol/elixir-template:alpha
LABEL maintainer="[email protected]"
ADD . /app/project/
RUN mix run

Template Development

Adding dependencies

To add dependencies to the template they need to be added in 3 places:

  1. ./mix.exs
  2. ./project/mix.exs
  3. README.md

About

Alpha version for a template for writing mu.semte.ch services in Elixir using plug_cowboy 2.1.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published