GithubHelp home page GithubHelp logo

mozilla / templeton Goto Github PK

View Code? Open in Web Editor NEW
3.0 11.0 4.0 329 KB

INACTIVE - http://mzl.la/ghe-archive - Basic framework for rapid creation of web tools

JavaScript 46.85% Python 53.15%
inactive unmaintained

templeton's Introduction

templeton, a minimal web framework

Templeton is a script, daemon, module, and support files intended for rapid development of web applications.

It currently runs on Linux. Development mode should run on OS X, but I haven't tried deployment mode.

Templeton apps

Templeton apps are essentially web.py apps with various enhancements and a (highly) suggested structure. They are managed by the templeton app server, templetond.

The main goal of templeton is to streamline the process of developing and deploying web apps. While developing, templeton provides a simple development server and a module to handle a lot of the underlying mechanics of modern web apps, such as decoding URL search/query strings and serving simple Python objects as JSON. When the app is ready to deploy, the templeton app server manages the app process and creates appropriate web-server config files.

Pursuant to this goal, templeton preserves the relative URL path in development and deployment. The development server run at the root directory of localhost on a given port. When deployed, the app is configured to have the same structure but live in a subpath, alongside other applications and content.

Templeton favours highly dynamic web sites based on the Single-Page Application pattern. To this end, static files are served at the base of the application's path, and dynamic content is (by default) served under api/. It is expected that index.html would be a static file that, with the help of JavaScript, would load dynamic content asynchronously. At the same time, this dynamic data is available at a known place to external applications.

(There are plans to remove this restriction so that templeton apps can also follow a template-based pattern, with static content consisting mainly of images, CSS, and JS.)

Templeton also packages up some common support web files, such as the jQuery library. The development server makes these available under /templeton. For deployment, the templeton command 'install' will copy the templeton/ directory to the root of a web server's html dir, again, so the apps can link to these files at a known location valid in both development and deployment modes.

For deployment, templeton currently supports nginx with spawn-fastcgi.

The templeton script

The templeton script is the user's point of contact with the whole templeton system.

Usage:

templeton install <www-data-dir>

Copies support files (JS, CSS) into a "templeton" directory in www-data-dir. The latter should be the root of the web site that will serve templeton apps, since the template HTML file loads JS and CSS from /templeton.

templeton init <appname>

Creates a new templeton project in a directory called appname, relative to the current working directory. If virtualenv is available, a virtualenv is installed to the new directory, and the template files are installed to appname/src/appname/. If virtualenv is not available, the templates are copied directly into appname/.

You should be able to serve up your default app by doing

# virtualenv available
source <appname>/bin/activate
cd <appname>/src/<appname>/server
python server.py [port]  # [port] defaults to 8080

or

# no virtualenv
cd <appname>/server
python server.py [port]  # [port] defaults to 8080

Go to http://localhost:8080/ to see the result. The next steps you'll want to do is edit handlers.py and put in your server-side business logic, and edit and create the files in ../html/ to build up your client-side logic.

The rest of the commands interface with the templeton app server:

templeton register

Registers the app in the current working directory with the app server. This also creates a web-server config file, for deployment. Note that you (probably) have to reload or restart your web server in order to pick up this config.

templeton unregister

Removes this app from the care of the app server.

templeton disable

Tells the app server not to start this app automatically.

templeton enable

Tells the app server to start this app automatically, if it was previously disabled.

templeton start

Starts the FastCGI process for dynamic content.

templeton stop

Stops the FastCGI process.

templeton list

Lists all templeton apps, with their status.

The templeton module

The templeton module has two main functions:

  • set up middleware to separate static pages from dynamic calls.
  • provide helpers for common tasks, such as handling specific request types.

middleware

The middleware module provides the patch_middleware() function to patch web.py's development server to serve the app with the standard templeton structure. The server.py template includes the necessary call.

handlers

templeton is geared toward client-rich, REST-based web applications. These typically involve a large amount of JSON. templeton provides decorators to simplify handler code.

  • @json_response is a decorator function that expects the decorated function to return a JSON-serializable object, which it uses to construct a proper web.py response.

  • @png_response expects the decorated function to return a PIL object, which it serves as a PNG image.

Both decoraters also redirect any URLs that don't end in a slash to one that does, to enforce some consistency. Thus if you access api/foo, it will redirect to api/foo/. Your handler URLs should reflect this.

