Skip to content

k8s-platform-hub/hello-ruby-sinatra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hello-sinatra

This quickstart consists of a basic hasura project using Sinatra ( http://www.sinatrarb.com/ ). Once this project is deployed on to a hasura cluster, you will have the Sinatra app running at https://app.cluster-name.hasura-app.io

This is the right place to start if you are planning to build or want to learn to build a Sinatra app with hasura.

Sections

Introduction

This quickstart project comes with the following by default:

  1. A basic hasura project
  2. Two tables article and author with some dummy data
  3. A basic Sinatra app

Quickstart

Follow this section to get this project working. Before you begin, ensure you have the latest version of hasura cli tool installed.

Step 1: Getting the project

$ hasura quickstart tiru/hello-sinatra
$ cd hello-sinatra

The above command does the following:

  1. Creates a new folder in the current working directory called hello-sinatra
  2. Creates a new trial hasura cluster for you and sets that cluster as the default cluster for this project
  3. Initializes hello-sinatra as a git repository and adds the necessary git remotes.

Step 2: Getting cluster information

Every hasura project is run on a Hasura cluster. To get details about the cluster this project is running on:

$ hasura cluster status

This will give you your cluster status like so

INFO Status:                                      
Cluster Name:       h34-excise98-stg
Cluster Alias:      hasura
Kube Context:       h34-excise98-stg
Platform Version:   v0.15.3
Cluster State:      Synced

Keep a note of your cluster name. Alternatively, you can also go to your hasura dashboard and see the clusters you have.

Step 3: Deploying on a hasura cluster

$ git add .
$ git commit -m "Initial Commit"
$ git push hasura master

Once the above commands are executed successfully, head over to https://app.cluster-name.hasura-app.io (in this case https://app.h34-excise98-stg.hasura-app.io) to view your app.

Api console

Every hasura cluster comes with an api console that gives your a GUI to test out the baas features of hasura. To open the api console

$ hasura api-console

Data APIs

Hasura provides ready to use data apis to make powerful data queries on your tables. This means that you have ready-to-use JSON apis on any tables created. The url to be used to make these queries is always of the type: https://data.cluster-name.hasura-app.io/v1/query (in this case https://data.h34-excise98-stg.hasura-app.io)

As mentioned earlier, this quickstart app comes with two pre-created tables author and article.

author

column type
id integer NOT NULL primary key
name text NOT NULL

article

column type
id serial NOT NULL primary key
title text NOT NULL
content text NOT NULL
rating numeric NOT NULL
author_id integer NOT NULL

Alternatively, you can also view the schema for these tables on the api console by heading over to the tab named data as shown in the screenshots below.

alt text alt text

This means that you can now leverage the hasura data queries to perform CRUD operations on these tables.

The nodejs app uses these data apis to show the respective data, to see it in action check out https://app.cluster-name.hasura-app.io/get_articles (replace cluster-name with your cluster name) and check out microservices/api/src/hasuraExamples.js to see how the calls are being made. You can also check out all the apis provided by Hasura from the api console by heading over to the API EXPLORER tab.

For eg, to fetch a list of all articles from the article table, you have to send the following JSON request to the data api endpoint -> https://data.cluster-name.hasura-app.io/v1/query (replace cluster-name with your cluster name)

{
    "type": "select",
    "args": {
        "table": "article",
        "columns": [
            "id",
            "title",
            "content",
            "rating",
            "author_id"
        ]
    }
}

To learn more about the data apis, head over to our docs

Auth APIs

Every app almost always requires some form of authentication. This is useful to identify a user and provide some sort of personalized experience to the user. Hasura provides various types of authentication (username/password, mobile/otp, email/password, Google, Facebook etc).

You can try out these in the API EXPLORER tab of the api console. To learn more, check out our docs

Filestore APIs

Sometimes, you would want to upload some files to the cloud. This can range from a profile pic for your user or images for things listed on your app. You can securely add, remove, manage, update files such as pictures, videos, documents using Hasura filestore.

You can try out these in the API EXPLORER tab of the api console. To learn more, check out our docs

Custom Service

There might be cases where you might want to perform some custom business logic on your apis. For example, sending an email/sms to a user on sign up or sending a push notification to the mobile device when some event happens. For this, you would want to create your own custom service which does these for you on the endpoints that you define.

This quickstart comes with one such custom service written in ruby using the Sinatra framework. Check it out in action at https://app.cluster-name.hasura-app.io . Currently, it just returns a "Hello Sinatra" at that endpoint.

In case you want to use another language/framework for your custom service. Take a look at our docs to see how you can add a new custom service.

Migrate from an existing Sinatra app

If you have an existing Sinatra app and would like to migrate it to Hasura:

  • Replace the microservices/app/ directory with your app directory.
  • Ensure that the structure of the app directory is
app
└─── src
|    Dockerfile
|    k8s.yaml
  • cd path-to-hello-sinatra/hello-sinatra
  • git add . && git commit -m "Migration Commit"
  • git push hasura master

Now your existing app should be running on https://app.cluster-name.hasura-app.io

Local development

Everytime you push, your code will get deployed on a public URL. However, for faster iteration you should locally test your changes.Since we are directly accessing the internal data endpoint (Read more about internal and external endpoints here) in the Sinatra app. We need to forward our requests to the port at which the data service is running.

$ hasura microservice forward -s data -n hasura --local-port 6432 --remote-port 8080

FAQ

  • How do I check the logs for my custom service ?
  • I want to change the subdomain from ui to something else. How do I do that ?
  • Permissions on filestore
  • How do I add a new custom service ?
  • I am stuck, what do I do now ?

Project structure

Files and Directories

The project (a.k.a. project directory) has a particular directory structure and it has to be maintained strictly, else hasura cli would not work as expected. A representative project is shown below:

.
├── hasura.yaml
├── clusters.yaml
├── conf
│   ├── authorized-keys.yaml
│   ├── auth.yaml
│   ├── ci.yaml
│   ├── domains.yaml
│   ├── filestore.yaml
│   ├── gateway.yaml
│   ├── http-directives.conf
│   ├── notify.yaml
│   ├── postgres.yaml
│   ├── routes.yaml
│   └── session-store.yaml
├── migrations/
└── microservices
    └── app/

hasura.yaml

This file contains some metadata about the project, namely a name, description and some keywords. Also contains platformVersion which says which Hasura platform version is compatible with this project.

clusters.yaml

Info about the clusters added to this project can be found in this file. Each cluster is defined by it's name allotted by Hasura. While adding the cluster to the project you are prompted to give an alias, which is just hasura by default. The kubeContext mentions the name of kubernetes context used to access the cluster, which is also managed by hasura. The config key denotes the location of cluster's metadata on the cluster itself. This information is parsed and cluster's metadata is appended while conf is rendered. data key is for holding custom variables that you can define. A sample clusters.yaml file would look like so:

- name: h34-ambitious93-stg
  alias: hasura
  kubeContext: h34-ambitious93-stg
  config:
    configmap: controller-conf
    namespace: hasura
  data: null  

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages