algernon
algernon
是一个小型独立纯 Go Web
服务器,支持 Lua
、Markdown
、HTTP/2
、QUIC
、Redis
和 PostgreSQL
。
特别是对于lua
的解析,还是挺有用的,把lua
的结果直接渲染成html
,以后就可以直接用lua
来写html
代码了。
github的地址在这里。
algernon
支持了很多的东西,包含内置支持 QUIC
、HTTP/2
、Lua
、Teal
、Markdown
、Pongo2
、HyperApp
、Amber
、Sass(SCSS)
、GCSS
、JSX
、BoltDB
的 Web
服务器(内置,将数据库存储在文件中,如 SQLite
)、Redis
、PostgreSQL
、MariaDB/MySQL
、速率限制、正常关闭、插件、用户和权限。
安装
需要 Go 1.19 或更高版本。
go install github.com/xyproto/algernon@latest
或手动:
git clone https://github.com/xyproto/algernon cd algernon go build -mod=vendor ./welcome.sh
示例
安装完成了以后,运行./welcome.sh
的脚本以后,我们会得到如下的输出,这里面的信息提示我们,只要访问localhost:3000/
就能看到相应的网站,比较好玩的是,在终端怎么能打印出这一对眼睛呢?是不是挺有趣的。后面我们在分析分析这个代码,这得下一点功夫,让他看起来有爱一点,让人感到温暖。
这时候我们打开localhost:3000/
,这的是看到了网站,网站的内容就是几个demo
的示例。先来看看这个页面是哪里来的。
启动脚本中../welcome.sh
,源码如下:
#!/bin/sh echo 'Try editing the markdown file in "samples" directory and see the' echo 'results instantly in the browser at http://localhost:3000/' ./algernon --dev --conf serverconf.lua --dir samples --httponly --debug --autorefresh --bolt --server "$@"
一段 执行脚本而已,并没有什么特别的,--dir
应该是指定了脚本的路径,那这个页面应该是在samples
目录的index
下,打开samples
目录发现了index.md
。 果然把Markdown
的文件渲染为HTML
了。
<!-- title: Algernon Server --> # Algernon Server Welcome ------- Your local Algernon server is up and running. You can find this Markdown file in the `samples/` directory. Try changing `index.md` in your favorite editor and watch this page refresh instantly as you save. Samples ------- * [Styled Markdown](greetings) * [Simple TODO application](todo) * [Poem by Algernon Charles Swinburne](threejs) * [Using React with Algernon](react_db) * [Hello Lua](lua) * [Counter](counter) * [Iterate](iterate) * [Bootstrap](bootstrap) * [Permissions](permissions) There are more samples in the `samples/` directory. Resources --------- * [GCSS documentation](https://github.com/yosssi/gcss/blob/master/README.md) * [Amber documentation](https://github.com/eknkc/amber/blob/master/README.md) * [Markdown documentation](https://daringfireball.net/projects/markdown/basics) * [Algernon web page](http://algernon.roboticoverlords.org/) * [Algernon project page](https://github.com/xyproto/algernon/) * [Learn Markdown in Y minutes](https://learnxinyminutes.com/docs/markdown/) * [Markdown tutorial](http://markdowntutorial.com/) * [Lua at Rosetta Code](https://rosettacode.org/wiki/Category:Lua) * [Learn Lua in Y minutes](https://learnxinyminutes.com/docs/lua/)
我们会发现有一个相关的style.gcss
,这个就是我们的样式了,通过修改style.gcss
可以修改出自己喜欢的样式来。
其中比较有意思的应该是Hello Lua
,他也是在samples
目录 下的lua
目录,文件为index.lua
简简单单的一句话:
print("Hello, Lua!")
这样我们就可以用lua来写html页面了。
algernon
的功能还是比较多的,更多的直接了解相关的网站。
以下文件名是特殊的,按优先顺序排列:
- index.lua 是 Lua 代码,被解释为当前目录的处理函数。
- index.html 是使用正确 Content-Type 输出的 HTML。
- index.md 是呈现为 HTML 的 Markdown 代码。
- index.txt 是使用正确 Content-Type 输出的纯文本。
- index.pongo2、index.po2 或 index.tmpl 是呈现为 HTML 的 Pongo2 代码。
- index.amber 是呈现为 HTML 的 Amber 代码。
- index.hyper.js 或 index.hyper.jsx 是呈现为 HTML 的 JSX+HyperApp 代码
- index.tl 是Teal 代码,被解释为当前目录的处理函数。
- data.lua 是 Lua 代码,其中函数和变量可用于同一目录中的 Pongo2、Amber 和 Markdown 页面。
- 如果将单个 Lua 脚本作为命令行参数给出,它将被用作独立服务器。它可用于设置处理程序或为特定 URL 前缀提供文件和目录。
- style.gcss 是 GCSS 代码,用作同一目录中所有 Pongo2、Amber 和 Markdown 页面的样式。
代码分析
我们刚才讲到,那个小眼睛是怎么展示出来的,这里的代码就是用来展示小眼睛的。
func decompressImage(asciigfx string) string {
unbasedBytes, err := base64.StdEncoding.DecodeString(asciigfx) if err != nil {
panic("Could not decode base64: " + err.Error()) } buf := bytes.NewBuffer(unbasedBytes) decompressorReader, err := gzip.NewReader(buf) if err != nil {
panic("Could not read buffer: " + err.Error()) } decompressedBytes, err := io.ReadAll(decompressorReader) decompressorReader.Close() if err != nil {
panic("Could not decompress: " + err.Error()) } return string(decompressedBytes) }
显示:
func Banner(versionString, description string) string {
tabs := "\t\t\t\t" s := tabs + strings.ReplaceAll("\n"+decompressImage(image), "\n", "\n"+tabs) parts := strings.Fields(versionString) // See https://github.com/shiena/ansicolor/blob/master/README.md for ANSI color code table s = insertText(s, tabs, 3, 2, "\x1b[37m"+parts[0]+"\x1b[0m", 1) s = insertText(s, tabs, 4, 1, "\x1b[90m"+parts[1]+"\x1b[0\t", 1) s = insertText(s, tabs, 5, 1, "\x1b[94m"+description+"\x1b[0m", 1) return s }
今天的文章
Github每日精选(第43期):小型独立纯 Go Web 服务器algernon,支持 Lua、Markdown、HTTP/2、QUIC、Redis 和 PostgreSQL分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/87803.html