duanaozhong0696
2019-03-30 15:49
浏览 604
已采纳

使用config.toml文件部署Golang应用(api)

I have created a small golang API (nrfapi) which include a config.toml file. I would like to deploy the api on other ubuntu VM therefore i build the API using the "GOOS=linux GOARCH=amd64 go build" and then scp the build file to the VM /var/www/go diretory. I also create a unit file (nrf.service) ending in .service within the /etc/systemd/system directory. In the .service file i have the following configuration

[Unit]
Description= instance to serve nrf api
After=network.target

[Service]
User=root
Group=www-data

ExecStart=/var/www/go/nrfapi)

[Install]
WantedBy=multi-user.target

ERROR

Error

● nrf.service - instance to serve nrf api
   Loaded: loaded (/etc/systemd/system/nrf.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2019-03-30 16:44:51 EET; 11s ago
 Main PID: 4066 (code=exited, status=1/FAILURE)

Mar 30 16:44:51 ubuntu systemd[1]: Started instance to serve nrf api.
Mar 30 16:44:51 ubuntu nrfapi[4066]: 2019/03/30 16:44:51 open config.toml: no such file or directory
Mar 30 16:44:51 ubuntu systemd[1]: nrf.service: Main process exited, code=exited, status=1/FAILURE
Mar 30 16:44:51 ubuntu systemd[1]: nrf.service: Unit entered failed state.
Mar 30 16:44:51 ubuntu systemd[1]: nrf.service: Failed with result 'exit-code'.

However, after starting the API using the following commands

sudo systemctl start nrfapi sudo systemctl enable nrfapi

the API is not running. I realize from the error message above that the API need the config.toml file configurations.

My problem now is that i dont know which directory to place the config.toml file so that the golang api can read the configuration parameters from there. Can anyone help me solve this? How do i go about this?

图片转代码服务由CSDN问答提供 功能建议

我创建了一个小型的golang API(nrfapi),其中包含config.toml文件。 我想在其他ubuntu VM上部署api,因此我使用“ GOOS = linux GOARCH = amd64 go build”构建API,然后将构建文件scp到VM / var / www / go目录。 我还在/ etc / systemd / system目录中创建了一个以.service结尾的单位文件(nrf.service)。 在.service文件中,我具有以下配置

  [Unit] 
Description =实例以提供nrf api 
After = network.target 
 
 [Service] 
User  =根
组= WWW的数据
 
ExecStart = /无功/网络/去/ nrfapi)
 
 [安装] 
WantedBy = multi-user.target 
 
   \  n 
 

ERROR

  Error 
 
●nrf.service-服务于nrf api的实例
已加载:已加载(/ etc / systemd / system / nrf  .service;已启用;供应商预设:已启用)
活动:失败,自2019年6月30日星期六开始(结果:退出代码)EET;  11s之前
主PID:4066(代码已退出,状态为1 /失败)
 
3月30日16:44:51 ubuntu systemd [1]:已启动实例来提供nrf api。
3月30日16:44:51  ubuntu nrfapi [4066]:2019/03/30 16:44:51打开config.toml:无此类文件或目录
Mar 30 16:44:51 ubuntu systemd [1]:nrf.service:主进程已退出,代码= 退出,状态= 1 / FAILURE 
Mar 30 16:44:51 ubuntu systemd [1]:nrf.service:设备进入失败状态。
Mar 30 16:44:51 ubuntu systemd [1]:nrf.service:失败 结果为“退出代码”。
 
   
 
 

但是,使用以下命令启动API后

sudo systemctl start nrfapi sudo systemctl启用nrfapi

API未运行。 我从上面的错误消息中意识到,该API需要配置config.toml文件。

我现在的问题是我不知道将config.toml文件放在哪个目录中,因此golang api可以从那里读取配置参数。 谁能帮我解决这个问题? 我该怎么办?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dspwy24648 2019-03-30 15:56
    已采纳

    If you use relative paths to files in your Go scrips then executable will look for them relative to the current working directory. To change working directory in systemd just add WorkingDirectory parameter to the Service section:

    [Service]
    WorkingDirectory=/var/www/go
    

    And place config.toml file inside /var/www/go dir.

    You can also embed static files into Go binary using this library: https://github.com/gobuffalo/packr

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题