-
Notifications
You must be signed in to change notification settings - Fork 8
Develop new module
You can easily add new modules. The guide below is how to create the skeleton.
We are going to create a module named smile
.
If you need a database, create a file in nimhapkg/resources/database/modules/smile_database.nim
.
We are going to create 2 tables: a normal table and a template table.
Insert your table requirements, e.g.:
import db_sqlite
proc smileDatabase*(db: DbConn) =
if not tryExec(db, sql"""
CREATE TABLE IF NOT EXISTS smile (
id INTEGER PRIMARY KEY,
quote TEXT,
creation timestamp NOT NULL default (STRFTIME('%s', 'now'))
);"""):
echo "ERROR: Smile templates table could not be created"
if not tryExec(db, sql"""
CREATE TABLE IF NOT EXISTS smile_templates (
id INTEGER PRIMARY KEY,
name TEXT,
mood TEXT,
creation timestamp NOT NULL default (STRFTIME('%s', 'now'))
);"""):
echo "ERROR: Smile templates table could not be created"
To create the database navigate to nimha.nim
and find the proc proc createDbTables() =
.
Insert a new db variable and the proc:
var dbSmile = conn("dbSmile.db")
smileDatabase(dbSmile)
If your module uses proc's, create a file: nimhapkg/modules/smile/smile.nim
and insert your proc's, e.g.:
proc smileNow*(): string =
return "Smile now!"
We are going to add a small settings page. It is in this page, you should allow the user add and delete templates. See the other modules for inspiration.
Create the file nimhapkg/tmpl/smile.tmpl
and insert:
#? stdtmpl | standard
#
#proc genSmile(c: var TData): string =
# result = ""
<head>
${genMainHead(c)}
</head>
<body>
<header>
${genMainHeader()}
</header>
<main>
<div id="pageType" data-userid="${c.userid}" data-type="os" style="display: none;"></div>
<div class="wrapper">
${genMainSidebar()}
<div id="pagewrapper">
<div id="smile">
<h1>SMILE</h1>
<hr>
<div class="smileTemplates">
# let smileTemplates = getAllRows(dbSmile, sql"SELECT id, quote FROM smile")
# for smile in smileTemplates:
<div>${smile[2]]</div>
# end for
</div>
</div>
</div>
</div>
</main>
<footer>
${genMainFooter()}
</footer>
${genMainNotify()}
</body>
#end proc
Open nimhapkg/mainmodules/nimha_webinterface.nim
:
- Import smile proc: import ../modules/smile/smile
- Add the database:
var dbSmile = conn("dbSmile.db")
- Include the HTML:
include "../tmpl/smile.tmpl"
- Insert the route:
get "/smil":
createTFD()
if not c.loggedIn:
redirect("/login")
resp genSmile(c)
get "/smile/proc":
createTFD()
if not c.loggedIn:
redirect("/login")
return smileNow()
get "/smile/template":
createTFD()
if not c.loggedIn:
redirect("/login")
resp(getValue(dbSmile, sql("SELECT mood FROM smile_templates WHERE id = ?"), @"smileid")
If you modules support templates, then they should be added to:
nimhapkg/tmpl/cron.tmpl
nimhapkg/tmpl/alarm.tmpl
This will allow the cron and the alarm to use your smile_templates table.
- Home
- Requirements
- Install NimHA
- Optional
- Modules
- Tutorials (helpers, etc.)
- Development