func createNewJSONConfig(filename string) { newConfig := gabs.New() newConfig.SetP(false, "meta.admin") newConfig.SetP("https://api.civo.com", "meta.url") newConfig.SetP("1", "meta.version") ioutil.WriteFile(filename, []byte(newConfig.StringIndent("", " ")), 0600) }
func getConfig(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json := gabs.New() json.Set(readonly == "true", "readonly") w.Write([]byte(json.String())) }
func (a *RawAdapter) Stream(logstream chan *router.Message) { msg := gabs.New() for message := range logstream { buf := new(bytes.Buffer) err := a.tmpl.Execute(buf, message) if err != nil { log.Debug("raw:", err) return } if cn := utils.M1[message.Container.Name]; cn != "" { //logmsg := utils.SendMessage(cn, buf.String(), message.Container) //_, err = connection.Write([]byte(logmsg)) utils.SendMessage(cn, buf.String(), message.Container.ID, msg) _, err = connection.Write(append([]byte(msg.String()), '\n')) if err != nil { log.Error("raw:", err, reflect.TypeOf(a.conn).String()) /*if reflect.TypeOf(a.conn).String() != "*net.TCPConn"{ return }*/ } } } }
func createJSONContainer(jsonString string) *gabs.Container { if jsonString == "" { return gabs.New() } container, err := gabs.ParseJSON([]byte(jsonString)) if err != nil { Fatalf("Failed to parse piped JSON string: %s", err) } return container }
func persistenCounter() { if len(counter) > 0 { json := gabs.New() counterlock.Lock() for k, v := range counter { json.Set(v, k) } counterlock.Unlock() err := ioutil.WriteFile(countFile, json.Bytes(), 0x644) if err != nil { log.Error("persisten counter failed: ", err) } } }
func UpdateJsonFile(file string, paths []string, value interface{}, ui packer.Ui, force bool) error { // TODO: add a flag to create a file if it does not exist content, err := ioutil.ReadFile(file) if err != nil { return err } // Parse file and create new JSON object if invalid detected and overwrite json, err := gabs.ParseJSON(content) if err != nil { ui.Say("Invalid JSON Template") if force { json = gabs.New() } else { return err } } if newValueMap, success := value.(map[string]string); success { for _, path := range paths { oldValue, _ := json.Path(path).Data().(string) ui.Say(fmt.Sprintf("Updating %s from %s to %s in %s...", path, oldValue, newValueMap, file)) for key, value := range newValueMap { ui.Say(fmt.Sprintf("[DEBUG] Setting %s to %s at %s", key, value, path)) // TODO: this is pretty brittle here since if the type of value of node // is not changeable. This might lead to bad render of JSON where previously // erroreous values are prevailed over consecutive runs. json.Set("", path, key) json.Set(value, path, key) ui.Say(fmt.Sprintf("[DEBUG] JSON Dump %s", json.String())) } } } else { newValue, _ := value.(string) for _, path := range paths { oldValue, _ := json.Path(path).Data().(string) ui.Say(fmt.Sprintf("Updating %s from %s to %s in %s...", path, oldValue, newValue, file)) ui.Say(fmt.Sprintf("%s", path)) json.SetP(newValue, path) } } // pretty printed JSON dump err = ioutil.WriteFile(file, []byte(json.StringIndent("", " ")), 0644) return err }
func (m *Maropost) GetContactsByList(list string, page string) (*gabs.Container, error) { // Make our request response, err := MakeRequest(m.Account+"/lists/"+list+"/contacts.json?page="+page+"&auth_token="+m.AuthToken, "GET", nil, false) if err != nil { return nil, err } var object interface{} jsonDecoder := json.NewDecoder(response.Body) if err = jsonDecoder.Decode(&object); err != nil { return nil, err } jsonObject := gabs.New() jsonObject.SetP(object, "array") defer response.Body.Close() return jsonObject.S("array"), nil }
func (a *RawAdapter) Stream(logstream chan *router.Message) { msg := gabs.New() for message := range logstream { buf := new(bytes.Buffer) err := a.tmpl.Execute(buf, message) if err != nil { log.Error("raw:", err) return } if cn := utils.M1[message.Container.Name]; cn != "" { utils.SendMessage(cn, buf.String(), message.Container.ID, msg) //logmsg := utils.SendMessage(cn, buf.String(), message.Container) msg := &kafka.ProducerMessage{Topic: topic, Value: kafka.StringEncoder(msg.String())} partition, offset, err := a.producer.SendMessage(msg) _, _, _ = partition, offset, err } } }
func saveDoc(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json := gabs.New() if readonly == "false" { r.ParseForm() path := r.Form["path"][0] markdown := r.Form["doc"][0] err := ioutil.WriteFile(path, []byte(markdown), 0644) if err != nil { json.Set(false, "success") json.Set(err.Error(), "error") } else { json.Set(true, "success") } } else { json.Set(false, "success") json.Set("moc is in readonly mode", "error") } w.Write([]byte(json.String())) }
// map json from player to logicserver func mapPlayerToGlobal(db gorm.DB) *gabs.Container { // TODO: IMPORTANT THIS RELIES ON GAME TICK LOGIC var player_actions []PlayerAction // fetch all active entries to memory // ordering by game tick so only recent ones are taken db.Where("Stale = ?", false).Order("GameTick").Find(&player_actions) // update all the entries as stale in a batch // TODO: ideally the above two should be in a transaction db.Table("player_action").Where("Stale = ?", false).Updates(PlayerAction{Stale: true}) jsonObj := gabs.New() // ref: https://github.com/Jeffail/gabs#generating-json for _, player_state := range player_actions { jsonObj.Set(player_state.Actions, player_state.Username, "Actions") jsonObj.Set(player_state.GameTick, player_state.Username, "GameTick") } return jsonObj }
func (this Elasticsearch) statsToAtollReport(data string) (string, error) { statsParsed, err := gabs.ParseJSON([]byte(data)) if err != nil { return "", err } atollReport := gabs.New() atollReport.SetP("elasticsearch", "id") atollReport.SetP("Elasticsearch", "name") // Main state state := "error" esStatus := statsParsed.S("status").Data().(string) if esStatus == "green" { state = "ok" } else if esStatus == "yellow" { state = "warn" } atollReport.SetP(state, "report.status.state") // Num Nodes atollReport.SetP(statsParsed.S("nodes").S("count").S("total").Data().(float64), "report.stats.numberOfNodes.value") // Core caches atollReport.SetP(statsParsed.S("indices").S("fielddata").S("memory_size_in_bytes").Data().(float64)/1024/1024, "report.stats.fieldDataCacheSize.value") atollReport.SetP([2]string{"megabytes", "memory"}, "report.stats.fieldDataCacheSize.classes") atollReport.SetP(statsParsed.S("indices").S("filter_cache").S("memory_size_in_bytes").Data().(float64)/1024/1024, "report.stats.filterCacheSize.value") atollReport.SetP([2]string{"megabytes", "memory"}, "report.stats.filterCacheSize.classes") atollReport.SetP(statsParsed.S("indices").S("id_cache").S("memory_size_in_bytes").Data().(float64)/1024/1024, "report.stats.idCacheSize.value") atollReport.SetP([2]string{"megabytes", "memory"}, "report.stats.idCacheSize.classes") // JVM stats atollReport.SetP(statsParsed.S("nodes").S("jvm").S("mem").S("heap_used_in_bytes").Data().(float64)/1024/1024, "report.stats.jvmHeapUsage.value") atollReport.SetP([2]string{"megabytes", "memory"}, "report.stats.jvmHeapUsage.classes") atollReport.SetP(statsParsed.S("nodes").S("jvm").S("mem").S("heap_max_in_bytes").Data().(float64)/1024/1024, "report.stats.jvmHeapSize.value") atollReport.SetP([2]string{"megabytes", "memory"}, "report.stats.jvmHeapSize.classes") return atollReport.String(), nil }
func (this RabbitMQ) statsToAtollReport(data string) (string, error) { statsParsed, err := gabs.ParseJSON([]byte(data)) if err != nil { return "", err } atollReport := gabs.New() atollReport.SetP("rabbitmq", "id") atollReport.SetP("RabbitMQ", "name") atollReport.ArrayP("report.items") var children, _ = statsParsed.S("result").Children() for i, child := range children { atollReport.ArrayAppendP(map[string]interface{}{}, "report.items") atollItem := atollReport.S("report").S("items").Index(i) atollItem.SetP(child.S("name").Data().(string), "name") atollItem.SetP([1]string{"queue"}, "classes") // Queue stats atollItem.SetP([2]string{"memory", "bytes"}, "stats.memoryUsage.classes") atollItem.SetP(child.S("memory").Data().(float64), "stats.memoryUsage.value") atollItem.SetP([2]string{"messages", "count"}, "stats.numMessages.classes") atollItem.SetP(child.S("messages").Data().(float64), "stats.numMessages.value") publishRate := child.S("message_stats").S("publish_details").S("rate") if publishRate.String() != "{}" { atollItem.SetP([3]string{"throughput", "per-second", "average"}, "stats.publishRate.classes") atollItem.SetP(publishRate.Data().(float64), "stats.publishRate.value") } deliverRate := child.S("message_stats").S("deliver_get_details").S("rate") if deliverRate.String() != "{}" { atollItem.SetP([3]string{"throughput", "per-second", "average"}, "stats.deliverRate.classes") atollItem.SetP(deliverRate.Data().(float64), "stats.deliverRate.value") } } return atollReport.String(), nil }
func getJSONSchemaRecursively(obj interface{}) *gabs.Container { schema := gabs.New() switch obj.(type) { case []map[string]interface{}: objAsArray := obj.([]map[string]interface{}) if len(objAsArray) > 0 { schema.Set("array", "type") schema.Set(getJSONSchemaRecursively(objAsArray[0]).Data(), "items") } case []interface{}: objAsArray := obj.([]interface{}) if len(objAsArray) > 0 { schema.Set("array", "type") schema.Set(getJSONSchemaRecursively(objAsArray[0]).Data(), "items") } case map[string]interface{}: objAsMap := obj.(map[string]interface{}) schema.Set("object", "type") var keys []string for key, v := range objAsMap { schema.SetP(getJSONSchemaRecursively(v).Data(), "properties."+key) keys = append(keys, key) } sort.Strings(keys) schema.Set(false, "additionalProperties") schema.Set(keys, "required") case string: schema.Set("string", "type") case bool: schema.Set("boolean", "type") default: schema.Set("number", "type") } return schema }
func (this MongoDB) Monitor() (string, error) { session, err := mgo.Dial(fmt.Sprintf("%s:%d", this.host, this.port)) if err != nil { return "", err } var serverStatus serverStatusResult session.Run("serverStatus", &serverStatus) var replSetStatus replsSetStatusResult session.DB("admin").Run("replSetGetStatus", &replSetStatus) var currentOp currentOpResult query := bson.M{} coll := session.DB("admin").C("$cmd.sys.inprog") coll.Find(query).One(¤tOp) atollReport := gabs.New() atollReport.SetP("mongodb", "id") atollReport.SetP("MongoDB", "name") atollReport.ArrayP("report.items") // Main state state := "ok" if len(replSetStatus.Members) > 0 { for i, member := range replSetStatus.Members { atollReport.ArrayAppendP(map[string]interface{}{}, "report.items") atollItem := atollReport.S("report").S("items").Index(i) if member.Health != 1 { state = "error" } atollItem.SetP(member.Name, "name") atollItem.SetP([1]string{"replicaset-member"}, "classes") atollItem.SetP(strings.ToLower(member.StateStr), "role") atollItem.SetP(member.State, "stats.state.value") atollItem.SetP(member.Health, "stats.health.value") atollItem.SetP([2]string{"seconds", "duration"}, "stats.uptime.classes") atollItem.SetP(member.Uptime, "stats.uptime.value") atollItem.SetP([2]string{"miliseconds", "duration"}, "stats.ping.classes") atollItem.SetP(member.PingMs, "stats.ping.value") } } atollReport.SetP(state, "report.status.state") atollReport.SetP(serverStatus.Connections.Available, "report.stats.availableConnections.value") atollReport.SetP(serverStatus.Connections.Current, "report.stats.currentConnections.value") atollReport.SetP(len(currentOp.Inprog), "report.stats.numCurrentOps.value") var numRunningFor2Seconds uint64 = 0 var numRunningFor10Seconds uint64 = 0 var numRunningFor60Seconds uint64 = 0 var totalRunningSeconds uint64 = 0 if len(currentOp.Inprog) > 0 { for i, _ := range currentOp.Inprog { op := currentOp.Inprog[i] if op.SecsRunning >= 2 { numRunningFor2Seconds++ } if op.SecsRunning >= 10 { numRunningFor10Seconds++ } if op.SecsRunning >= 60 { numRunningFor60Seconds++ } totalRunningSeconds = totalRunningSeconds + op.SecsRunning log.Printf("Op %v, %d", op, op.SecsRunning) } } atollReport.SetP(totalRunningSeconds, "report.stats.currentOpsTotalRunningTime.value") atollReport.SetP([2]string{"duration", "seconds"}, "report.stats.currentOpsTotalRunningTime.classes") atollReport.SetP(numRunningFor2Seconds, "report.stats.numCurrentOps2Seconds.value") atollReport.SetP(numRunningFor10Seconds, "report.stats.numCurrentOps10Seconds.value") atollReport.SetP(numRunningFor60Seconds, "report.stats.numCurrentOps60Seconds.value") return atollReport.String(), nil }
func SockServer(w http.ResponseWriter, r *http.Request) { conn, err := websocket.Upgrade(w, r, nil, 1024, 1024) if _, ok := err.(websocket.HandshakeError); ok { http.Error(w, "Not a websocket handshake", 400) return } db, err := gorm.Open("postgres", "user=Roman password=Roman dbname=DB1 sslmode=disable") if err != nil { log.Fatalf("error: %v\n", err) } //db.CreateTable(&MyList{}) //создаём табл var allToDo []MyList Soc_Id := atomic.AddUint64(Max_SocId, 1) sockCli := ClientConn{conn, Soc_Id} ActiveClients[Soc_Id] = sockCli db.DB() db.Order("ID").Find(&allToDo) jsonObj := gabs.New() jsonObj.Set("All_List", "Command") jsonObj.Set(&allToDo, "Data") SendMessageToSock(Soc_Id, jsonObj.String()) for { _, msg, err := conn.ReadMessage() if err != nil { delete(ActiveClients, Soc_Id) return } var Command string var ok bool jsonParsed, err_ := gabs.ParseJSON(msg) if err_ != nil { Davay_Do_Svidaniya(Soc_Id, conn) return } Command, ok = jsonParsed.Path("Command").Data().(string) if ok == false { Davay_Do_Svidaniya(Soc_Id, conn) return } switch Command { case "Todo_Add": var Name string Name, ok = jsonParsed.Path("Data.Name").Data().(string) if ok == false { Davay_Do_Svidaniya(Soc_Id, conn) return } var todo MyList todo.Name = Name todo.IsTrue = false db.Create(&todo) jsonObj := gabs.New() jsonObj.Set("New_Todo", "Command") jsonObj.Set(&todo.ID, "Data", "ID") jsonObj.Set(Name, "Data", "Name") SendMessageToAll(jsonObj.String()) case "Todo_Delete": var ID float64 ID, ok = jsonParsed.Path("Data.ID").Data().(float64) if ok == false { Davay_Do_Svidaniya(Soc_Id, conn) return } db.Where("ID = ?", ID).Delete(&MyList{}) var todo MyList todo.IsTrue = false jsonObj := gabs.New() jsonObj.Set("Todo_Deleted", "Command") jsonObj.Set(ID, "Data", "ID") SendMessageToAll(jsonObj.String()) case "Todo_Check": var ID float64 var Value bool var value_ string ID, ok = jsonParsed.Path("Data.ID").Data().(float64) if ok == false { Davay_Do_Svidaniya(Soc_Id, conn) return } value_, ok = jsonParsed.Path("Data.IsTrue").Data().(string) Value = ParseBool(value_) if ok == false { Davay_Do_Svidaniya(Soc_Id, conn) return } var my_list MyList db.First(&my_list, uint64(ID)) my_list.IsTrue = Value db.Save(&my_list) jsonObj := gabs.New() jsonObj.Set("Todo_Checked", "Command") jsonObj.Set(ID, "Data", "ID") jsonObj.Set(value_, "Data", "IsTrue") SendMessageToAll(jsonObj.String()) case "RemoveAll": var todo MyList db.Delete(&todo) db.Order("ID").Find(&allToDo) jsonObj := gabs.New() jsonObj.Set("All_List", "Command") jsonObj.Set(&allToDo, "Data") SendMessageToAll(jsonObj.String()) } } conn.Close() }