Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSoc 2016: Admin console and Elasticsearch Integration (WIP) #125

Closed
wants to merge 90 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
eceed3a
feat(admin): Basic code for admin module
May 27, 2016
715e29e
Just checks for password for now
May 27, 2016
ce0f765
Merge pull request #1 from sailorproject/master
r-nikhil May 29, 2016
ee01d57
Merge pull request #2 from rnikhil275/master
r-nikhil May 29, 2016
3b6852c
(not working) add autogen and conf
May 29, 2016
08ed53e
(admin centre) Use access module
May 30, 2016
17b9324
use access module
May 30, 2016
ba96069
(not working) use session open function directly
May 31, 2016
39e2512
TEST
Jun 2, 2016
a43716b
(working with apache) autogen and hide the text box
Jun 2, 2016
1435aaa
logout button
Jun 2, 2016
e09db73
logout button working
Jun 3, 2016
47498b1
remove random text
Jun 3, 2016
b5ad116
logout link working.
Jun 3, 2016
bdb1d7b
add link to go back to admin centre from autogen
Jun 3, 2016
b28a562
add else statement
Jun 5, 2016
dbd2173
add conf.lua
Jun 7, 2016
eacf6ae
edit rockspec
Jun 7, 2016
b1d1a73
add conf function to admin module
Jun 7, 2016
4054602
parameters print to console
Jun 7, 2016
d4aa925
(working) change password through the conf editor
Jun 10, 2016
45a76ec
(working) file write working.
Jun 10, 2016
b8a0be6
(working) change admin password with quotes
Jun 12, 2016
fd3c0bf
(working) make fields with default values for confetti
Jun 12, 2016
912cbc4
views for the config editor
Jun 12, 2016
030d976
Merge pull request #3 from rnikhil275/devel
r-nikhil Jun 13, 2016
3e4b63d
use tables instead of multiple file:writes
Jun 13, 2016
16319ff
(working) config editor
Jun 13, 2016
9cbb468
Merge pull request #4 from rnikhil275/devel
r-nikhil Jun 13, 2016
8bc5781
feat(admin): Add mail/db settings to the config editor
Jun 14, 2016
e9aa764
feat(admin) : add other settings to configedit
Jun 14, 2016
36ac439
Fix minor error
Jun 14, 2016
9181986
edit(admin): Add a success message when conf is changed
Jun 18, 2016
3e86023
edit(conf): Make admin centre and autogen default to false
Jun 18, 2016
ae08e32
test(admin): Sample test for admin console.
Jun 19, 2016
9bdbd2f
refactor(admin): Re-organize admin module
Jun 26, 2016
d457589
feat(admin): Views for config editor
Jun 27, 2016
b7927d4
feat(admin): Controller for config editor
Jun 27, 2016
f7d20be
fix(admin): Change mailer settings instead of db
Jun 28, 2016
38646fd
refactor(admin): Check for enable_admin and remove functions from sai…
Jun 28, 2016
d4a4909
edit(admin): Add checks for empty password
Jun 28, 2016
48f5e38
feat(admin): Add logout button
Jun 28, 2016
6d9206b
feat(admin): Add autogen functions to dashboard
Jun 28, 2016
873fb9e
Remove unnecessary files
Jun 28, 2016
5005b4c
tests(admin): Tests for autogen
Jun 28, 2016
c911e0f
add autogen back in the rock spec
Jun 28, 2016
74b8c18
Remove autogen.lua test file.
Jun 28, 2016
25fce2d
BAD COMMIT: Deliberately change admin.lua to make tests pass.
Jun 28, 2016
0f8f830
add require statement to make travis pass
Jun 28, 2016
6ae526a
bugfix(admin): remove autogen related post variables
Jun 29, 2016
8b4f9fd
bugfix(admin): Add missing fields in conf file and fix typo in admin …
Jun 29, 2016
ac476ca
feat(admin): Highlight messages
Jun 29, 2016
b6466fb
Merge pull request #5 from sailorproject/master
r-nikhil Jul 7, 2016
ae555a2
fix(admin): Add an extra function to access module to check username
Jul 7, 2016
b0d3a13
fix(admin): Add support for multiple db environments
Jul 7, 2016
e02a32a
test(admin): Help wanted, cannot figure out why admin test fails
Jul 7, 2016
4b17e90
Merge pull request #6 from sailorproject/master
r-nikhil Jul 23, 2016
dda91d5
fix(admin test), test(access), docs(testing)
Jul 23, 2016
37fbde5
feat(elasticsearch): Add search module to search through elasticsearch
Jul 28, 2016
77e78b7
feat(elasticsearch): Add scaffolding functions
Jul 29, 2016
d8c22c1
feat(elasticsearch): Take host parameters from the conf file
Aug 2, 2016
2eedd9a
feat(elastic_model): Add a layer of abstraction for elasticsearch
Aug 2, 2016
cf9d344
feat(Emodel): You can add parameters directly now
Aug 3, 2016
be9ae1a
fest(ESmodel): search method
Aug 3, 2016
a77172f
feat(Emodel): Add support for multiple hosts
Aug 4, 2016
67e54ef
feat(conf): Add ES parameters also
Aug 4, 2016
889f59b
style(elasaticsearch): Make changes to naming of certain functions an…
Aug 8, 2016
7b43fd0
feat(elasticsearch): Return status code also
Aug 8, 2016
3a32199
feat(elasticsearch): Support for parameters to be passed
Aug 9, 2016
f92d2e5
feat(elasticsearch): Improve functions
Aug 9, 2016
57f90b6
feat(configEditor): Add support for editing search server settings
Aug 9, 2016
39da2bc
doc(Elasticsearch): Add some basic comments
Aug 9, 2016
66dc1f8
remove typo
Aug 9, 2016
8ae1cd1
Elasticsearch: Add example and fix table names
Aug 9, 2016
fd2b207
bugfix(elasticsearch): Add support for elasticsearch in config editor
Aug 9, 2016
f5fc9b5
refactor(elasticsearch)
Aug 10, 2016
2ce5404
bugfix(elasticsearch): remove one hardcoded parameter
Aug 10, 2016
24f42c6
test(elasticsearch): Add tests for elasticsearch integration
Aug 13, 2016
c53353a
edit travis
Aug 13, 2016
904634e
travis edit
Aug 13, 2016
300721f
fix travis
Aug 13, 2016
7c2949a
docs(elasticsearch): Add some basic docs for model functions
Aug 13, 2016
2c8be47
docs(ES integration), refactor(ES views).
Aug 15, 2016
9ab556f
docs, test, feat in ES integration for bulkIndex
Aug 15, 2016
35a3c6f
(tests), (docs) for ES integration: Add client:mget() function
Aug 15, 2016
2779b0b
tests(ES integration): Add more functional tests
Aug 15, 2016
386ee24
feat(elasticsearch integration): Add support for using namespaces fro…
Aug 15, 2016
63dbc44
docs(admin centre): Add docs, edit functions
Aug 16, 2016
e454897
refactor(ES): Move logic to controller
Aug 25, 2016
c96ee68
Merge pull request #8 from sailorproject/master
r-nikhil Oct 22, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -11,6 +11,7 @@ addons:
services:
- mysql
- postgresql
- elasticsearch

