Exemplo n.º 1
0
func readAccessRoles() {
	rows, err := App.db.Query("select RID,Name from roles")
	lib.Errcheck(err)
	defer rows.Close()
	for rows.Next() {
		var r Role
		lib.Errcheck(rows.Scan(&r.RID, &r.Name))
		App.Roles = append(App.Roles, r)
	}
	lib.Errcheck(rows.Err())
}
Exemplo n.º 2
0
func main() {
	rand.Seed(time.Now().UnixNano()) // We need this for generating random passwords, probably other random things too.

	//=============================
	//  Hardcoded defaults...
	//=============================
	Phonebook.ReqMem = make(chan int)
	Phonebook.ReqMemAck = make(chan int)
	Phonebook.ReqSessionMem = make(chan int)
	Phonebook.ReqSessionMemAck = make(chan int)
	Phonebook.ReqCountersMem = make(chan int)
	Phonebook.ReqCountersMemAck = make(chan int)
	Phonebook.Roles = make([]Role, 0)
	Phonebook.SessionTimeout = 120    // minutes
	Phonebook.SessionCleanupTime = 10 // minutes

	//==============================================
	// There may be some command line overrides...
	//==============================================
	readCommandLineArgs()

	//==============================================
	// Now open the logfile and the database...
	//==============================================
	var err error
	Phonebook.LogFile, err = os.OpenFile("Phonebook.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	lib.Errcheck(err)
	defer Phonebook.LogFile.Close()
	log.SetOutput(Phonebook.LogFile)
	ulog("*** Accord PHONEBOOK ***\n")

	//==============================================
	// And the database...
	//==============================================
	// dbopenparms := fmt.Sprintf("%s:@/%s?charset=utf8&parseTime=True", Phonebook.DBUser, Phonebook.DBName)
	lib.ReadConfig()
	dbopenparms := lib.GetSQLOpenString(Phonebook.DBUser, Phonebook.DBName)
	db, err := sql.Open("mysql", dbopenparms)
	lib.Errcheck(err)
	defer db.Close()
	err = db.Ping()
	if nil != err {
		ulog("db.Ping: Error = %v\n", err)
		s := fmt.Sprintf("Could not establish database connection to db: %s, dbuser: %s\n", Phonebook.DBName, Phonebook.DBUser)
		ulog(s)
		fmt.Println(s)
		os.Exit(2)
	}
	ulog("MySQL database opened with \"%s\"\n", dbopenparms)
	Phonebook.db = db
	buildPreparedStatements()

	//==============================================
	// Load some of the database info...
	//==============================================
	loadMaps()
	readAccessRoles()
	if Phonebook.Debug {
		dumpAccessRoles()
	}

	//==============================================
	// On with the show...
	//==============================================
	initUI()
	go Dispatcher()
	go CounterDispatcher()
	go UpdateCounters()
	go SessionDispatcher()
	go SessionCleanup()

	initHTTP()
	sessionInit()

	ulog("Phonebook initiating HTTP service on port %d\n", Phonebook.Port)
	err = http.ListenAndServe(fmt.Sprintf(":%d", Phonebook.Port), nil)
	if nil != err {
		fmt.Printf("*** Error on http.ListenAndServe: %v\n", err)
		ulog("*** Error on http.ListenAndServe: %v\n", err)
		os.Exit(1)
	}
}