Golang Web应用与系统服务集成问题解决方案

Golang Web应用与系统服务集成问题解决方案 我正在尝试在 Ubuntu 18.04 上使用服务创建一个 Go 语言服务器。问题出在我的应用程序(https://github.com/hmuschett/victoryDash.git)上。

root@tes-server:~/victoryDash# ./webapp
2020/06/16 09:16:15 The server is lisening on 3000 port

当它在终端中执行时,工作正常,但通过服务运行时却不行。 我在 /etc/systemd/system/goweb.service 创建了这个服务:

Description=goweb

[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/root/victoryDash/webapp


[Install]
WantedBy=multi-user.target

我遇到了这个错误:

root@tes-server:/lib/systemd/system# systemctl status gotest
● gotest.service - goweb
   Loaded: loaded (/lib/systemd/system/gotest.service; disabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Tue 2020-06-16 11:21:16 UTC; 2s ago
  Process: 23262 ExecStart=/root/victoryDash/webapp (code=exited, status=2)
 Main PID: 23262 (code=exited, status=2)

Jun 16 11:21:16 tes-server systemd[1]: gotest.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Jun 16 11:21:16 tes-server systemd[1]: gotest.service: Failed with result 'exit-code'.

问题是这类使用 html/template 的项目。我为另一个 REST API 项目创建了服务,它是可以运行的。


更多关于Golang Web应用与系统服务集成问题解决方案的实战教程也可以访问 https://www.itying.com/category-94-b0.html

5 回复

请原谅我的无知,但我该如何实现这两种解决方案中的一种呢?

更多关于Golang Web应用与系统服务集成问题解决方案的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


非常感谢, 我使用了工作目录,它运行得非常完美, 向您致以我所有的爱意,我为此困扰了两天。

通过在调用 http.Dir 时使用绝对路径,或者在服务文件中指定正确的工作目录,例如 WorkingDirectory=/var/lib/myAppData/

您正在一个没有工作目录的上下文中尝试使用相对路径。您需要在代码中使用绝对路径,或者在单元文件中指定一个 WorkingDirectory

// 代码示例:设置工作目录
func main() {
    // 使用绝对路径或设置工作目录
    // ...
}

问题出在服务的工作目录上。当通过systemd服务运行时,工作目录不是应用程序所在的目录,导致模板文件无法正确加载。

解决方案是明确设置工作目录,并确保所有文件路径都使用绝对路径或相对于工作目录的路径。

首先,修改你的systemd服务文件,添加WorkingDirectory

[Unit]
Description=goweb

[Service]
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/root/victoryDash
ExecStart=/root/victoryDash/webapp

[Install]
WantedBy=multi-user.target

其次,在你的Go代码中,确保模板加载使用绝对路径。修改模板加载部分:

package main

import (
    "html/template"
    "log"
    "net/http"
    "path/filepath"
    "os"
)

func main() {
    // 获取当前工作目录
    wd, err := os.Getwd()
    if err != nil {
        log.Fatal(err)
    }

    // 使用绝对路径加载模板
    tmpl := template.Must(template.ParseFiles(
        filepath.Join(wd, "templates", "index.html"),
        filepath.Join(wd, "templates", "header.html"),
        filepath.Join(wd, "templates", "footer.html"),
    ))

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        tmpl.ExecuteTemplate(w, "index.html", nil)
    })

    log.Println("The server is listening on port 3000")
    log.Fatal(http.ListenAndServe(":3000", nil))
}

或者,如果你使用的是相对路径,确保它们相对于正确的工作目录:

func loadTemplates() *template.Template {
    // 使用相对路径,但确保工作目录正确
    return template.Must(template.ParseGlob("templates/*.html"))
}

重新加载systemd配置并重启服务:

sudo systemctl daemon-reload
sudo systemctl restart goweb
sudo systemctl status goweb

如果需要查看详细的日志,可以修改服务文件添加标准输出重定向:

[Service]
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/root/victoryDash
ExecStart=/root/victoryDash/webapp
StandardOutput=journal
StandardError=journal

然后查看日志:

sudo journalctl -u goweb -f

这样就能确保模板文件在systemd服务中正确加载了。

回到顶部