目前一些 Web 框架 比如 Gin fiber 等 都支持 HTTP 服务,可以使用静态资源
func main() { app := fiber.New() app.Static("/", "./home.html") ..... }
将打包编译的二进制文件 与 html 静态资源文件放置在一起 "/usr/local/bin/", 想通过创建 systemd 服务来管理进程
[Unit] Description=GoWeb Server After=network.target ntp.service [Service] PrivateTmp=true Type=simple ExecStart=/usr/local/bin/goweb ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s SIGINT $MAINPID User=root Group=root [Install] WantedBy=multi-user.target
程序启动后发现调用的 "app.Static("/", "./home.html")" 相对路径找不到资源, 发现 systemd 启动的环境变量可能不对? 而手动启动 /usr/local/bin/goweb 能找到资源
[root@VM-16-10-~]# systemctl start goweb 查看状态 [root@VM-16-10-~]# systemctl status goweb ctrls.service - GoWeb Server Loaded: loaded (/etc/systemd/system/go-web.service, enabled) Active: active (running)
[Unit] Description=GoWeb Server After=network.target ntp.service [Service] PrivateTmp=true Type=simple WorkingDirectory=/usr/local/bin/ ExecStart=/usr/local/bin/goweb ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s SIGINT $MAINPID User=root Group=root [Install] WantedBy=multi-user.target
//go:embed views/* var viewsfs embed.FS
1 tairan2006 2022-11-11 09:39:14 +08:00 ![]() 你没配置 WorkingDirectory… |
![]() | 2 anviod OP |
![]() | 3 Great233 2022-11-11 09:57:36 +08:00 ![]() 静态资源可以用 embed 包嵌入到 build 之后的 go 程序里面去 |
![]() | 4 Great233 2022-11-11 09:57:48 +08:00 |
6 julyclyde 2022-11-11 11:42:11 +08:00 像你这么上进的人不多 很少有考虑在 systemd 环境里运行的一些细节的 |
![]() | 7 anviod OP @julyclyde systemd 管理进程挺好的,省的再开发守护进程了, 虽然 systemd 很复杂,但是用起来还是很好的, 怪自己之前没详细看 systemd 文档. |
![]() | 8 yin1999 2022-11-12 13:18:07 +08:00 via Android 可以使用 dynamicUser ,直接 root 有点粗暴 |
10 sibowen 2022-12-16 11:36:58 +08:00 程序静态文件目录,可以考虑用配置,或者环境变量来传入吧,这么搞改动起来比较费劲的。 |