From 1882acf44b2b9d4fc24dd7fbf515cda8aaf4b9bb Mon Sep 17 00:00:00 2001 From: patrick Date: Wed, 18 Mar 2020 02:34:07 +1100 Subject: [PATCH] reading config and write log into absolute path using current directory --- config.go | 6 ++++-- deploy.sh | 12 +++++++++++- main.go | 13 ++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 2835c8e..9be48b4 100644 --- a/config.go +++ b/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 { diff --git a/deploy.sh b/deploy.sh index b126ec6..50c16fe 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,2 +1,12 @@ #!/bin/bash -echo compile program \ No newline at end of file +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" + diff --git a/main.go b/main.go index 1cd0a47..08632a4 100644 --- a/main.go +++ b/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