env:
global:
Expand Down Expand Up @@ -42,6 +43,7 @@ install:
- luarocks install luacov
- luarocks install luacov-coveralls
- luarocks make rockspecs/sailor-current-1.rockspec
- luarocks install --server=http://luarocks.org/dev elasticsearch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can now use:

luarocks install elasticsearch


before_script:
- source .travis/setdatabases.sh
Expand Down
36 changes: 36 additions & 0 deletions docs/admin_center.md
@@ -0,0 +1,36 @@
##Admin Center

The controller and views for admin center are pre loaded with the blank-app. So, when you create a new app using `sailor create "app_name"` these files are within the new folders.

###Admin center structure

There is one controller named `admin.lua` and a views folder named "admin" inside which are three files. One is `dashboard.lp` which is the views file for the autogen and config editor functions. `index.lp` is for logging into the admin center. Third file is `error.lp`.

###Getting started
First open `conf/conf.lua`. Change these two parameters:

-- the default value for this is "false"
enable_admin = "true"

-- the defaullt password is empty
admin_password = "someDifficultPassword"

After running the `start-server.lua`, visit <a href="http://localhost:8080/?r=admin">here</a>. Enter the password set it in the config file in the above setup. You would be
re-directed to `localhost:8000/?r=admin/dashboard`.
The autogen functions are located on the left and the fields from the config file are on the right. You can edit any of the config editor values and then press `submit` button
at the bottom of the screen.

###Autogen Functions

The first autogen function is to Generate a model based on a table existing in the database. This is a very handy function to automatically generate the model file.
Enter the table name and press enter.

There would be a success message ` Model generated with success.`.
To create the methods for CRUD operations in the controller, use the next autogen function.
To Generate CRUD, enter the name of the `model` created in the previous step. This would be same as the name of the table.

###Config editor

Config editor is basically a long form which when saved, writes data into the config file. The existing values can be edited and then saved at the end. The main file edited here is `conf/conf.lua`.

After making necessary changes, don't forget to logout and also disable admin in production servers.
147 changes: 147 additions & 0 deletions docs/manual_ES_model.md
@@ -0,0 +1,147 @@
##Reference Manual
### Elastic Search Model

