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

Add web plugins functionality. #245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

danhper
Copy link

@danhper danhper commented May 2, 2015

Hi,

This is a proposal to make sinopia web interface extensible.

Background

I am currently using sinopia in production, and I like it a lot.
There are some things that I would like to be able to do through the web interface, for example managing users, but it seems that there is no such functionality for the moment.
I believe this is not something that should belong to the core, so it would be great to be able to plugin some functionality in the web interface.

Implementation

  • Extract layout from index.hbs so it can be shared

  • Set all variables needed for the layout in a middleware

  • Load all plugins listed in config.web_plugins and use them as an express middleware. Plugins should export a function called with the following parameters

    • Plugin configuration
    • Extra params:
      • config: Sinopia configuration
      • logger: Sinopia logger
      • auth: The authentication module
      • storage: The storage module
      • Handlebars: The Handlebars object with the registered layout (it does not seem to be possible to reuse the layout when requiring Handlebars from another file)

    and return an object with the following keys:

    • title: The displayed title of the plugin
    • basePath: The base path for the plugin
    • middleware: The express middleware for the plugin

With the above implementation, a plugin can be created with a code like this:

var fs         = require('fs')
var _          = require('lodash')
var express    = require('express')

module.exports = function (config, params) {
  var router = express.Router()

  var template = params.Handlebars.compile(fs.readFileSync(require.resolve('../templates/index.hbs'), 'utf8'))

  router.get('/', function (req, res, next) {
    res.setHeader('Content-Type', 'text/html')

    next(template(_.extend({}, req.base_params, {
      // extra variables
    })))
  })

  return {
    basePath: 'users',
    title: 'Users',
    middleware: router
  }
}
{{#extend "layout"}}
  {{#content "main"}}
    <section class="container">
      <h1>My really cool Sinopia web plugin</h1>
    </section>
  {{/content}}
{{/extend}}

And will be available at /-/users (where users is the basePath exported by the plugin).

When present, plugins are available from the header as a dropdown menu.

sinopia-plugins

This change only concerns the web interface, and does not change anything in the rest of the application nor makes anything incompatible with previous versions of sinopia.

Please tell me what you think about it.

Thanks.

@danhper danhper force-pushed the web_plugins branch 8 times, most recently from 7ea6879 to 4470b77 Compare May 2, 2015 08:10
@danhper
Copy link
Author

danhper commented Jun 2, 2015

@rlidwka Any thoughts about this?

@rlidwka
Copy link
Owner

rlidwka commented Jun 7, 2015

@tuvistavie , well my thoughts were mostly around "oh my god I need to find a time to check this".

What's handlebars-layouts? If handlebars can't handle blocks out of the box, maybe switch to jade? Just a wild thought.

Looks like a good change overall.

config: config,
logger: logger,
auth: auth,
storage: storage,
Copy link
Owner

Choose a reason for hiding this comment

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

What auth and storage are supposed to be used for in plugins?

Copy link
Author

Choose a reason for hiding this comment

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

For the auth, it would be mostly useful to check the roles of the user, and dynamically change the content of the plugin depending on it.
For the storage, it could be used to build some package related stuff, for example a plugin that could give more details about each package.

Copy link
Author

Choose a reason for hiding this comment

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

By the way, for the auth part, I am already using it in
https://github.com/tuvistavie/sinopia-users-management
For the moment, this allows me to add users easily through the web interface.

@danhper
Copy link
Author

danhper commented Jun 7, 2015

Hi, thank you for the feedback, I understand it is quite a heavy PR.
I did not want to change parts that are not absolutely necessary, so
I used handlebars-layouts to avoid changing the template engine,
but I agree that Jade is a more robust solution.
So if you are ok with it I'll change the template language to Jade.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants