コード例 #1
0
func (this *DataServer) createNewSession(resp *Response, req *http.Request) (err error) {
	userid := req.FormValue(USER_ID_FORM_KEY)
	gameid := req.FormValue(APPLICATION_ID_FORM_KEY)

	userTbl := this.database.GetTable(USERS_TABLE_NAME)
	if userTbl == nil {
		resp.AddValueToKey("message", "Server Error.")
		err = errors.New("Users table doesn't exist.")
		return
	}
	hasKey, dbErr := userTbl.HasPrimaryKey(userid)
	if !hasKey || dbErr.Exists() {
		resp.AddValueToKey("message", "Failed to create session: Invalid userid.")
		err = errors.New(dbErr.Error())
		return
	}

	appTbl := this.database.GetTable(APPLICATIONS_TABLE_NAME)
	if appTbl == nil {
		resp.AddValueToKey("message", "Server Error.")
		err = errors.New("Application table doesn't exist.")
		return
	}
	hasKey, err = appTbl.HasPrimaryKey(gameid)
	if !hasKey || err != nil {
		resp.AddValueToKey("message", "Failed to create session: Invalid game_id.")
		return
	}

	sessionTbl := this.database.GetTable(SESSIONS_TABLE_NAME)
	if sessionTbl == nil {
		resp.AddValueToKey("message", "Server Error.")
		err = errors.New("Sessions table doesn't exist.")
		return
	}

	sessionid := GenerateGUID(userid, gameid, time.Now().String())[:32]

	colVals := sessionTbl.GetColumnMap()
	colVals.Update("guid", sessionid)
	colVals.Update("application_id", gameid)
	colVals.Update("user_id", userid)
	colVals.Update("start_time", mysqldb.CurrentTime())
	colVals.Update("end_time", mysqldb.CurrentTimePlus(time.Minute*5))
	dbErr = sessionTbl.Insert(colVals)
	if dbErr.Exists() {
		resp.AddValueToKey("message", "Failed to create session: Database Error.")
		err = errors.New(dbErr.Error())
		return
	}
	resp.AddValueToKey("message", fmt.Sprintf("Logging in as %v in game %v, session_id=%v", userid, gameid, sessionid))

	return
}
コード例 #2
0
ファイル: dataserver.go プロジェクト: mehulsbhatt/dataserver
func (this *DataServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	var err error = nil
	eventType := req.FormValue(REQUEST_TYPE_FORM_KEY)
	resp := MakeResponse()
	switch eventType {
	case CREATE_USER_REQ:
		err = this.handleNewUser(resp, req)
	case NEW_SESSION_REQ:
		err = this.handleNewSession(resp, req)
	case REFRESH_SESSION_REQ:
		err = this.handleRefreshSession(resp, req)
	case REGISTER_APPLICATION_REQ:
		err = this.handleRegisterApplication(resp, req)
	case REGISTER_EVENT_REQ:
		err = this.handleRegisterEvent(resp, req)
	default:
		err = this.handleEventCall(eventType, resp, req)
	}

	argsStr := "{"
	if req.Form != nil {
		for k, v := range req.Form {
			argsStr += fmt.Sprintf("%v=%v,", k, v)
		}
		argsStr = strings.TrimSuffix(argsStr, ",")
	}
	argsStr += "}"
	log := fmt.Sprintf("Request: %v\r\n%v", req.URL, argsStr)
	log += "\r\nErrors: "
	if err != nil {
		log += err.Error()
	}
	log += "\r\nResponse: " + resp.String()
	dateStr := mysqldb.CurrentTime()
	fileName := this.LogRoot() + dateStr + "_RequestLog.txt"
	fileName = strings.Replace(fileName, ":", "-", -1)
	err = ioutil.WriteFile(fileName, []byte(log), os.ModePerm)
	if err != nil {
		fmt.Println("Could not write to request log: ", err.Error(), "\r\n", fileName)
	}

	w.Write([]byte(resp.String()))
}
コード例 #3
0
ファイル: dataserver.go プロジェクト: mehulsbhatt/dataserver
func New(jsonDB *mysqldb.JSONDB, config DataServerConfig) (server *DataServer, err error) {
	server = new(DataServer)
	server.config = config
	fmt.Println("Loading default database.")
	conn, myErr := mysqldb.NewConnection(config.MySQLUsername, config.MySQLPassword)
	if myErr.Exists() {
		fmt.Println(myErr.Error())
		return
	}
	server.database = conn.NewDatabaseFromJSON(jsonDB)
	conn.UseDatabase(server.database)

	evtTbl := server.database.GetTable(EVENTS_TABLE_NAME)
	if evtTbl == nil {
		fmt.Println("Events table does not exist.")
		return
	}

	fmt.Println("Loading event tables.")
	dbErr := evtTbl.SelectAll(func(rows *sql.Rows) (stop bool) {
		stop = false
		eventName := ""
		tableName := ""
		definition := ""
		err = rows.Scan(&eventName, &tableName, &definition)
		if err != nil {
			fmt.Println(fmt.Sprintf("Error reading row. Extracted { event_name=%v, table_name=%v, definition=%v }", eventName, tableName, definition))
			return
		}

		jTbl, serr := server.CreateTableFromEventDefinition(definition)
		if serr != nil {
			err = serr
			fmt.Println(err.Error())
			stop = true
			return
		}

		server.database.NewTableFromJSON(jTbl)
		fmt.Println("Added Event Table: ", jTbl.KeyName)
		return
	})

	if dbErr.Exists() {
		fmt.Println("Couldn't select from event table: ", dbErr.Error())
		return
	}

	http.Handle("/", server)

	fmt.Println("Creating Startup Log")
	log := "Start Up\r\n"
	dateStr := mysqldb.CurrentTime()
	fileName := server.LogRoot() + dateStr + "_StartupLog.txt"
	fileName = strings.Replace(fileName, ":", "-", -1)
	err = ioutil.WriteFile(fileName, []byte(log), os.ModePerm)
	if err != nil {
		fmt.Println("Could not write to startup log: ", err.Error(), "\r\n", fileName)
	}

	fmt.Println("Starting Server")
	err = http.ListenAndServe(server.config.PortNumber, nil)
	if err != nil {
		fmt.Println("Failed to start server: ", err.Error())
	}

	fmt.Println("Stopping Server")
	return
}