This module makes it possible to store, retrieve and search data from <a href = "https://www.elastic.co/downloads/elasticsearch">Elasticsearch </a>. It uses the <a href = "github.com/dhavalkapil/elasticsearch-lua">elasticsearch-lua</a> client for Lua. Refer the client <a href="http://elasticsearch-lua.readthedocs.io/en/latest/">here</a> to know the parameters which can be sent through the model functions if any. Return responses are also same as the one offered by this client.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include in the docs how to install the necessary files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some instructions. Please check 2c8be47


###Installation and getting started.

To get started, install the elasticsearch-lua rock using

luarocks install --server=http://luarocks.org/dev elasticsearch

Make sure you have elasticsearch installed and running. Refer to the instructions given on the official <a href = "https://www.elastic.co/downloads/elasticsearch">website</a>.

The `es_model.lua` file is the main module. After importing "es_model" we proceed to create a new instance of the model. There should be a file inside models/ which contains the keys and types defined for the particular model.

local es_model = require "sailor.es_model"
-- test.lua should be inside models/ folder.
local contact = es.model.new("test")

Now, you can assign attributes to `contact` like

contact.name = "test name"
contact.email = "test@test.com"

And save it using,

msg , code = contact.save{id = 1}

The `arg` represented here is named arguments which have to be passed into the function. For list of args that can be passed check the elasticsearch-lua reference.

The return value for these functions is same as elasticsearch-lua.
Example:

local msg , err = contact.save{id = 2, routing = "test.com", timout = "2m"}

If the call succeeds then `msg` contains the result table and `err` contains the status code.
If the call fails then `msg` contains `nil` and `err` contains the error message.

###Elasticsearch administration

To use administrative functionalities,

local es_model = require("sailor.db.es_model")
-- To use the namespaces
local res, err = es_model.client.cluster:stats()

Check this <a href="http://elasticsearch-lua.readthedocs.io/en/latest/namespaces/">page</a> for more functions.

###model.save(arg)

This function takes a list of paramters which would be same as for the elasticsearch-lua client. It saves the attributes assigned to the model if the `body` parameter is not present. Else it just saves the array passsed in the body parameter. The below example saves the `name` attribute into the ES

contact.name = "test"
contact.save = {id = 1}

The index can be preset in the config file which can also be over written when passing the parameters.

###model.getCount()

This functions returns the number of documents present in the given index and type. You don't need to pass any parameter.
Example:

local count = contact.getCount()

###model.getAll()

This function returns a table of all the documents present in the given index and type. No parameter is required. It uses the `client:search()` function from elasticsearch-lua on the inside.

local data, err = contact.getAll()

###model.update(arg)

This is used for updating documents already in the index. The id has to be specified.

contact.update{id = 1, body = {doc = { name = "new name" }}}

###model.delete(arg)

Deletes a particular document with the given id.

contact.delete{id = 1}

###model.search(arg)

Search the given index and type. The arg list supports a lot of parameters. Read more about it <a href="https://dhavalkapil.com/elasticsearch-lua/docs/classes/Client.html#Client:search">here.</a>

contact.search{q = "queryString", size = 15}

###model.get(arg)

Get's documents from the database based on the parameters provided. Full list of args can be found <a href="https://dhavalkapil.com/elasticsearch-lua/docs/classes/Client.html#Client:get">here.</a>

contact.get{id = 1, fields = {name, email}}

###model.bulkIndex(arg)

For indexing multiple documents at once. Uses `client:bulk()`. The index and type have to set in the parameters. For a list of parameters check <a href="https://dhavalkapil.com/elasticsearch-lua/docs/classes/Client.html#Client:bulk">here.</a>

contact.bulkIndex{
body = {
-- First action
{
index = {
["_index"] = "my_index1",
["_type"] = "my_type1"
}
},
-- First body
{
my_key1 = "my_value1",
},
-- Second action
{
index = {
["_index"] = "my_index2",
["_type"] = "my_type2"
}
},
-- Second body
{
my_key2 = "my_value2",
}
}
}

###model.mget(arg)

You can get multiple documents in the form of nested table using the `client:mget()`. The allowed parameters are <a href="https://dhavalkapil.com/elasticsearch-lua/docs/classes/Client.html#Client:mget">here.</a>

contact.mget{
body = {
docs = {
-- First document
{
["_index"] = "my_index1",
["_type"] = "my_type1",
["_id"] = "my_id1"
},
-- Second document
{
["_index"] = "my_index2",
["_type"] = "my_type2",
["_id"] = "my_id2"
}
}
}
}
2 changes: 1 addition & 1 deletion docs/tutorial_testing.md
Expand Up @@ -51,7 +51,7 @@ Let's suppose we have a User model and we want to unit test some of the User met

