/** * @function name: func apiParser(rw http.ResponseWriter, req *http.Request) * @description: This function parses the method of API request. * @related issues: OWL-085 * @param: rw http.ResponseWriter * @param: req *http.Request * @return: void * @author: Don Hsieh * @since: 09/11/2015 * @last modified: 09/23/2015 * @called by: http.HandleFunc("/api", apiParser) * in func main() */ func apiParser(rw http.ResponseWriter, req *http.Request) { buf := new(bytes.Buffer) buf.ReadFrom(req.Body) s := buf.String() // Does a complete copy of the bytes in the buffer. log.Println("s =", s) json, err := simplejson.NewJson(buf.Bytes()) if err != nil { log.Println(err.Error()) } f, err := os.OpenFile("falcon_api.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatal("error opening file: %v", err) } defer f.Close() log.SetOutput(f) var nodes = make(map[string]interface{}) nodes, _ = json.Map() method := nodes["method"] log.Println(method) delete(nodes, "method") delete(nodes, "auth") if method == "host.create" { hostCreate(nodes, rw) } else if method == "host.delete" { hostDelete(nodes, rw) } else if method == "host.update" { hostUpdate(nodes, rw) } else if method == "host.exists" { // hostExist(params) } else if method == "hostgroup.create" { hostgroupCreate(nodes, rw) } else if method == "hostgroup.delete" { hostgroupDelete(nodes, rw) } else if method == "hostgroup.update" { hostgroupUpdate(nodes, rw) } else if method == "hostgroup.exists" { // hostgroupExist(params) } else if method == "template.create" { templateCreate(nodes, rw) } else if method == "template.delete" { templateDelete(nodes, rw) } else if method == "template.update" { templateUpdate(nodes, rw) } else if method == "template.exists" { // templateExist(params) } }
/** * @function name: func apiParser(rw http.ResponseWriter, req *http.Request) * @description: This function parses the method of API request. * @related issues: OWL-257, OWL-254, OWL-085 * @param: rw http.ResponseWriter * @param: req *http.Request * @return: void * @author: Don Hsieh * @since: 09/11/2015 * @last modified: 01/01/2016 * @called by: http.HandleFunc("/api", apiParser) * in func main() */ func apiParser(rw http.ResponseWriter, req *http.Request) { log.Println("func apiParser(rw http.ResponseWriter, req *http.Request)") buf := new(bytes.Buffer) buf.ReadFrom(req.Body) log.Println(buf.Len()) if buf.Len() == 0 { apiAlert(rw, req) } else { s := buf.String() // Does a complete copy of the bytes in the buffer. log.Println("s =", s) json, err := simplejson.NewJson(buf.Bytes()) if err != nil { log.Println(err.Error()) } var nodes = make(map[string]interface{}) nodes, _ = json.Map() method := nodes["method"] log.Println(method) if method == "host.create" { hostCreate(nodes) } else if method == "host.delete" { hostDelete(nodes) } else if method == "host.get" { hostGet(nodes) } else if method == "host.update" { hostUpdate(nodes) } else if method == "hostgroup.create" { hostgroupCreate(nodes) } else if method == "hostgroup.delete" { hostgroupDelete(nodes) } else if method == "hostgroup.get" { hostgroupGet(nodes) } else if method == "hostgroup.update" { hostgroupUpdate(nodes) } else if method == "template.create" { templateCreate(nodes) } else if method == "template.delete" { templateDelete(nodes) } else if method == "template.get" { templateGet(nodes) } else if method == "template.update" { templateUpdate(nodes) } setResponse(rw, nodes) } }
func main() { _, err := os.Stat("log") if err != nil { os.Mkdir("log", os.ModePerm) } if _, err := os.Stat("log/master.log"); err == nil { os.Remove("log/master.log") } log.WriteToFile("log/master.log") defer func() { if e := recover(); e != nil { log.LogFatalf(e) time.Sleep(1 * time.Second) } }() flag.Parse() if *config == "" { flag.PrintDefaults() return } m := master.NewMaster() apps := *config + "/app.json" appdata, e := ioutil.ReadFile(apps) if e != nil { panic(e) } var app master.App if err := json.Unmarshal(appdata, &app); err != nil { panic(err) } m.AppDef = app servers := *config + "/servers.json" json, err := readConfig(servers) if err != nil { panic(err) } def, err1 := json.Map() if err1 != nil { panic(err1) } for key := range def { if key == "master" { mst := json.Get(key) if host, ok := mst.CheckGet("host"); ok { v, err := host.String() if err != nil { panic(err) } m.Host = v } else { m.Host = "127.0.0.1" } if localip, ok := mst.CheckGet("localip"); ok { v, err := localip.String() if err != nil { panic(err) } m.LocalIP = v } else { m.LocalIP = "127.0.0.1" } if outerip, ok := mst.CheckGet("outerip"); ok { v, err := outerip.String() if err != nil { panic(err) } m.OuterIP = v } else { m.OuterIP = "127.0.0.1" } if port, ok := mst.CheckGet("port"); ok { v, err := port.Int() if err != nil { panic(err) } m.Port = v } else { m.Port = 5100 } if agent, ok := mst.CheckGet("agent"); ok { v, err := agent.Bool() if err != nil { panic(err) } m.Agent = v } if agentid, ok := mst.CheckGet("agentid"); ok { v, err := agentid.String() if err != nil { panic(err) } m.AgentId = v } if cp, ok := mst.CheckGet("consoleport"); ok { v, err := cp.Int() if err != nil { panic(err) } m.ConsolePort = v } if tpl, ok := mst.CheckGet("consoleroot"); ok { v, err := tpl.String() if err != nil { panic(err) } log.LogMessage("path:", v) m.Template = v } if waits, ok := mst.CheckGet("waitagents"); ok { v, err := waits.Int() if err != nil { panic(err) } m.WaitAgents = v } if startuid, ok := mst.CheckGet("startuid"); ok { v, err := startuid.Int() if err != nil { panic(err) } master.AppUid = int32(v) } if nobalance, ok := mst.CheckGet("nobalance"); ok { v, err := nobalance.Bool() if err != nil { panic(err) } m.NoBalance = v } continue } m.AppArgs[key], _ = json.Get(key).MarshalJSON() } exitChan := make(chan int) signalChan := make(chan os.Signal, 1) go func() { <-signalChan exitChan <- 1 }() signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) m.Start() m.Wait(exitChan) m.Exit() log.CloseLogger() }
func setStrategyTags(rw http.ResponseWriter, req *http.Request) { errors := []string{} var result = make(map[string]interface{}) result["error"] = errors buf := new(bytes.Buffer) buf.ReadFrom(req.Body) json, err := simplejson.NewJson(buf.Bytes()) if err != nil { setError(err.Error(), result) } var nodes = make(map[string]interface{}) nodes, _ = json.Map() strategyID := "" tagName := "" tagValue := "" if value, ok := nodes["strategyID"]; ok { strategyID = value.(string) delete(nodes, "strategyID") } if value, ok := nodes["tagName"]; ok { tagName = value.(string) delete(nodes, "tagName") } if value, ok := nodes["tagValue"]; ok { tagValue = value.(string) delete(nodes, "tagValue") } if len(strategyID) > 0 && len(tagName) > 0 && len(tagValue) > 0 { strategyIDint, err := strconv.Atoi(strategyID) if err != nil { setError(err.Error(), result) } else { o := orm.NewOrm() var tag Tag sqlcmd := "SELECT * FROM falcon_portal.tags WHERE strategy_id=?" err = o.Raw(sqlcmd, strategyIDint).QueryRow(&tag) if err == orm.ErrNoRows { sql := "INSERT INTO tags(strategy_id, name, value, create_at) VALUES(?, ?, ?, ?)" res, err := o.Raw(sql, strategyIDint, tagName, tagValue, getNow()).Exec() if err != nil { setError(err.Error(), result) } else { num, _ := res.RowsAffected() log.Println("mysql row affected nums =", num) result["strategyID"] = strategyID result["action"] = "create" } } else if err != nil { setError(err.Error(), result) } else { sql := "UPDATE tags SET name = ?, value = ? WHERE strategy_id = ?" res, err := o.Raw(sql, tagName, tagValue, strategyIDint).Exec() if err != nil { setError(err.Error(), result) } else { num, _ := res.RowsAffected() log.Println("mysql row affected nums =", num) result["strategyID"] = strategyID result["action"] = "update" } } } } else { setError("Input value errors.", result) } nodes["result"] = result setResponse(rw, nodes) }