lundi 27 janvier 2014

[Bottle + Tornado] From a Bottle application, sending parameters to the Tornado server

When you use Bottle + Tornado (or CherryPy,... etc...), you can give options to the Tornado server, by declaring them inside the Bottle run(...) method.

Here hello_bottle.py :

from bottle import route, run, template
import os

@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)

data_dir='/'

#we give ssl_options to the Tornado server :
run(host='localhost', port=8080,server='tornado',ssl_options={
    "certfile": os.path.join(data_dir, "mydomain.crt"),
    "keyfile": os.path.join(data_dir, "mydomain.key"),
})

And when we execute :

python hello_bottle.py

Bottle v0.13-dev server starting up (using TornadoServer(ssl_options={'keyfile': '/mydomain.key', 'certfile': '/mydomain.crt'}))...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

Voila :o)


Source : 
SSL with Tornado :

Sending parameters to CherryPy:

Servers allowed with Bottle :

lundi 6 janvier 2014

Comment se compliquer la vie en Java...

Mouaaaaa, je trouve ça marrant, les gens qui veulent montrer qu'ils connaissent les toutes dernières fonctionnalités de Java.

Voilà un exemple concret : suppression basique d'un répertoire temporaire (je dis bien : une simple "SUPPRESSION BASIQUE").

Ça c'est la version "je me la pète je maîtrise Java 7" :

Files.walkFileTree(tempDirectory, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file,
                        BasicFileAttributes attrs) throws IOException {

                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir,
                        IOException exc) throws IOException {

                    if (exc == null) {
                        Files.delete(dir);
                        return FileVisitResult.CONTINUE;
                    } else {
                        throw exc;
                    }
                }
});

Euhhhh oui, c'est beau, il y a plein d'indentations et ça fait 15-20 lignes quand même...

Pourquoi ne pas implémenter une toute petite méthode récursive (le truc de base que l'on apprend en cours) ou tout simplement faire appel à Apache Commons (FileUtils.deleteDirectory(tempDirectory)) ?

Hormis le fait que je râle sur ce satané bout de code "tout moche" sur lequel je viens de tomber,
il y a derrière un vrai problème de fond : "faire la part des choses entre la maintenance/lisibilité et l'utilisation systématique/radicale/extrême des fonctionnalités évoluées de Java".

Categories