The fixture is a regular Lua script that will be required. This for multiple possibilities, such as making loops for inserting many entries, as long as in the end you return a table containing samples of your model.

After creating your fixture, you must add this on the bottom of your bootstrap file so they are loaded before running your tests.
After creating your fixture, you must add this on the bottom of your bootstrap file so they are loaded before running your tests. Also don't forget to set the package.path to the sailor directory you want to test.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


--/tests/bootstrap.lua
...
Expand Down
3 changes: 2 additions & 1 deletion rockspecs/sailor-current-1.rockspec
Expand Up @@ -33,9 +33,10 @@ build = {
modules = {
sailor = "src/sailor.lua",
['sailor.access'] = "src/sailor/access.lua",
['sailor.autogen'] = "src/sailor/autogen.lua",
['sailor.cookie'] = "src/sailor/cookie.lua",
['sailor.db'] = "src/sailor/db.lua",
['sailor.autogen'] = "src/sailor/autogen.lua",
['sailor.db.es_model'] = "src/sailor/db/es_model.lua",
['sailor.db.resty_mysql'] = "src/sailor/db/resty_mysql.lua",
['sailor.db.luasql_common'] = "src/sailor/db/luasql_common.lua",
['sailor.blank-app.conf.conf'] = "src/sailor/blank-app/conf/conf.lua",
Expand Down
19 changes: 0 additions & 19 deletions src/sailor.lua
Expand Up @@ -103,17 +103,6 @@ function sailor.init(r)
return p
end


-- Auxiliary function to open the autogen page for models and CRUDs
-- page: our page object
local function autogen(page)
local autogen = require "sailor.autogen"

local src = autogen.gen()
src = lp.translate(src)
page:render('sailor/autogen',{page=page},src)
end

-- Gets parameter from url query and made by mod rewrite and reassembles into page.GET
-- TODO - improve
local function apache_friendly_url(page)
Expand Down Expand Up @@ -174,14 +163,6 @@ function sailor.route(page)
controller, action = match(route_name, "([^/]+)/?([^/]*)")
end

if controller == "autogen" then
if conf.sailor.enable_autogen then
local _,res = xpcall(function () autogen(page) end, error_handler)
return res or httpd.OK or page.r.status or 200
end
return error_404()
end

local ctr
local _, res = xpcall(function() ctr = require("controllers."..controller) end, error_handler)
if ctr then
Expand Down
4 changes: 4 additions & 0 deletions src/sailor/access.lua
Expand Up @@ -104,4 +104,8 @@ function access.logout()
return session.destroy(sailor.r)
end

function access.is_loggedin(login)
if (session.data.login == login) then return true else return false end
end

return access
44 changes: 0 additions & 44 deletions src/sailor/autogen.lua
Expand Up @@ -9,50 +9,6 @@ local lfs = require "lfs"

local M = {}

--The code for the autogen page
function M.gen()
local code = [[
<?lua
local model = require "sailor.model"
local mogelgen = false
local crudgen = false
if next(page.POST) then

if page.POST.table_name then
modelgen = model.generate_model(page.POST.table_name)
end

if page.POST.model_name then
crudgen = model.generate_crud(page.POST.model_name)
end

end
?>

<h2>Generate model</h2>

<form method="post">
<input type=text placeholder="Table Name..." name="table_name"/>
<input type="submit" />
</form>
<?lua if modelgen then ?>
Model generated with success!
<?lua end ?>
<br/><br/>

<h2>Generate CRUD</h2>

<form method="post">
<input type=text placeholder="Model Name..." name="model_name"/>
<input type="submit" />
</form>
<?lua if crudgen then ?>
CRUD generated with success!
<?lua end ?>
]]
return code
end

-- The code for the generated controller
-- model: the model from which the generated CRUD will refer to
function M.generate_controller(model)
Expand Down
11 changes: 6 additions & 5 deletions src/sailor/blank-app/conf/conf.lua
Expand Up @@ -9,11 +9,12 @@ local conf = {
layout = 'main',
route_parameter = 'r',
default_error404 = 'error/404',
enable_autogen = false, -- default is false, should be true only in development environment
friendly_urls = false,
max_upload = 1024 * 1024,
max_upload = 1024 * 1024,
environment = "development", -- this will use db configuration named development
hide_stack_trace = false -- false recommended for development, true recommended for production
hide_stack_trace = false, -- false recommended for development, true recommended for production
enable_admin=false, -- set true only in development. Set password below too.
admin_password=""
},

db = {
Expand All @@ -28,8 +29,8 @@ local conf = {

smtp = {
server = '',
user = '',
pass = '',
smtpuser = '',
smtppass = '',
from = ''
},

Expand Down