mojolicious perl web 框架学习一
以前是通过传统模式安装的,以下是通过perlbrew 安装以及使用
安装perlbrew
安装perl
注意选择需要5.20 以及的版本
perlbrew install perl-5.32.0
perlbrew switch perl-5.32.0
安装mojolicious
基于以上配置的perl 环境
curl -L https://cpanmin.us | perl - -M https://mirrors.tuna.tsinghua.edu.cn/CPAN/ -n Mojolicious
使用cli 命令
- 支持的cli
Usage: APPLICATION COMMAND [OPTIONS]
mojo version
mojo generate lite-app
./myapp.pl daemon -m production -l http://*:8080
./myapp.pl get /foo
./myapp.pl routes -v
Tip: CGI and PSGI environments can be automatically detected very often and
work without commands.
Options (for all commands):
-h, --help Get more information on a specific command
--home <path> Path to home directory of your application, defaults to
the value of MOJO_HOME or auto-detection
-m, --mode <name> Operating mode for your application, defaults to the
value of MOJO_MODE/PLACK_ENV or "development"
Commands:
cgi Start application with CGI
cpanify Upload distribution to CPAN
daemon Start application with HTTP and WebSocket server
eval Run code against application
generate Generate files and directories from templates
get Perform HTTP request
inflate Inflate embedded files to real files
prefork Start application with pre-forking HTTP and WebSocket server
psgi Start application with PSGI
routes Show available routes
version Show versions of available modules
See 'APPLICATION help COMMAND' for more information on a specific command.
- 创建一个项目
mojo generate lite-app demoapp
效果
- 运行
morbo demoapp
- 部署
推荐基于hypnotoad
hypnotoad ./demoapp
- 容器部署模式
参考
FROM perl:5.26
RUN cpan install Mojolicious::Lite \
&& mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY demoapp /usr/src/app/
ENTRYPOINT [ "sh","/usr/src/app/demoapp","daemon","-m","production","-l","http://*:8080" ]
参考资料
https://perlbrew.pl/
https://mojolicious.org/
http://mirrors.ustc.edu.cn/
https://docs.mojolicious.org/Mojo/Server/Hypnotoad