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

使用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?

  • 写回答

1条回答 默认 最新

  • 普通网友 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 依据报错在原代吗格式的基础上解决问题
  • ¥15 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图
  • ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA
  • ¥25 LT码在高斯信道下的误码率仿真
  • ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
  • ¥15 yolov5目标检测并显示目标出现的时间或视频帧
  • ¥15 电视版的优酷可以设置电影连续播放吗?