Skip to content
/ sipo Public

Ruby: Sinatra-MongoDB Skeleton Code with User Authentication. Nginx + Docker orchestration

Notifications You must be signed in to change notification settings

sabzo/sipo

Repository files navigation

Sipo - Sinatra MongoDB Starter Code

API

Sipo uses Sinatra framework, warden for Authentication Warden and MongoDB for storage.

This is simply minimalistic starter code to be used for a simple Ruby MongoDB web-app.

Usage (in irb shell for example)

 # BEFORE ANYTHING set up Environment Varaibles for MongoDB connection!! -- mongoconnect.rb
example connection to mongo cluser:
`export URI= 'mongodb+srv://<username>:<password>@<cluster-url>/<database>?retryWrites=true&w=majority'`
 # Can simply use a Rack Server to run or in irb for example you can do the following
 # include the API starting point
 require_relative 'app'

 # Creates a new user and encrypts the plaintext password
 u = User.new( {email: 'email@example.com', password: 'plainText'})

 # Create a new user and save (insert) into database
 u1 = User.new( {email: 'email@example.com', password: 'plainText'}).insert()

 # get Email or User of this object is simple as using the fields of the object
 u1.email # returns email@example.com

 u1.password # returns $2a$10$NyxVqdcX8gD4a1kuFhoRuO6ZiH6sklRjFYjAywWllbW7HZ910FgFm

 # Retrieve a User from the Database and return a user object
 u2 = User.new().find_one( {email: 'email4@example.com'} )

 # Remove a user (or any other object from a collection) ex:
 r = User.new({_id: '569809fda2dc7acb80000000'}).remove_one()

# Update a User by an email address
# If no key is passed in (1st param to `update()`) then Api will use internal @id
# When updating, an @id must already exist (since the doc was created & saved to DB)
u = User.new().update({email: 's@s'}, {email: 's@m'})

# Remove a user (or any other object from a collection) ex:
r = User.new({_id: '569809fda2dc7acb80000000'}).remove_one()

# Update a User by an email address
# If no key is passed in (1st param to `update()`) then Api will use internal @id
# When updating, an @id must already exist (since the doc was created & saved to DB)
u = User.new({email: 's@s'}).update({}, {email: 's@m'})

RESTful API

This code maybe run using a webserver such as Unicorn. An ideal setup may include using NGINX as a reverse proxy which will route requests to Unicorn.

Routes

User

  • http POST #0.0.0.0:8080/user < user.json

Open Source

License

Copyright [2019] [Sabelo Mhlambi]

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License

About

Ruby: Sinatra-MongoDB Skeleton Code with User Authentication. Nginx + Docker orchestration

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages