Browser-based app framework for Ruby - html

I've a ruby script running out of the command line. I want to provide a local GUI for it (for my use). As I have some exposure to Sinatra and other web frameworks, I want to use HTML pages as my front-end. But I don't want to start a server and type in a URL every time I want to launch my app.
My solution would be to write a shell script which will start a Sinatra based server and then launch Chromium(Browser) in app mode to that url.
Is there some framework which can do it better/cleaner?
I'm not interested in learning a non-HTML framework like Shoes or Ruby-Gnome2.

#!/bin/sh
ruby $1 &
chromium localhost:4567
Put that somewhere in your $PATH (or change it to contain $HOME/bin with export PATH=$HOME/bin:$PATH and put it there), make it executable with chmod +x <file> and have fun by calling <file> <sinatra startup file>
You could extend this to read the port from Sinatra, but that would require a ruby startup, and this should do in most of the cases (the 80%, as people call it).

Related

Kickstart file inside boot_command

I have the following question. I want to build a Centos 7 imagine using Packer. I want to run the template without a kickstart file .cfg due to some security issues. Is it possible to write all the commands from the kick start file to the template.json?
This is how it actually looks:
"boot_command": "<tab> inst.text inst.ks=https://raw.githubusercontent.com/xxx/xxx/xxx/centos-7.cfg <enter><wait>",
# Turning on text-mode installation (little quicker than GUI)
text
# Setting up authentication and keyboard
auth --enableshadow --passalgo=sha512
keyboard --vckeymap=us --xlayouts='us'
# Installation files source (CentOS-7.0-1406-x86_64-Minimal.iso)
cdrom
# Using only primary disk, ignoring others
ignoredisk --only-use=sda
# Setting up language to English
lang en-US.UTF-8
...
And I want something like:
text
auth --enableshadow --passalgo=sha512
keyboard --vckeymap=us --xlayouts='us'
cdrom
ignoredisk --only-use=sda
lang en-US.UTF-8"
...
Unfortunately, the virtual machine doesn't realize that I am sending these commands as kickstart commands and it starts in the GUI mode.
How is your source built?
You will need to create a boot command directive that simulates the keys so that it can start and make the ks file available for the installation (using floppy_dirs or similar)

Running Ruby script with HTML button?

