瀏覽代碼

reading config and write log into absolute path using current directory

master
patrick 5 年之前
父節點
當前提交
1882acf44b
共有 3 個檔案被更改,包括 27 行新增4 行删除
  1. +4
    -2
      config.go
  2. +11
    -1
      deploy.sh
  3. +12
    -1
      main.go

+ 4
- 2
config.go 查看文件

@@ -35,12 +35,14 @@ var Config AppConfig

func readConfig() error {
log.Println("Read configration for production ")
return readConfigFile("config.json")
workingdir := getWorkingDir()
return readConfigFile(workingdir + "/config.json")
}

func readConfigForTest() error {
log.Println("Read configration for Test ")
return readConfigFile("config.test.json")
workingdir := getWorkingDir()
return readConfigFile(workingdir + "/config.test.json")
}

func readConfigFile(file string) error {

+ 11
- 1
deploy.sh 查看文件

@@ -1,2 +1,12 @@
#!/bin/bash
echo compile program
echo "compile program to rpn "
go build -o rpn

scp rpn rpn@hk-01.biukop.com:~/go/
scp config.json rpn@hk-01.biukop.com:~/go/
rsync -a --exclude _pgbackup PG rpn@hk-01.biukop.com:~/go/
rsync -a form rpn@hk-01.biukop.com:~/go/

echo 'stop remote rpn and start new one'
ssh root@hk-01.biukop.com "/usr/sbin/service rpn stop; /usr/sbin/service rpn start"


+ 12
- 1
main.go 查看文件

@@ -4,6 +4,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"text/template"
"time"
)
@@ -11,8 +12,10 @@ import (
func main() {
db.h = nil //make sure it's in proper state.

workingdir := getWorkingDir()

//setup log file
filename := "logs/rpn.superforex." + time.Now().Format("2006-01-02-15-04-06") + ".log"
filename := workingdir + "/logs/rpn.superforex." + time.Now().Format("2006-01-02-15-04-06") + ".log"
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Error opening file: %v", err)
@@ -39,6 +42,14 @@ func main() {
//program never reach here.
}

func getWorkingDir() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
return dir
}

func errPage(w http.ResponseWriter, code int, msg string) {
var data struct {
ErrorMessage string

Loading…
取消
儲存