예제 #1
0
func SqlQueryServer(ws *websocket.Conn) {

	dbConn, err := sql.Open("postgres", "postgres://"+config.All().Database.Username+
		":"+config.All().Database.Password+
		"@"+config.All().Database.Address+
		"/"+config.All().Database.Name+
		"?sslmode=require")
	if err != nil {
		logging.Error("data-websock", "DB error: ", err.Error())
		return
	}
	defer dbConn.Close()

	for {
		var data struct {
			Type  string
			Query string
		}
		err := websocket.JSON.Receive(ws, &data)
		if err != nil {
			logging.Warning("data-websock", "Recieve error: ", err.Error())
			return
		}

		//process message
		switch data.Type {
		case "query":
			logging.Info("data-websock", "Got Query: ", data.Query)
			doQuery(data.Query, ws, dbConn)
		}
	}
}
예제 #2
0
파일: main.go 프로젝트: twitchyliquid64/CNC
func Initialise() {
	logging.Info("data", "Initialise()")
	trackingSetup()

	dbConn, err := sql.Open("postgres", "postgres://"+config.All().Database.Username+
		":"+config.All().Database.Password+
		"@"+config.All().Database.Address+
		"/"+config.All().Database.Name+
		"?sslmode=require")
	if err != nil {
		logging.Error("data", "Error opening DB connection")
		logging.Error("data", "Error: ", err)
		tracking_notifyFault(err)
	}

	DB, err = gorm.Open("postgres", dbConn)
	DB.LogMode(true)

	if err != nil {
		logging.Error("data", "Error launching DB engine")
		logging.Error("data", "Error: ", err)
	}

	checkStructures(DB)

	//make sure that objects in the config BaseObjects are
	//existing, creating them if nessesary.
	for _, usr := range config.All().BaseObjects.AdminUsers {

		tmp := user.User{}

		DB.Where(&user.User{Username: usr.Username}).First(&tmp)

		if tmp.Username != usr.Username { //if the user was not found
			logging.Info("data", "Creating admin user: "******"data", "Could not create admin. ", err)
			}

			DB.Create(&user.User{Username: usr.Username,
				Permissions: []user.Permission{user.Permission{Name: user.PERM_ADMIN}},
				AuthMethods: []user.AuthenticationMethod{authMethod},
			})
		}
	}

	logging.Info("data", "Initialisation finished.")
}