• Stars
    star
    12
  • Rank 1,597,372 (Top 32 %)
  • Language
    Crystal
  • Created over 8 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Class based Http APIs in crystal

Melon

Class based Http APIs in crystal

Description

This is a proof of concept of creating a shard that allows people to create APIs with classes with the following fetaures:

  • DSL for building the APIs (get, put, post, etc...)
  • Inherit form an other APIs
  • Mounting other APIs at an endpoint
  • Inspectable routes and APIs for documentation

Example:

require "../src/melon"

USERS = [
  {name: "John Doe"},
  {name: "Someone Else"},
]

class Users < Melon::Api
  description "Users Endpoint"

  get description: "Get all users" do
    json USERS
  end
end

class Root < Melon::Api
  description "My Awesome API"

  get description: "Simple GET request." do
    ok "text/plain", "Hello there!"
  end

  post do
    ok "text/plain", "You have posted something."
  end

  mount Users, "users"
end

Melon.print_routes Root
Melon.listen Root, 8080

# Root - My Awesome API
# ----------------------------------------
# โ”œโ”€ GET - /         # Simple GET request.
# โ”œโ”€ POST - /
# โ””โ”€โ”ฌโ”€ API - /users  # Users Endpoint
#   โ””โ”€ GET - /       # Get all users
# ----------------------------------------
# Listening on http://0.0.0.0:8080

Explanation

This implementation is heavily uses macros and a pattern matching to make it work:

  • macros create a subclass class Route%id < Route # Route__temp_25
  • macros create overloaded methods with the subclass def handle_route(id : Route__temp_25)
  • macros save the instances of these classes in a registry for access with metadata Registry[path] = Route%id.new "some metadata"
  • overloaded methods are run with the subclass from the matching route in the registry instance.handle_route(Registry[path])
  • a fallback implemention is needed for matching the base class def handle_route(id : Route) for compability (to actually compile) which will never be called
  • all created routes are inspectable in the registry

This behavior used can further explained with the following code:

# Define a class
class A
end

# Define a registry
REGISTRY = {} of String => A

# Define the which will have the DSL
class B
  # Define the DSL method
  macro make_print(key, text)
    # Create a sub class with a unique name
    class A%name < A
    end

    # Save an instance of that class in the registry
    REGISTRY[{{key}}] = A%name.new

    # Create an overloaded method that responds to that class only
    def handle_print(id : A%name)
      # Run things here
      puts {{text}}
    end
  end

  # Create a fallback method for compability
  def handle_print(id : A)
    puts "fallback"
  end

  # Have a method call the overloaded functions
  def print(key)
    handle_print REGISTRY[key]
  end

  # Actually make the overloaded methods
  make_print "a", "hello"
  make_print "b", "bye"
end

b = B.new
b.print "a" # "hello"
b.print "b" # "bye"

More Repositories

1

elm-ui

UI library for making web applications with Elm
Elm
921
star
2

elm-github-install

An alternative decentralized package manager for Elm
Ruby
205
star
3

cr-dotenv

Loads ".env" files
Crystal
94
star
4

elm-spec

End-to-end integration testing for Elm apps and components
JavaScript
55
star
5

elm-ui-examples

Examples applications for Elm-UI
Elm
32
star
6

elm-directory

Find and read documentation of Elm packages.
Ruby
17
star
7

elm-storage

Unified interface for accessing and modifying LocalStorage, SessionStorage and Cookies
Elm
13
star
8

elm-dev-env

Opinionated development environment for building Elm apps
JavaScript
12
star
9

elm-dom

Alternative Elm package for DOM manipulation.
Elm
11
star
10

elm-html-styles

Add CSS styles to your HTML!
Elm
8
star
11

quality-control

A Gem for running CI tasks like a boss.
Ruby
8
star
12

moosql

MooTools class wrapper for HTML 5 Sql storage
CoffeeScript
7
star
13

Rubik-s-Cube

Rubik's Cube Game in Three.js
CoffeeScript
5
star
14

cr-corm

SQL Statement Builder
Crystal
5
star
15

elm-ui-website

Website for Elm-UI
Elm
2
star
16

blender-gle

Blender Layout Engine for G.UI
CoffeeScript
2
star
17

coffer

Integrated Components
CoffeeScript
2
star
18

advent-of-code-2022

Advent of Code 2022 in Mint.
Mint
2
star
19

mui

MUI - Modern User Interfaces
CoffeeScript
2
star
20

diamond

Chrome / Node-Webkit App development environment
JavaScript
2
star
21

crystal

JavaScript library
JavaScript
2
star
22

fron-acr

Ruby
1
star
23

global-actions-example

Elm
1
star
24

shards

Repository for client side JavaScript libraries.
Ruby
1
star
25

elm-html

Elm
1
star
26

worm

virtual Object-relational mapping
CoffeeScript
1
star
27

nodesass

Sass compiler for node.js
CoffeeScript
1
star
28

Lattice

A JavaScript User Interface Library with the power of MooTools.
CoffeeScript
1
star
29

stash

Object based initialization
JavaScript
1
star
30

moxy-demos

JavaScript
1
star
31

cr-dkim

Crystal
1
star
32

cr-quickjs

Crystal
1
star
33

Coffee-Store

Simple Key - Value storage interface for local storage implementations.
JavaScript
1
star
34

rennee-extras

Some extra methods and stuff for Renee.
1
star
35

Moosic

A simple HTML5 audio player with the power of MooTools.
JavaScript
1
star