You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.4 KiB
103 lines
2.4 KiB
6 years ago
|
//
|
||
|
// Device server which handle communication to various devices (currently implements RUPTELA)
|
||
|
//
|
||
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"log"
|
||
|
"runtime"
|
||
|
"os"
|
||
|
"io/ioutil"
|
||
|
"strconv"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/ipsusila/opt"
|
||
|
//"../../model"
|
||
|
"../../server"
|
||
|
"../../lumberjack"
|
||
|
|
||
|
_ "github.com/lib/pq"
|
||
|
_ "github.com/mattn/go-sqlite3"
|
||
|
)
|
||
|
|
||
|
var exPath = "";
|
||
|
|
||
|
func startServer(options *opt.Options) {
|
||
|
//---options ---------------------------------------------------------
|
||
|
//start server according to mode
|
||
|
svcNet := &server.DevicePrsServerNet{
|
||
|
Options: options,
|
||
|
}
|
||
|
svcNet.Start()
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
//maximum cpu
|
||
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||
|
|
||
|
ex, err := os.Executable()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
exPath = filepath.Dir(ex) + "/"
|
||
|
confPath := exPath + "config.hjson"
|
||
|
// re-open file
|
||
|
file, err := os.Open(confPath)
|
||
|
if err != nil {
|
||
|
confPath = "config.hjson"
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
//parse configurationf file
|
||
|
cfgFile := flag.String("conf", confPath, "Configuration file")
|
||
|
flag.Parse()
|
||
|
|
||
|
//load options
|
||
|
config, err := opt.FromFile(*cfgFile, opt.FormatAuto)
|
||
|
if err != nil {
|
||
|
log.Printf("Error while loading configuration file %v -> %v\n", *cfgFile, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
//display server ID (displayed in console)
|
||
|
log.Printf("Server ID: %v\n", config.Get("server").GetString("id", "undefined (check configuration)"))
|
||
|
|
||
|
//setup log
|
||
|
//lw, err := utility.NewLogWriter(config)
|
||
|
//if err != nil {
|
||
|
// log.Printf("Error while setup logging: %v\n", err)
|
||
|
// return
|
||
|
//}
|
||
|
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
|
||
|
//log.SetOutput(lw)
|
||
|
logName := config.Get("log").Get("file").GetString("name", "")
|
||
|
log.SetOutput(&lumberjack.Logger{
|
||
|
Filename: logName,
|
||
|
MaxSize: 10, // megabytes
|
||
|
MaxBackups: 3,
|
||
|
MaxAge: 3, //days
|
||
|
Compress: true, // disabled by default
|
||
|
})
|
||
|
//defer lw.Close()
|
||
|
|
||
|
pidFilePath := exPath + "receiver-" + config.Get("server").GetString("id", "undefined (check configuration)") + "-pid"
|
||
|
errRemoveFile := os.Remove(pidFilePath)
|
||
|
if errRemoveFile != nil {
|
||
|
log.Printf("PID file will be created")
|
||
|
}
|
||
|
pidStr := strconv.Itoa(os.Getpid())
|
||
|
log.Printf("PID: %v\n", pidStr)
|
||
|
d1 := []byte(pidStr)
|
||
|
errCreateFile := ioutil.WriteFile(pidFilePath, d1, 0644)
|
||
|
if errCreateFile != nil {
|
||
|
log.Printf("Error while setup write PID file: %v\n", errCreateFile)
|
||
|
} else{
|
||
|
log.Printf("PID file created")
|
||
|
}
|
||
|
|
||
|
//Run application
|
||
|
startServer(config)
|
||
|
}
|