Ejemplo n.º 1
0
Archivo: fis.go Proyecto: royburns/blog
// fis map
func Fis(key string) template.HTML {
	var text string
	content := loadMap()
	json, _ := com.JsonDecode(content)
	json = json.(map[string]interface{})["res"]
	if fileMap, ok := json.(map[string]interface{}); !ok {
		fmt.Println("map.json id illeage!")
	} else {
		for tmpKey, views := range fileMap {
			uri, ok := views.(map[string]interface{})["uri"].(string)
			if !ok {
				fmt.Println("error in map.json")
			}

			fileType, ok := views.(map[string]interface{})["type"].(string)
			if !ok {
				fmt.Println("error in map.json")
			}

			if tmpKey == key {
				if fileType == "css" {
					text = `<link rel="stylesheet" href="` + uri + `">`
				} else if fileType == "js" {
					text = `<script src="` + uri + `"></script>`
				}
			}
		}
	}

	return template.HTML(text)
}
Ejemplo n.º 2
0
func (this *ProjectListController) DeleteProject() {
	// if not login, permission deny
	user := this.GetSession("username")
	if user == nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "login first please", "refer": nil}
		this.ServeJson()
		return
	}

	paramsBody := string(this.Ctx.Input.RequestBody)
	var params map[string]interface{}
	p, err := com.JsonDecode(paramsBody)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "parse params failed", "refer": "/"}
		this.ServeJson()
		return
	} else {
		params = p.(map[string]interface{})["params"].(map[string]interface{})
	}

	id := int64(params["id"].(float64))

	err = models.DeleteProject(id)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "删除失败", "error": err}
	} else {
		this.Data["json"] = map[string]interface{}{"result": true, "msg": "删除成功"}
	}

	this.ServeJson()
}
Ejemplo n.º 3
0
func Parse(frame string, cli *Client) {
	log.Yellowln(frame)
	json, err := com.JsonDecode(frame)
	if err != nil {
		log.Redln(err)
	} else {
		data := json.(map[string]interface{})

		actonName, ok := data["action"].(string)
		if !ok {
			cli.Write("invalid request, action name is not exist.")
			return
		}

		// 如果不是登录请求,并且用户处于未登录状态,禁止通行
		if actonName != "login" {
			if !cli.login {
				cli.Write("you have not login.")
				return
			}
		}

		RouterMap[actonName].Tcp(data, cli)
	}

}
Ejemplo n.º 4
0
func (this *AdminArticleController) UpdateArticle() {
	// if not login, permission deny
	user := this.GetSession("username")
	if user == nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "login first please", "refer": nil}
		this.ServeJson()
		return
	}

	paramsBody := string(this.Ctx.Input.RequestBody)
	var params map[string]interface{}
	p, err := com.JsonDecode(paramsBody)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "parse params failed", "refer": "/"}
		this.ServeJson()
		return
	} else {
		params = p.(map[string]interface{})["params"].(map[string]interface{})
	}

	id := int64(params["id"].(float64))
	newTitle := params["title"].(string)
	newContent := params["content"].(string)
	newKeywords := params["keywords"].(string)

	if "" == newTitle {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "title can not be empty", "refer": "/"}
		this.ServeJson()
	}

	var art Article

	if nil == err {
		art, err = GetArticle(int(id))
	} else {
		this.Ctx.WriteString("not found")
		return
	}

	art.Title = newTitle
	art.Content = newContent
	art.Keywords = newKeywords

	err = UpdateArticle(id, "", art)

	if nil != err {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "update failed", "refer": "/"}
		this.ServeJson()
	} else {
		this.Data["json"] = map[string]interface{}{"result": true, "msg": "update success", "refer": "/"}
		this.ServeJson()
	}

}
Ejemplo n.º 5
0
func (this *MapJsonController) Get() {
	staticMap := beego.AppConfig.String("static_map")
	content := com.ReadFile(staticMap)
	data, err := com.JsonDecode(content)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "can not get map.json"}
	} else {
		this.Data["json"] = data
	}

	this.ServeJson()
}
Ejemplo n.º 6
0
// run before get
func (this *AdminBaseController) Prepare() {
	// login status
	user := this.GetSession("username")
	if user == nil {
		this.Redirect("/login", 302)
	} else {
		// find user id
		username := user.(string)
		u, err := models.FindUser(username)
		if err != nil {
			log.Warnln(err)
		} else {
			userLog := &models.UserLog{}

			// get ip
			ipPort := this.Ctx.Request.Header.Get("X-Forwarded-For")
			ipPortArr := strings.Split(ipPort, ":")
			ip := ipPortArr[0]

			// get location
			location := ""
			userLogIp, err := userLog.GetUserLogByIp(ip)
			if err == nil {
				locationData, err := com.JsonDecode(userLog.Location)
				if err == nil {
					locationJson := locationData.(map[string]interface{})
					if userLog.IsValidLocation(locationJson) {
						location = userLogIp.Location
					} else {
						location, _ = utils.GetLocation(ip)
					}
				} else {
					location, _ = utils.GetLocation(ip)
				}

			} else {
				location, _ = utils.GetLocation(ip)
			}

			// get user agent
			ua := this.Ctx.Request.UserAgent()

			// save data
			_, err = userLog.AddUserlog(int64(u.Id), ip, ua, location, 0)
			if err != nil {
				log.Warnln(err)
			}
		}
	}

	this.Data["isAdmin"] = true
	this.Data["inDev"] = beego.AppConfig.String("runmode") == "dev"
}
Ejemplo n.º 7
0
func HandleJsonRpc(w http.ResponseWriter, r *http.Request) {
	// get request content
	p := make([]byte, r.ContentLength)
	r.Body.Read(p)

	content := string(p)

	log.Blueln(content)

	json, err := com.JsonDecode(content)

	if err != nil {
		log.Warnln("not json-rpc format")
		return
	}

	data := json.(map[string]interface{})

	// get system password
	password := C.Get("", "password")

	// parse received password
	passwordRecv, ok := data["password"].(string)
	if !ok {
		result, _ := com.JsonEncode(map[string]interface{}{
			"result": false, //bool, login result
			"msg":    "invalid password, password must be string.",
		})
		io.WriteString(w, result)
		return
	}

	// compare password
	if password != passwordRecv {
		result, _ := com.JsonEncode(map[string]interface{}{
			"result": false, //bool, login failed
		})
		io.WriteString(w, result)
		return
	}

	// trigger controller
	ctrl, exists := RouterMap[data["action"].(string)]
	if !exists {
		log.Warnln("not exist")
		return
	}
	ctrl.Http(data, w, r)
}
Ejemplo n.º 8
0
func (this *AdminArticleController) AddArticle() {
	paramsBody := string(this.Ctx.Input.RequestBody)
	var params map[string]interface{}
	p, err := com.JsonDecode(paramsBody)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "parse params failed", "refer": "/"}
		this.ServeJson()
		return
	} else {
		params = p.(map[string]interface{})["params"].(map[string]interface{})
	}

	// log.Pinkln(params)

	title := params["title"].(string)
	content := params["content"].(string)
	keywords := params["keywords"].(string)
	abstract := params["abstract"].(string)

	// if not login, permission deny
	user := this.GetSession("username")
	if user == nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "login first please", "refer": nil}
		this.ServeJson()
		return
	}

	if "" == title {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "title can not be empty", "refer": "/"}
		this.ServeJson()
		return
	}

	username := user.(string)

	id, err := AddArticle(title, content, keywords, abstract, username)
	if nil == err {
		this.Data["json"] = map[string]interface{}{
			"result": true,
			"msg":    "success added, id " + fmt.Sprintf("[%d] ", id),
			"data":   id,
			"refer":  nil,
		}
	} else {
		log.Warnln(err)
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "added failed", "refer": nil}
	}
	this.ServeJson()
}
Ejemplo n.º 9
0
// fis map
func Fis(key string) template.HTML {
	runmode := beego.AppConfig.String("runmode")
	if runmode == "dev" {
		text := ""
		uri := "/static/" + key
		uri = strings.Replace(uri, "scss", "css", -1)
		// uri = strings.Replace(uri, "sass", "css", -1)
		uri = strings.Replace(uri, "coffee", "js", -1)
		uri = strings.Replace(uri, "ts", "js", -1)

		if strings.HasSuffix(uri, "css") {
			text = `<link rel="stylesheet" href="` + uri + `">`
		} else if strings.HasSuffix(uri, "js") {
			text = `<script src="` + uri + `"></script>`
		}
		return template.HTML(text)
	}

	var text string
	content := loadMap()
	json, _ := com.JsonDecode(content)
	json = json.(map[string]interface{})["res"]
	if fileMap, ok := json.(map[string]interface{}); !ok {
		fmt.Println("map.json id illeage!")
	} else {
		for tmpKey, views := range fileMap {
			uri, ok := views.(map[string]interface{})["uri"].(string)
			if !ok {
				fmt.Println("error in map.json")
			}

			fileType, ok := views.(map[string]interface{})["type"].(string)
			if !ok {
				fmt.Println("error in map.json")
			}

			if tmpKey == key {
				if fileType == "css" {
					text = `<link rel="stylesheet" href="` + uri + `">`
				} else if fileType == "js" {
					text = `<script src="` + uri + `"></script>`
				}
			}
		}
	}

	return template.HTML(text)
}
Ejemplo n.º 10
0
// load json config file
func (this *Config) loadJsonConfig() error {
	fi, err := os.Open(this.configFilePath)
	if err != nil {
		panic(err)
	}
	defer fi.Close()
	fd, err := ioutil.ReadAll(fi)
	configString := string(fd)

	// kick out the comment
	regFilter := regexp.MustCompile(`//[\d\D][^\r]*\r`)
	configString = regFilter.ReplaceAllString(configString, "")
	this.jsonConfigData, err = com.JsonDecode(configString)
	if err != nil {
		log.Fatalln("Read config file failed. please check `conf/config.json`.")
	}

	return err
}
Ejemplo n.º 11
0
func (this *AdminArticleController) DelArticle() {
	// if not login, permission deny
	user := this.GetSession("username")
	if user == nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "login first please", "refer": nil}
		this.ServeJson()
		return
	}

	paramsBody := string(this.Ctx.Input.RequestBody)
	var params map[string]interface{}
	p, err := com.JsonDecode(paramsBody)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "parse params failed", "refer": "/"}
		this.ServeJson()
		return
	} else {
		params = p.(map[string]interface{})["params"].(map[string]interface{})
	}

	id := int64(params["id"].(float64))
	// title := this.Ctx.Input.Param(":title")

	if id < 0 {
		id = 0
	}

	num, err := DeleteArticle(id, "")

	if nil != err {
		log.Fatal(err)
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "delete faild", "refer": nil}
		this.ServeJson()
	} else if 0 == num {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "articles dose not exist", "refer": nil}
		this.ServeJson()
	} else {
		this.Data["json"] = map[string]interface{}{"result": true, "msg": fmt.Sprintf("[%d]", num) + " articles deleted", "refer": nil}
		this.ServeJson()
	}
}
Ejemplo n.º 12
0
func (this *ProjectListController) UpdateProject() {
	// if not login, permission deny
	user := this.GetSession("username")
	if user == nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "login first please", "refer": nil}
		this.ServeJson()
		return
	}

	//	log.Pinkln(user)

	paramsBody := string(this.Ctx.Input.RequestBody)
	var params map[string]interface{}
	p, err := com.JsonDecode(paramsBody)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "parse params failed", "refer": "/"}
		this.ServeJson()
		return
	} else {
		params = p.(map[string]interface{})["params"].(map[string]interface{})
	}

	log.Pinkln(params)

	id := int64(params["id"].(float64))
	name := params["name"].(string)
	icon := params["icon"].(string)
	description := params["description"].(string)

	log.Pinkln(id)

	err = models.UpdateProject(id, name, icon, description)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "修改失败", "error": err}
	} else {
		this.Data["json"] = map[string]interface{}{"result": true, "msg": "修改成功"}
	}

	this.ServeJson()
}
Ejemplo n.º 13
0
func (this *ProjectListController) AddProject() {

	paramsBody := string(this.Ctx.Input.RequestBody)
	var params map[string]interface{}
	p, err := com.JsonDecode(paramsBody)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "parse params failed", "refer": "/"}
		this.ServeJson()
		return
	} else {
		params = p.(map[string]interface{})["params"].(map[string]interface{})
	}

	name := params["name"].(string)
	icon := params["icon"].(string)
	description := params["description"].(string)
	createTime := time.Now()

	user := this.GetSession("username")
	if user == nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "login first please", "refer": nil}
		this.ServeJson()
		return
	}

	author := user.(string)

	id, err := models.AddProject(name, icon, author, description, createTime)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "添加失败", "error": err}
	} else {
		if id <= 0 {
			this.Data["json"] = map[string]interface{}{"result": false, "msg": "添加失败"}
		}
	}

	this.Data["json"] = map[string]interface{}{"result": true, "msg": "添加成功", "id": id}
	this.ServeJson()
}
Ejemplo n.º 14
0
// send request
func (this *JClient) Request(msg map[string]interface{}) (map[string]interface{}, error) {
	msgStr, err := com.JsonEncode(msg)

	if err != nil {
		return nil, err
	}

	if this.conn == nil {
		log.Warnln("Connection Not Exist")
		return nil, errors.New("Connection Not Exist")
	}

	_, err = this.conn.Write([]byte(msgStr + this.mark))
	if err != nil {
		log.Warnln("[Write Error]", err)
		this.conn.Close()
		return nil, err
	}

	content, err := this.read()

	if err != nil {
		return nil, err
	}

	// kick sep char
	reg := regexp.MustCompile(this.mark)
	content = reg.ReplaceAllString(content, "")

	if this.debug {
		log.Bluef("[judger/send:%s]\n%s\n", time.Now(), msgStr)
		log.Warnf("[judger/recv:%s]\n%s\n", time.Now(), content)
	}

	resp, err := com.JsonDecode(content)

	return resp.(map[string]interface{}), err
}