V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
anviod
V2EX  ›  Go 编程语言

[求助]Go Web 部署 Systemd 启动后路径问题, 找不到静态资源

  •  1
     
  •   anviod · 2022-11-11 09:31:33 +08:00 · 1613 次点击
    这是一个创建于 524 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目前一些 Web 框架 比如 Gin fiber 等 都支持 HTTP 服务,可以使用静态资源

    func main() {
    	app := fiber.New()
    
    	app.Static("/", "./home.html")
        .....
        }
    

    例如 fiber 示例代码

    将打包编译的二进制文件 与 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)
    

    在此想咨询一下有没有办法解决? 难道讲资源路径硬编码写死吗? 多谢各位

    第 1 条附言  ·  2022-11-11 10:25:57 +08:00

    第一 增加 WorkingDirectory 能解决

    [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 特性 也可以解决

    //go:embed views/*
    var viewsfs embed.FS
    

    多谢各位!!!

    11 条回复    2022-12-30 17:26:11 +08:00
    tairan2006
        1
    tairan2006  
       2022-11-11 09:39:14 +08:00   ❤️ 2
    你没配置 WorkingDirectory…
    anviod
        2
    anviod  
    OP
       2022-11-11 09:51:51 +08:00
    @tairan2006
    ```
    WorkingDirectory=/usr/local/bin/
    ```
    我去加上 试试看
    Great233
        3
    Great233  
       2022-11-11 09:57:36 +08:00   ❤️ 1
    静态资源可以用 embed 包嵌入到 build 之后的 go 程序里面去
    Great233
        4
    Great233  
       2022-11-11 09:57:48 +08:00
    anviod
        5
    anviod  
    OP
       2022-11-11 10:17:21 +08:00
    @Great233 也是个解决办法 可以解决,分发更加容易了
    julyclyde
        6
    julyclyde  
       2022-11-11 11:42:11 +08:00
    像你这么上进的人不多
    很少有考虑在 systemd 环境里运行的一些细节的
    anviod
        7
    anviod  
    OP
       2022-11-11 13:12:03 +08:00
    @julyclyde systemd 管理进程挺好的,省的再开发守护进程了, 虽然 systemd 很复杂,但是用起来还是很好的, 怪自己之前没详细看 systemd 文档.
    yin1999
        8
    yin1999  
       2022-11-12 13:18:07 +08:00 via Android
    可以使用 dynamicUser ,直接 root 有点粗暴
    anviod
        9
    anviod  
    OP
       2022-11-16 14:45:40 +08:00
    @yin1999 嗯 需要创建一个 nologin 用户, 给部分执行权限
    sibowen
        10
    sibowen  
       2022-12-16 11:36:58 +08:00
    程序静态文件目录,可以考虑用配置,或者环境变量来传入吧,这么搞改动起来比较费劲的。
    anviod
        11
    anviod  
    OP
       2022-12-30 17:26:11 +08:00
    @sibowen
    ```
    有没有尝试过多平台发布统一升级, 比如基于 goreleaser 打包成 rpm deb 针对平台 Linux arm/X86_64/AMD64 系列
    可以 例用 rpm/dnf/yum/apt 之类的软件进行升级 只要系统支持 systemd 都能很好的管理.
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5586 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 03:13 · PVG 11:13 · LAX 20:13 · JFK 23:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.