Go to file
Rick Carlino 5c127e2b83 Update gitignore II 2017-06-29 16:52:56 -05:00
app Change API point radius default to 25, not 50 2017-06-29 11:06:53 -05:00
bin Add more fields, validations to image and start writing model tests. 2017-01-11 09:27:06 -06:00
config Disable ActionController::Base.expire_page 2017-06-21 18:10:59 -05:00
db API docs and default radius changes plus merging of feature_lock on FE 2017-06-29 13:45:36 -05:00
lib/tasks Move tasks folder back into lib 2016-11-30 09:22:19 -06:00
public Update gitignore II 2017-06-29 16:52:56 -05:00
spec API docs and default radius changes plus merging of feature_lock on FE 2017-06-29 13:45:36 -05:00
src Move stuff into root 2017-06-29 16:06:19 -05:00
tools Move stuff into root 2017-06-29 16:06:19 -05:00
typings Move stuff into root 2017-06-29 16:06:19 -05:00
.buildpacks use heroku-postbuild 2016-09-22 11:17:12 -05:00
.gitignore Update gitignore II 2017-06-29 16:52:56 -05:00
.ruby-gemset rename application from `dss` to `farmbot` 2015-03-09 05:31:05 -05:00
.ruby-version Add reproduction steps 2017-05-16 08:39:12 -05:00
.travis.yml Update version on travis.yml 2017-05-18 13:31:18 -05:00
CONTRIBUTING.md Move stuff into root 2017-06-29 16:06:19 -05:00
DEPLOYMENT.md Keep deployment docs up to date 2017-06-22 14:30:27 -05:00
Gemfile Added Procfile. Need to bring frontend/ dir up one level. 2017-06-29 16:01:27 -05:00
Gemfile.lock Added Procfile. Need to bring frontend/ dir up one level. 2017-06-29 16:01:27 -05:00
ISSUE_TEMPLATE Move stuff into root 2017-06-29 16:06:19 -05:00
LICENSE Move stuff into root 2017-06-29 16:06:19 -05:00
Procfile.dev More fixes for Procfile 2017-06-29 16:25:53 -05:00
README.md More rebuilding on staging 2017-06-29 16:34:38 -05:00
Rakefile Cruft removal, part I 2015-10-20 13:03:42 -05:00
api_docs.md.erb API docs and default radius changes plus merging of feature_lock on FE 2017-06-29 13:45:36 -05:00
config.ru Added foundation and high voltage. Readme updates, too. 2014-03-31 06:31:38 -07:00
favicon.ico Move stuff into root 2017-06-29 16:06:19 -05:00
index.js Add `rake frontend:install`. 2016-09-22 09:15:57 -05:00
install_frontend.sh install_frontend.sh changes 2017-06-29 16:45:04 -05:00
latest_corpus.rb Fix referential integrity checks on polymorphic points associations 2017-05-22 07:53:31 -05:00
notes.md Move stuff into root 2017-06-29 16:06:19 -05:00
notes.tsx Move stuff into root 2017-06-29 16:06:19 -05:00
package.json More fixes for Procfile 2017-06-29 16:25:53 -05:00
tsconfig.json Move stuff into root 2017-06-29 16:06:19 -05:00
tslint.json Move stuff into root 2017-06-29 16:06:19 -05:00
yarn.lock Move stuff into root 2017-06-29 16:06:19 -05:00

README.md

Code Climate Test Coverage

Q: Do I need this?

This repository is intended for software developers who wish to modify the Farmbot Web App. If you are not a developer, you are highly encouraged to use the publicly available web app. Running a server is a non-trivial task which will require an intermediate background in Ruby, SQL and Linux system administration.

If you are a developer interested in contributing or would like to provision your own server, you are in the right place.

Q: What is the Farmbot Web API?

This Repo is the RESTful JSON API for Farmbot. This includes things like storage of user data, plant data, authorization tokens and a variety of other resources.

The key responsibility of the API is information and permissions management. This should not be confused with device control, which is done via MQTT.

Q: Can I see some example API requests?

For a list of example API requests and responses, see our reference documentation. If you wish to write an add-on application that uses the FarmBot API, please let us know in an issue. We are happy to answer any specific questions you may have.

Q: How do I Setup an API locally?

Prerequisites

Your machine will need the following:

  1. A Linux or Mac based machine. We do not support windows at this time.
  2. Ruby 2.3.3
  3. ImageMagick (brew install imagemagick (Mac) or sudo apt-get install imagemagick (Ubuntu))
  4. Node JS > v6
  5. libpq-dev and postgresql