The handlers module also provides helper functions.

load_urls() takes a web.py URL-handler sequence, i.e. (path, class_name, path, class_name, ...), and prepends the REST API path, '/api', to each given path. The default server.py (created by the 'init' script command) uses this function to load urls from handlers.py.

get_request_parms() parses the current request's search string and body as JSON and returns the results as (args, body).

A trivial example of a JSON handler that echoes back any search-string args:

import templeton

urls = (
    '/test/?', 'JsonTest'
)

class JsonTest(object):

    @templeton.handlers.json_response
    def GET(self):
        args, body = templeton.handlers.get_request_parms()
        return args

With the development server running on port 8080,

$ curl 'http://localhost:8080/api/test/?cat=meow&dog=bark&cat=purr'
{"dog": ["bark"], "cat": ["meow", "purr"]}

templeton's People

Contributors

wlach avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

templeton's Issues

duplicate ports assigned

Seems that, at least sometimes, a previously assigned port is assigned in response to the "register" command, so the app can't start since the port is taken.

Support virtualenv

templeton should support virtualenv, understanding the directory structure and optionally creating a virtualenv when creating a new project via the 'init' command.

More/better error messages

Right now we crash with no errors on these occasions:

  • nginx locations dir is not writable
  • spawn-fcgi not available

Don't require the cwd to be the application root for most commands

Commands like 'start' and 'stop' require the cwd to be the root of the application, so it can glean the app name from the directory name. You should be able to run these commands from within a subdir. Only the 'register' command should require the cwd to be the app root.

Rather than figuring out the app within the templeton script, which would require pinging the app server, we should sent the cwd to the server and have it figure it out instead.

CODE_OF_CONDUCT.md file missing

As of January 1 2019, Mozilla requires that all GitHub projects include this CODE_OF_CONDUCT.md file in the project root. The file has two parts:

  1. Required Text - All text under the headings Community Participation Guidelines and How to Report, are required, and should not be altered.
  2. Optional Text - The Project Specific Etiquette heading provides a space to speak more specifically about ways people can work effectively and inclusively together. Some examples of those can be found on the Firefox Debugger project, and Common Voice. (The optional part is commented out in the raw template file, and will not be visible until you modify and uncomment that part.)

If you have any questions about this file, or Code of Conduct policies and procedures, please reach out to [email protected].

(Message COC001)

Should be possible to execute commands outside of app dir

Commands like start, stop, register, etc., require the cwd to be the app's base. It should be possible to specify the app by name for some of these commands (start/stop/disable/enable), or the path to the directory for others (register).

templetond seems to freeze sometimes

Probably need more logging to figure this out, but sometimes when I run a templeton command, e.g. "templeton register", the templeton client just hangs, implying that the server listener is frozen.

flup should be a requirement of templeton

We keep running into the problem that flup isn't installed when we deploy a templeton app. This isn't obvious because it's not required for the development server, only when deployed via FastCGI. It should be added as a dependency in templeton's setup.py.

Wiki changes

FYI: The following changes were made to this repository's wiki:

  • defacing spam has been removed

  • the wiki has been disabled, as it was not used

These were made as the result of a recent automated defacement of publically writeable wikis.

Problems with templeton.fcgi

Two problems with templeton.fcgi:

  • it isn't marked executable by default
  • it appears to use the default python, which is not necessarily desirable

Add run command

We do so much through the templeton script, we may as well have it run the dev server as well.

Allow specification of path for dynamic and static content

Templeton more-or-less enforces a path structure, placing static content at the root and dynamic content under api/. Technically you can override the location of dynamic content in handlers.py, but the app server will still put it under api/ in the nginx config file. We can potentially have some way for the app server to identify when an app wants to serve its dynamic content under a different path--and may as well do this for static content too.

apps and log files went missing

At some point, probably when the server started up, /var/run/templetond/templeton.apps disappeared, and the templetond log file was cleared. Discovered this when rebooting a machine running the app server.

remind user that the web server needs to be restarted after registering app

Since templetond doesn't (and probably shouldn't...?) control the web server, it should remind the user to reload/restart the web server after a user registers a new app (since this is when the conf file is created).

Also put a comment in the docs and/or code to explain the reasoning that this file is created at registration even though it means the static files will be available before 'start' is run: simply to avoid restarting the web server when the app is restarted. Maybe an error page should be served instead of the real index.html if the app hasn't been started...?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.