We writing an application where its sync data in b/w two databases in the background.
Currently, we have written an application which read connection string details from JSON file and JSON contains the following data
{
“database”: {
“host”: “192.168.0.47”,
“port”: “1433”,
“uid”: “xxxxxx”,
“password”: “xxxxxx”,
“db”:“db_test”
}
}
import (
"fmt"
_"github.com/denisenkom/go-mssqldb"
"log"
"encoding/json"
"os"
"database/sql"
)
type Account struct {
AccountId int
AccountName string
}
type Config struct {
Database *Database `json:"database"`
}
type Database struct {
Host string `json:"host"`
Port string `json:"port"`
Uid string `json:"uid"`
Password string `json:"password"`
Db string `json:"db"`
}
func LoadConfigration() (Config, error) {
var config Config
config.Database=&Database{}
configFile, err := os.Open("Config/Config.json")
if err != nil {
return config, err
}
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&config)
defer configFile.Close()
if err!=nil{
}
return config, err
}
func GetAccountDetails() []Account {
config, err := LoadConfigration()
if err != nil {
}
connectionString := "server=" + config.Database.Host + ";port=" + config.Database.Port + ";user id=" + config.Database.Uid + ";password=" + config.Database.Password + ";database=" + config.Database.Db + ";"
condb, err := sql.Open("mssql", connectionString)
if err != nil {
}
accountList := []Account{}
rows, err := condb.Query("select AccountId,AccountName from Account")
if err != nil {
fmt.Println("Error in fun GetAllAccountConnectionAccountId ")
}
for rows.Next() {
var row Account
err := rows.Scan(&row.AccountId, &row.AccountName)
if err != nil {
log.Fatal(err)
}
accountList = append(accountList, row)
}
defer condb.Close()
return accountList
}
Here Struct account which I am read from db account table, Config struct we are using for loading configuration details from file (Config.json file which is placed in Config folder)
GetAccountDetails is the function which we are calling. It work fine when we run go run file.go
We just created this app as service by sc create “test” - In windos service it is not working. even we don’t know how trace the error.
import (
"github.com/labstack/gommon/log"
"github.com/kardianos/service"
"github.com/jasonlvhit/gocron"
"fmt"
"Project/MasterDataLayer"
)
var logger service.Logger
type program struct{}
func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async.
go p.run()
return nil
}
func (p *program) run() {
scheduler := gocron.NewScheduler()
scheduler.Every(5).Seconds().Do(Synctask)
<- scheduler.Start()
}
func Synctask() {
fmt.Println("Sync is called")
d := MasterDataLayer.GetAccountDetails()
fmt.Println(d)
}
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
func main() {
svcConfig := &service.Config{
Name: "Test",
DisplayName: "Test",
Description: "Test",
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
logger.Error(err)
}
}
``