在威联通NAS中使用LeaNote

威联通是支持supervisor的守护的,所以只需要在配置文件里加上LeaNote的启动配置就可以了 配置文件位于

/share/CACHEDEV1_DATA/.qpkg/container-station/etc/supervisord.conf

运行日志位于

/var/log/container-station/supervisord.log

LeaNote守护配置

[program:leanote]
command=LEANOTEPATH -importPath=github.com/htmambo/leanote -runMode=prod -port=9000 -srcPath=YOURLEANOTESRCPATH
directory=/share/www/
autorestart=true
startsecs=3
startretries=3
stdout_logfile=/share/www/wwwlogs/leanote.out.log
stderr_logfile=/share/www/wwwlogs/leanote.err.log
stdout_logfile_maxbytes=2MB
stderr_logfile_maxbytes=2MB
user=admin
priority=999
numprocs=1
process_name=%(program_name)s_%(process_num)02d

因为群晖的很多东西在重启后会自动不愿,所以重启看效果: 一切正常 威联通自带的NoteStation不支持MD,关键是它居然还是使用Docker封装了一堆东西(独立的Apache、MySQL、PHP),为了节省资源,所以直接放弃它了。尝试了下在威联通中搞git和go,测试能正常编译,正好把以前放在公网上的Leanote给搬到NAS中来。 Leanote要直接使用的话,用Docker比较方便,只是我懒得自己去打包Docker(因为我用的Leanote是在官方基础上修改了许多东西的,仓库地址:这里),所以我基本上是自己编译使用的。

安装Go环境

使用opkg,直接

opkg install go 
opkg install libcurl-openssl-dev
opkg install git-http

编译、运行

新版的Go需要使用mod来配置依赖关系,懒得挨个去重建,直接将以前的GO_PATH中的内容全部扔到/opt/go中去完整(包括以前生成的revel入口文件),然后直接编译:

go build -o /opt/bin/leanote github.com/htmambo/leanote/app/tmp
nohup /opt/bin/leanote -importPath=github.com/htmambo/leanote -runMode=prod -port=9000 >/share/www/wwwlogs/leanote.log &

替换wkhtmltopdf

这玩意一直没找到合适的可以在我的NAS中使用的方法,只好寻找一个替代方案了,GitHub中找到了一个方案,使用Docker来完成: Gotenberg:用于将Markdown文件,HTML文件和Office文档转换为PDF的无状态API

go get -u github.com/thecodingmachine/gotenberg-go-client

我没有使用Mod,因为怕把leanote给折腾坏了,就使用的还是老方案 示例代码:

package main

import (
    "flag"
    "fmt"
    "os"

    "github.com/thecodingmachine/gotenberg-go-client"
)

var input = flag.String("i", "", "Input text filename; default is os.Stdin")
var output = flag.String("o", "", "Output PDF filename; requiRed")
var help = flag.Bool("help", false, "Show usage message")

func main() {

    flag.Parse()

    if *help {
        usage("Help Message")
    }

    if *output == "" {
        usage("Output PDF filename is required")
    }

    if *input == "" {
        usage("Input URL is required")
    }
    c := &gotenberg.Client{Hostname: "http://127.0.0.1:3000"}
    req := gotenberg.NewURLRequest(*input)
    req.Margins(gotenberg.NoMargins)
    dest := *output
    c.Store(req, dest)
}

func usage(msg string) {
    fmt.Println(msg + "\n")
    fmt.Print("Usage: convert [options]\n")
    flag.PrintDefaults()
    os.Exit(0)
}

测试一下:

go run convert.go -i https://www.baidu.com -o test.pdf

可以正常生成test.pdf文件 可以开始改造Leanote了 添加两个配置项:

app.use_gotenberg=true   #是否开启gotenberg
app.gotenberg_host=http://127.0.0.1:3000  #gotenberg地址

相关的代码: 涉及两个文件,NoteController.go和ApiNoteController.go,函数名:ExportPdf

    useGotenberg, _ := revel.Config.Bool("app.use_gotenberg")
    gotenbergHost, _ := revel.Config.String("app.gotenberg_host")
    if(useGotenberg) {
        c := &gotenberg.Client{Hostname: gotenbergHost}
        req := gotenberg.NewURLRequest(url)
        req.Margins(gotenberg.NoMargins)
        c.Store(req, path)
    } else {
        // 生成之
        binPath := configService.GetGlobalStringConfig("exportPdfBinPath")
最后修改:4年前