I have a Ruby script that I run through the Command Line locally. Now I want to run that script through a browser, with a simple HTML button. Is there any way to make a simple HTML page and run the script similar to a JS onclick event? OR will I have to set up a rails environment first?
To run a Ruby script, you need a Ruby interpreter. JavaScript can be run locally by the browser that executes the HTML page, Ruby no.
Therefore, your cannot run the Ruby code directly in the browser. There are several possible solutions.
You can host the Ruby script somewhere, and have the HTML page reference it. If the script is reasonably simple, you can use one of those services that run a Ruby script in a sandbox.
But if it's a Rails app, no way. You need to host it somewhere.
Short answer, you need a server/computer with the Ruby interpreter to execute your script. It can't be executed by the client browser.
There are a few options here. Depending on your use case the most likely two are:
Convert your Ruby script into JavaScript via Opal so that it can run in the browser.
Set up a simple server with Sinatra and run it on a server somewhere, then call it via AJAX.
There are couple of options available for your use case but I would use Sinatra for this. Here is a very simple example for you.
your_file.rb
require 'sinatra'
get '/' do
"<a href='/another-file'> Link to another file </a>"
end
get '/another-file' do
"Today is #{Time.now}"
end
If you do not have sinatra gem installed then please try
gem install sinatra
Once done then you can run your script on your desire port (I'm using 5000)
ruby your_file.rb -p 5000
Now you can browse your simple app at http://localhost:5000. Please checkout sinatra website for complete documentation.

How to call a ruby script from webpage and output the results?

Okay so maybe you guys can advise me. Here is what I am attempting to do.
I am trying to call my ruby script from a html form on a website that has already been created.
The form needs to pass a variable entered in a text box to the ruby script.
Once the script is finished (it's doing a sql query) it needs to output the results to the webpage.
What do you guys think this is the best way to go about doing this? I looked at Rails but the webpage is already created and I don't want to create full scale webapp. I also looked at Rack, but I am not sure if that is the best option. Thanks!
Ruby code is executed in a RVM (MRI, JRUBY, etc). It is served by a Webserver(NGINX, Apache, etc) which processes it by a RVM WebServer stack(CGI/Phussion Passenger) and passes it through Ruby middleware (Rack) and associated frameworks (Sinatra, Rails, etc).
Some WebServers have RVM Stacks built into them Puma, Unicorn, etc.
Using nginx to proxy-pass a specific request path from a url to a RVM enabled webstack + sinatra app would work fine. You might include the uri/pages as an iframe to inject into your current server as well.
A simple example of exposing a sinatra app running in a rvm unicorn stack through nginx.
https://github.com/p8952/nginx-unicorn-sinatra

Simplest way to host html [duplicate]

This question already has answers here:
Best lightweight web server (only static content) for Windows [closed]
(8 answers)
Closed 8 years ago.
What is the simplest way to host an HTML page over LAN?
I literally just need to have like 5 lines of HTML, so I don't want to download and setup an Apache server. I just want to know the fastest/simplest way to do this on Windows, or I can also use one of my Linux virtual machines if it's faster.
Use netcat, or nc:
:top
nc -l -p 80 -q 1 < index.html
goto top
It's a simple binary without any installation. It doesn't do CGI or PHP or anything, but it can sure dish up 5 lines of HTML.
Actually, if you use the "k" (keep-alive) option you can remove the loop, and make it simpler:
nc -kl 80 < index.html
Since you need a web server for testing and no heavy concurrent use is expected, I'll just keep it simple.
Please note that both solutions are very simple but not very secure, use them for development purposes but don't rely on neither of them for anything barely similar to a stable (people would say "production") server.
Navigate to the directory where your HTML file is located using cmd.exe, then issue:
Using Python
python -m SimpleHTTPServer
A HTTP server will be started on port 8000. Should you need a different port, just specify it:
python -m SimpleHTTPServer 8080
SimpleHTTPServer is part of the "batteries included": you will not need to install any extra package, apart from the Python interpreter, of course.
Python comes already installed on most Linux distributions, so switching to Linux might be simpler than install Python on Windows, although that boils down to downloading and running an installer.
Using PHP 5.4 or above
php -S 0.0.0.0:8080
This will also process PHP scripts, but HTML resources will be served fine.
http://www.lighttpd.net/ is pretty light weight and easy to get running.
I recently used mongoose for a similar purpose. It supports Windows. From the homepage:
Mongoose executable does not depend on any external library or
configuration. If it is copied to any directory and executed, it
starts to serve that directory on port 8080. If some additional config
is required - for example, different listening port or IP-based access
control, then a mongoose.conf file with respective options (see
example) can be created in the same directory where executable lives.
This makes Mongoose perfect for all sorts of demos, quick tests, file
sharing, and Web programming.
Download the windows exe (no need to install) from here , save it on the folder where your html file is and execute it. Check the image below to know how to start the server:
After selecting Start Browser on Port 8080 your browser will open automatically displaying the contents of the folder.

Installing JSON.pm on a web host without shell access

My host (iPage) does not have JSON.pm installed. I don't want to use the modules they have installed (XML) in order to transfer data from a CGI script back to a web page. Is there anyway that I can use JSON without them installing it on Perl?
The reason I ask is because I noticed when I downloaded the JSON zip that I had to run a makefile json.pm command but I don't have access to a Unix shell or a SSH terminal.
If your Perl is new enough, 5.14 and up, it will come with JSON::PP, a pure Perl implementation of the JSON parser. Confusingly it does not come with JSON.pm. So try use JSON::PP and see if it works.
Otherwise, follow Ilmari's instructions. If you switch to a host with shell access, you can use local::lib to manage CPAN modules.
You should be able to install a local copy of the pure Perl version of the JSON module without shell access. Just download the .tar.gz archive to your own computer, unpack it and copy everything under the lib subdirectory to a suitable location on your webhost.
You'll also need to tell Perl where to find the module, for which you need to know the filesystem path to which you copied the module. For example, if you copied the contents of the lib directory to /home/username/perl-lib on your webhost, then you would include in your code the lines:
use lib '/home/username/perl-lib';
use JSON;
Depending on how your webhost is configured, you might also be able to use $ENV{HOME} to obtain the path to your home directory, so that you can write:
use lib "$ENV{HOME}/perl-lib";
use JSON;
or you could try using the FindBin module to find the path to the directory containing your script, and locate the lib directory from there (see the example in the FindBin documentation).