Ejemplo n.º 1
0
Archivo: session.go Proyecto: elago/ela
func (sess *Session) getPath(key string) string {
	length := len(key) - 3
	dir1 := com.SubString(key, 0, 1)
	dir2 := com.SubString(key, 1, 2)
	file := com.SubString(key, 3, length)
	return filepath.Join(sess.path, dir1, dir2, file)
}
Ejemplo n.º 2
0
func checkTimer(cmd *exec.Cmd, comp *Compile, id int) {
	for {
		// if building process hava exit normally, exit timer
		if BuildProcessHaveExit {
			log.Blueln("Building Process Exit Normally.")
			return
		}

		stn := time.Now()
		now := stn.UnixNano()
		// over 10s
		if now-BuildStartTime > 10*1000000000 {
			comp.buildOverTime = true
			log.Warnln("Building Out of Time, Terminated!")
			cmd.Process.Kill()

			systemTag := com.SubString(runtime.GOOS, 0, 5)
			if systemTag == "linux" {
				// ps -ef|grep cc1|grep 5.c|awk '{print $2}'|xargs kill -9
				cleanScript := fmt.Sprintf("ps -ef|grep cc1|grep %d.c|awk '{print $2}'|xargs kill -9", id)
				cleanCmd := exec.Command("sh",
					"-c",
					cleanScript,
				)
				err := cleanCmd.Run()
				if err != nil {
					log.Warnln("clean orphan failed")
				}
			}
			return
		}
	}
}
Ejemplo n.º 3
0
// start the session
func (this *JClient) Start(ip string, port int, password string) error {
	// not login, first time
	this.login = false

	// default # to get the real sep
	this.mark = "#"
	addr := fmt.Sprintf("%s:%d", ip, port)
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		if conn != nil {
			conn.Close()
		}

		log.Warnln("connect judge server failed in port:", port)

		return err
	} else {
		this.conn = conn
		this.connected = true
		content, err := this.read()
		if err != nil {
			return err
		}

		// get seperater mark
		reg := regexp.MustCompile(`/[\d\D]+$`)
		if arr := reg.FindAllString(content, -1); len(arr) > 0 {
			this.mark = com.SubString(arr[0], 1, 1)
		}

		log.Blueln(content)
	}

	// login
	loginRequest := map[string]interface{}{
		"action":   "login",
		"password": password,
	}

	response, err := this.Request(loginRequest)
	if err != nil {
		return err
	}

	result, ok := response["result"].(bool)
	if !result || !ok {
		return errors.New("login failed.")
	}

	sid, ok := response["sid"].(string)
	if ok {
		this.sid = sid
	} else {
		this.sid = ""
	}

	this.login = true

	return err
}
Ejemplo n.º 4
0
// 新建文章
func newPost(params interface{}) string {
	username := params.([]interface{})[1].(string)
	password := params.([]interface{})[2].(string)
	result := login(username, password)

	if result {
		str := com.ReadFile("views/rpcxml/response_new_post.xml")

		title := params.([]interface{})[3].(map[string]interface{})["title"].(string)
		content := params.([]interface{})[3].(map[string]interface{})["description"].(string)
		keywords := ""
		categories := params.([]interface{})[3].(map[string]interface{})["categories"]
		if categories != nil {
			cata := categories.([]interface{})
			for _, v := range cata {
				keywords = fmt.Sprintf(keywords+"%s,", v.(string))
			}
			keywords = strings.TrimSuffix(keywords, ",")
		}

		id, err := AddArticle(title, content, keywords, com.SubString(content, 0, 100), username)

		if err == nil {
			return fmt.Sprintf(str, id)
		} else {
			str := com.ReadFile("views/rpcxml/response_failed.xml")
			return fmt.Sprintf(str, "文章发布失败! 注意标题不能重名")
		}

	} else {
		return com.ReadFile("views/rpcxml/response_login_failed.xml")
	}
}
Ejemplo n.º 5
0
// create session and add session into database
func (this *SessionTab) CreateSession() (string, error) {
	sid := com.Md5(com.CreateGUID())
	sid = com.SubString(sid, 0, 7)
	o := orm.NewOrm()
	var sess SessionTab

	sess.Session = sid
	sess.CreateTime = time.Now()

	_, err := o.Insert(&sess)

	return sid, err
}
func TestGet(t *testing.T) {
	article, _ := services.ArticleById(35120)
	var content = html.UnescapeString(article.Content)
	if len(content) == 0 {
		return
	}
	reader := strings.NewReader(content)
	doc, _ := goquery.NewDocumentFromReader(reader)
	text := doc.Text()
	text = strings.Replace(text, " ", "", -1)
	text = strings.Replace(text, "\n", "", -1)
	text = strings.Replace(text, "\t", "", -1)
	subText := com.SubString(text, 0, 160)
	beego.Error(subText)
}
Ejemplo n.º 7
0
func (a Article) ShortContent(subLength int) string {
	var content = html.UnescapeString(a.Content)
	if len(content) == 0 {
		return ""
	}
	reader := strings.NewReader(content)
	doc, _ := goquery.NewDocumentFromReader(reader)
	text := doc.Text()
	text = strings.Replace(text, " ", "", -1)
	text = strings.Replace(text, "\n", "", -1)
	text = strings.Replace(text, "#", "", -1)
	text = strings.Replace(text, "*", "", -1)
	subText := com.SubString(text, 0, 160)
	return subText
}