googlecreativelab / coder

A simple way to make web stuff on Raspberry Pi
http://goo.gl/coder
Apache License 2.0
2.42k stars 275 forks source link

No php support #7

Open Code-nomad opened 10 years ago

Code-nomad commented 10 years ago

your platform is really awesome. I'm just now taking it for a test spin. Is it possible to somehow add php support though? For me the lack of php is really an issue.

If you point me in the direction of a commandline I'll gladly commit or fork a revised version.

Thanks in advance!

WebReflection commented 10 years ago

and what about Python ? :-)

Funny enough, you can map urls to use PHP through the cli interface as "slow" CGI execution via spawn('php', ['-e', fs.fileGetSync(req.url)]) or something similar and return the result from the pipe.

This will be surely slower but much less problematic due Apache and ports stack on top of node Express, IMO.

Have you tried that?

Code-nomad commented 10 years ago

Not yet. But my Linux knowledge is not super extensive. Do you have a tutorial link? On Sep 13, 2013 5:44 PM, "Andrea Giammarchi" notifications@github.com wrote:

and what about Python ? :-)

Funny enough, you can map urls to use PHP through the cli interface as "slow" CGI execution via spawn('php', ['-e', fs.fileGetSync(req.url)]) or something similar and return the result from the pipe.

This will be surely slower but much less problematic due Apache and ports stack on top of node Express, IMO.

Have you tried that?

— Reply to this email directly or view it on GitHubhttps://github.com/googlecreativelab/coder/issues/7#issuecomment-24403692 .

WebReflection commented 10 years ago

you could do something like this for testing purpose only.

var
  fs = require('fs'),
  http = require('http'),
  path = require('path'),
  spawn = require('child_process').spawn,
  EOL = require('os').EOL,
  type = {
    '.js': 'application/javascript',
    '.css': 'text/css',
    '.png': 'image/png',
    '.jpg': 'image/jpg',
    '.gif': 'image/gif'
  }
;
http.createServer(function(req, res){
  var
    parts = req.url.split('?'),
    file = path.resolve(path.normalize('.' + parts[0])),
    ext = file.slice(-4),
    get = parts.slice(1).join('?'),
    php
  ;
  if (ext === '.php') {
    // assuming php used for HTML only
    res.writeHead(200, {
      'Content-Type': 'text/html'
    });
    php = spawn(
      'php',
      [
        '-r',
        [
          // POST is not supported (yet)
          "parse_str($_SERVER['_GET'], &$_GET);",
          "include('" + file + "');"
        ].join(EOL),
      ],
      {
        cwd: undefined,
        env: {
          _GET: get
        },
        detached: true,
        stdio: ['pipe', 'pipe', 'pipe']
      }
    );
    php.stdout.pipe(res);
    php.stderr.pipe(process.stderr);
    php.stdin.pipe(process.stdin);
    php.on('close', function(){
      res.end();
    });
    php.unref();
  } else {
    // TODO: missing everything else
    // a webserver could do ...
    res.writeHead(200, {
      'Content-Type': (type[ext] || 'text/plain').toLowerCase()
    });
    res.write(fs.readFileSync(file, 'utf-8'));
    res.end();
  }
}).listen(3333, '0.0.0.0');

If you have a test.php file, as example, with such content:

<?php
echo "Hello {$_GET['name']}!";
?>

and if you node php.js and go to http://localhost:3333/test.php?name=There you should see an HTML output saying: Hello There!

It's pretty simple/basic stuff but if you want to get started with coder and PHP is a quite simple/straight forward way to do it.

CMDann commented 10 years ago

PHP would hands down make this the best. I imagine it wouldn't be that amazingly hard to add php to coder? Would it be insane to fork and add php support?

eskwire commented 10 years ago

Also some CMS need database, apart of PHP, hopefully soon they would be able to add it, just to make it more like a server.

cfstras commented 10 years ago

As I see it, Coder is designed to bring newbies into programming without any fuss. Having and managing a Database like MySQL or Postgres is definitely not something you would want to learn if you're just starting to learn web development.

As such, if you get to a shell in coder, it'll be easy to deploy apache/php/mysql instead of nodejs. Also, if you want to use your Pi as a webserver, you should really look at some simpler solutions like Raspbian (Debian) or Pidora (Fedora) - You can get them on the RsPi download page

(Did I mention that in my opinion, PHP is one of the worst languages to teach to a newcomer to programming?)

shankarmozzapp commented 10 years ago

Adding MySQL support through a Node module should be possible (I think?). I agree that looking for PHP/Python support might not be the direction to go, for the stated goals of the project.

If we can experiment/explain how to add MySQL support through a Node module, that would be an awesome addition to this stack, I reckon.

WebReflection commented 10 years ago

if you want SQL like database feel free to npm install dblite and easily connect to it dblite you don't have to build or compile anything as long as any version of sqlite3 is in (and it should be at least 3.7 here ;-))