Setup

  1. git clone https://github.com/FarmBot/Farmbot-Web-API
  2. cd Farmbot-Web-API
  3. Install libpq-dev and postgresql
  4. bundle install
  5. MOST IMPORTANT STEP. Copy config/database.example.yml to config/database.yml. In GNU/Linux or Mac: mv config/database.example.yml config/database.yml. Please read the instructions inside the file. Replace the example values provided with real world values.
  6. Give permission to create a database*
  7. rake db:create:all db:migrate db:seed
  8. (optional) Verify installation with RAILS_ENV=test rake db:create db:migrate && rspec spec.
  9. MQTT_HOST=your_mqtt_server_domain rails s -b 0.0.0.0 where your_mqtt_server_domain is the IP or domain name of the MQTT server (see next step). The MQTT server does not need to be running yet.
  10. Now that the API server is running, provision an MQTT server.
  11. Initialize background workers in a seperate process: rails jobs:work. For production setups, this is a required step.
  12. (REQUIRES NODE JS > v6) Run ./install_frontend.sh to build the latest frontend app. You may also run the frontend on a seperate server. The frontend repository has more info on this if you wish to do so. For most users, ./install_frontend.sh should be sufficient.
  13. Open localhost:3000. The application is now ready for use.
  14. Raise an issue if you hit problems with any of these steps.

*Give permission to user to create database:

sudo -u postgres createuser user
sudo -u postgres psql
ALTER USER "user" WITH SUPERUSER;

Q: Is Dokku / Docker supported?

Doku (a Docker management system) is partially supported. Pull requests welcome. Please see deployment.md for more information.

Config Settings (important)

We try our best to follow the 12 Factor Methodology. Part of that means using ENV variables as a means of storing configuration. Your server won't run without setting ENV variables first.

You can accomplish this by setting the ENV variables directly from your shell / server management tool or by writing an application.yml file.

See config/application.example.yml for a list of all the variables that must be set.

Encryption keys: Encryption keys will be autogenerated if not present. They can be reset using rake keys:generate. If ENV['RSA_KEY'] is set, it will be used in place of the *.pem files. Useful for environments like Heroku, where file system access is not allowed.

We can't fix issues we don't know about. Please submit an issue if you are having trouble installing on your local machine.

Q: How can I Generate an API token?

You must pass a token string into most HTTP requests under the Authorization: request header.

Here's what a response looks like when you request a token:

{
    "token": {
        "unencoded": {
            "sub": "test123@test.com",
            "iat": 1459109728,
            "jti": "922a5a0d-0b3a-4767-9318-1e41ae600352",
            "iss": "http://localhost:3000/",
            "exp": 1459455328,
            "mqtt": "localhost",
            "bot": "aa7bb37f-5ba3-4654-b2e4-58ed5746508c"
        },
        "encoded":
        // THE IMPORTANT PART IS HERE!!:
         "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ0ZXN0MTIzQHRlc3QuY29tIiwiaWF0IjoxNDU5MTA5NzI4LCJqdGkiOiI5MjJhNWEwZC0wYjNhLTQ3NjctOTMxOC0xZTQxYWU2MDAzNTIiLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjMwMDAvIiwiZXhwIjoxNDU5NDU1MzI4LCJtcXR0IjoibG9jYWxob3N0IiwiYm90IjoiYWE3YmIzN2YtNWJhMy00NjU0LWIyZTQtNThlZDU3NDY1MDhjIn0.KpkNGR9YH68AF3iHP48GormqXzspBJrDGm23aMFGyL_eRIN8iKzy4gw733SaJgFjmebJOqZkz3cly9P5ZpCKwlaxAyn9RvfjQgFcUK0mywWAAvKp5lHfOFLhBBGICTW1r4HcZBgY1zTzVBw4BqS4zM7Y0BAAsflYRdl4dDRG_236p9ETCj0MSYxFagfLLLq0W63943jSJtNwv_nzfqi3TTi0xASB14k5vYMzUDXrC-Z2iBdgmwAYUZUVTi2HsfzkIkRcTZGE7l-rF6lvYKIiKpYx23x_d7xGjnQb8hqbDmLDRXZJnSBY3zGY7oEURxncGBMUp4F_Yaf3ftg4Ry7CiA"
    }
}

Important: The response is provided as JSON for human readability. For your Authorization header, you will only be using data.token.encoded. In this example, it's the string starting with eyJ0eXAiOiJ...

Via CURL

curl -H "Content-Type: application/json" \
     -X POST \
     -d '{"user":{"email":"test123@test.com","password":"password123"}}' \
     https://my.farmbot.io/api/tokens

Via JQuery

Since the API supports CORS, you can generate your token right in the browser.

Here's an example:

$.ajax({
    url: "https://my.farmbot.io/api/tokens",
    type: "POST",
    data: JSON.stringify({user: {email: 'admin@admin.com', password: 'password123'}}),
    contentType: "application/json",
    success: function (data) {
                 // You can now use your token:
                 var MY_SHINY_TOKEN = data.token.encoded;
             }
});

Want to Help?

Low Hanging Fruit. Raise an issue if you have any questions.