Procedures for starting Mojolicious development on Windows and publishing it on Sakura's rental server

Develop a web application using a web framework called Mojolicious on Windows, Sakura's rental server light I would like to write the procedure until it is published in the plan. The light plan is 125 yen per month, so you can feel free to try the web framework. The nice thing about Perl is that you can easily create web applications using modern web frameworks.

Let's also use a database called SQLite.

Environment construction

ActivePerl includes DBD::SQLite and DBI, so you don't need to install any database related modules. Also, SQLite can be used from the beginning on Sakura's rental server, so you do not need to prepare anything here either.

All you have to do is download Mojolicious. This time I will try it with version 1.47.

Download Mojolicious from the following page.

Mojolicious-1.47

Download

The downloaded file is compressed, so move it to the development directory and extract it. This time, let's move to "C: \ labo" and expand it. You can expand it by right-clicking → Expand All.

You should find the following files under the directory "C: \ labo \ kraih-mojo-v1.47-commit number".

kraih-mojo-commit number

Let's change this directory name to the name of the application we are about to create. Let's say myapp. This directory contains all the features of Mojolicious and is available to you.

myapp

Script creation

Go inside this directory and create a file called "app.cgi".

myapp --app.cgi

Let's create the following script. A word is a bulletin board. I am creating a bulletin board using a module called Mojolicious::Lite in the Mojolicious framework. Save this script in UTF-8.

#! / usr / bin / perl

Setting the library path to load #Mojolicious
use FindBin;
use lib "$FindBin::Bin / lib";

Using #Mojolicious::Lite
use Mojolicious::Lite;

use utf8;

use DBD::SQLite;
my $sqlite_unicode
  = $DBD::SQLite::VERSION> 1.26
  ?'sqlite_unicode'
  :'unicode';

#Module for database
use DBI;

#Connect to database
my $database = app->home->rel_file('app.db');
my $dbh = DBI->connect(
  "dbi: SQLite: dbname = $database",
  undef,
  undef,
  {RaiseError => 1, PrintError => 0, AutoCommit => 1,
   $sqlite_unicode => 1}
);

#Create table
$dbh->do(<< "EOS");
create table if not exists entry (
  id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
  message message
);
EOS

# top page
get'/' => sub {
  my $self = shift;
  
  my $sth = $dbh->prepare(<< "EOS");
select message from entry order by id desc;
EOS

  $sth->execute;
  
  my $rows = [];
  while (my $row = $sth->fetchrow_hashref) {
    push @$rows, $row;
  }
    
  $self->render(rows => $rows);
} =>'index';

#Data registration
post'/ register' => sub {
  my $self = shift;
  
  my $param = $self->req->params->to_hash;
  my $message = $param->{message} ||'';
  
  if ($message) {
    my $sth = $dbh->prepare(<< "EOS");
insert into entry (message) values ​​(?);
EOS
    $sth->execute($message);
  }
    
    $self->redirect_to('/');
};;

#Start application
app->start;

__DATA__

@@index.html.ep
<html>
  <head>
    <meta http-equiv = "Content-type" content = "text / html; charset = UTF-8">
    <title> Message board with just one word </title>
  </head>
  
  <h1> Message board with just one word </h1>
  <body>
    <form method = "post" action = "<%= url_for'/ register'%>">
      <div> message <input type = "text" name = "message"> </div>
      <div> <input type = "submit" value = "submit"> </div>
    </form>
    <div>
      %foreach my $row (@$rows) {
        <div> <%= $row->{message}%> </div>
        <hr>
      %}
    </div>
  </body>
</html>

(Reference) FindBin

Since the recommended UNICODE option differs depending on the version of DBD::SQLite, it is selected and used properly.

File upload

Please refer to the following for how to use FFFTP.

FFFTP (Sakura Internet)

Upload the files under the myapp directory to the following directories as they are.

/ home / username / www

Please note that "app.cgi" needs to be transferred in ASCII mode. By default, FFFTP will transfer files ending in ".cgi" in ASCII mode, so it's probably okay.

Next, you need to change the permissions only for "app.cgi". Please change the permission from "Right click"->"Attribute" to 755.

Access from the Web

You should now be able to see it in your browser from the URL below.

http://username.sakura.ne.jp/myapp/app.cgi

good job for today.

Associated Information