Exemplo n.º 1
0
func init() {
	// beego.Router("/", &controllers.MainController{})
	beego.Include(&controllers.MainController{})
	beego.AddFuncMap("ToString", func(b []byte) string {
		return goutils.ToString(b)
	})
}
Exemplo n.º 2
0
func markdownCBQiniu(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	rawContent := req.Form.Get("rawContent")
	// fmt.Println(rawContent)
	fmt.Print(",")
	out := make([]byte, 0, 100)
	in := goutils.ToByte(rawContent)
	RPC_Client = rpcsv.RPCClient(rpc_tcp_server)
	err := rpcsv.Markdown(RPC_Client, &in, &out)
	if goutils.CheckErr(err) {
		rw.Write(goutils.ToByte(err.Error()))
		return
	}
	qiniuWriter.Reset()
	qiniuWriter.Write(goutils.ToByte("CallbackFunc(`"))
	data := make(map[string]interface{})
	data["MDContent"] = template.HTML(goutils.ToString(out))
	err = rpcsv.Theme.Execute(qiniuWriter, data)
	if goutils.CheckErr(err) {
		rw.Write(goutils.ToByte(err.Error()))
	}
	qiniuWriter.Write(goutils.ToByte("`)"))
	rw.Write(qiniuWriter.Bytes())
	writeCrossDomainHeaders(rw, req)
}
Exemplo n.º 3
0
func markdown(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	rawContent := req.Form.Get("rawContent")
	// fmt.Println(req.RemoteAddr, req.Referer())
	fmt.Print(".")
	// fmt.Println(rawContent)
	out := make([]byte, 0, 100)
	in := goutils.ToByte(rawContent)
	times := 0
	connect()
retry:
	times++
	err := rpcsv.Markdown(RPC_Client, &in, &out)
	if goutils.CheckErr(err) {
		connect()
		if times < 6 {
			goto retry
		}
		rw.Write(goutils.ToByte(err.Error()))
		return
	}

	data := make(map[string]interface{})
	data["MDContent"] = template.HTML(goutils.ToString(out))
	err = rpcsv.Theme.Execute(rw, data)
	if goutils.CheckErr(err) {
		rw.Write(goutils.ToByte(err.Error()))
	}
	// if len(out) <= 0 {
	// 	rw.Write(goutils.ToByte("{response:nil}"))
	// 	return
	// }
	// writeCrossDomainHeaders(rw, req)
	// rw.Write(out)
}
Exemplo n.º 4
0
// in: Linux/index.md
// out: ./MDFs
func modifiedMD(file_in, dir_out string) {
	finfo, err := os.Stat(file_in)
	if goutils.CheckErr(err) {
		return
	}
	filename := finfo.Name()
	dir := filepath.Dir(file_in)
	fs := strings.Split(filename, ".")
	in := goutils.ReadFile(file_in)
	out := make([]byte, 1)
	err = rpcsv.Markdown(rpc_client, &in, &out)
	if goutils.CheckErr(err) {
		return
	}
	// fmt.Println(goutils.ToString(out))
	target := fmt.Sprintf("%s.html", filepath.Join(dir_out, dir, fs[0]))

	goutils.Mkdir(fmt.Sprintf("%s", filepath.Join(dir_out, dir)))
	outfile, _ := os.OpenFile(fmt.Sprintf("%s.html", filepath.Join(dir_out, dir, fs[0])), os.O_CREATE|os.O_WRONLY, 0666)
	defer outfile.Close()
	dt := make(map[string]interface{})
	dt["MDContent"] = template.HTML(goutils.ToString(out))
	tpl.Execute(outfile, dt)
	fmt.Println(file_in, " ==> ", target)
}
Exemplo n.º 5
0
func renderFile(filename string, redo bool) bool {
	_, err := os.Lstat(filename + ".html")
	if !redo && nil == err {
		return false
	}
	if nil == err {
		err = os.Remove(filename + ".html")
		goutils.CheckErr(err)
	}
	input := readFile(filename)
	if nil == input {
		return false
	}
	// unsafe := blackfriday.MarkdownCommon(input)
	// html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
	html := md.Markdown(input)

	data := make(map[string]interface{})
	data["MDContent"] = goutils.ToString(html)

	data["Title"] = getName(filename)
	of, err := os.OpenFile( /*targetDir+string(os.PathSeparator)+*/ filename+".html", os.O_CREATE|os.O_RDWR, 0666)
	defer of.Close()
	if goutils.CheckErr(err) {
		return false
	}
	err = theme.Execute(of, data)
	if goutils.CheckErr(err) {
		return false
	}
	return true
}
Exemplo n.º 6
0
func mailService(rw http.ResponseWriter, req *http.Request) {
	content := "Something changed:\n\n"
	reqb, err := ioutil.ReadAll(req.Body)
	if goutils.CheckErr(err) {
		content += err.Error() + "\n"
	}
	content += goutils.ToString(reqb)
	fmt.Println(req.RequestURI)
	fmt.Println(content)
	mail.SendMail(content)
}
Exemplo n.º 7
0
func TestRPC(t *testing.T) {
	return
	t.Parallel()
	c := RPCClientWithCodec(rpc_tcp_server)
	defer c.Close()
	in := []byte("#   [Hi](http://mdblog.daoapp.io/)")
	out := make([]byte, 10)
	Markdown(c, &in, &out)
	fmt.Println(goutils.ToString(out))
	// defer lis.Close()
}
Exemplo n.º 8
0
Arquivo: modle.go Projeto: shaalx/gooj
func renderOjModle(args map[string]interface{}, w io.Writer) error {
	b, err := ioutil.ReadFile("oj.tpl")
	if goutils.CheckErr(err) {
		return err
	}
	s := goutils.ToString(b)
	tpl, err := template.New("oj.tpl").Parse(s)
	if goutils.CheckErr(err) {
		return err
	}
	return tpl.Execute(w, args)
}
Exemplo n.º 9
0
func TestWallBack(t *testing.T) {
	// return
	t.Parallel()
	c := RPCClientWithCodec(rpc_tcp_server)
	defer c.Close()
	job := Job{Name: "mdblog", Result: goutils.ToByte("mdblog-result")}
	out := make([]byte, 10)
	// in := make([]byte, 1)
	err := c.Call("RPC.WallBack", &job, &out)
	goutils.CheckErr(err)
	fmt.Println("out:", goutils.ToString(out))
}
Exemplo n.º 10
0
func readLine(filename string) string {
	file, err := os.Open(filename)
	defer file.Close()
	if checkerr(err) {
		return ""
	}
	b := make([]byte, 300)
	n, err := file.Read(b)
	if checkerr(err) {
		return ""
	}
	return goutils.ToString(b[:n])
}
Exemplo n.º 11
0
// @router /upload/* [post,put]
func (c *MainController) Upload() {
	rw := c.Ctx.ResponseWriter
	req := c.Ctx.Request
	// if req.Method == "GET" {
	// 	return
	// }
	b, err := ioutil.ReadAll(req.Body)
	if checkerr(err) {
		rw.Write([]byte(err.Error()))
	}
	filename := c.Ctx.Input.Param(":splat")
	createFile(filename, goutils.ToString(b))
}
Exemplo n.º 12
0
func readFile(filename string) string {
	file, err := os.Open(volumn + filename)
	defer file.Close()
	if err != nil {
		return err.Error()
	} else {
		b, err := ioutil.ReadAll(file)
		if err != nil {
			return err.Error()
		}
		return goutils.ToString(b)
	}
}
Exemplo n.º 13
0
func TestAJob(t *testing.T) {
	t.Parallel()
	// return
	c := RPCClientWithCodec(rpc_tcp_server)
	defer c.Close()
	in := Job{Name: "docker", Target: "https://www.google.com/search?q=docker+ci&oq=docker+ci&aqs=chrome..69i57j69i60l4.1517j0j4&sourceid=chrome&ie=UTF-8"}
	// in := Job{Name: "docker", Target: "https://www.google.com/aclk?sa=l&ai=Cls_gs3pMV8qDGcuq9gWdmIDoBprG9PcJgp_wutwC7MbsFAgAEAFgibPGhPQToAG8ppTsA8gBAaoEJk_QWc_UQjNHKe39e-t7guvDvFwTnmO55c8m1AmJHWa40wgOFtPpgAes2esTkAcBqAemvhvYBwE&sig=AOD64_1CJPrrkYMy7tE2O8DDMb1KMt8GuQ&clui=0&ved=0ahUKEwiagI7aq4LNAhXhnaYKHX5UDeQQ0QwIEg&adurl=https://circleci.com/integrations/docker/"}
	// in := Job{Name: "jenkins2", Target: "https://jenkins.io/solutions/docker/"}
	// in := Job{Name: "jenkins-docker", Target: "https://youtu.be/ppCzDDpcqRk"}
	out := make([]byte, 10)
	err := c.Call("RPC.AJob", &in, &out)
	goutils.CheckErr(err)
	fmt.Println(in)
	fmt.Println(goutils.ToString(out))
}
Exemplo n.º 14
0
Arquivo: main.go Projeto: everfore/exc
func Cond(path string, info os.FileInfo) (ifExec bool, skip error) {
	if strings.EqualFold(info.Name(), ".git") {
		return false, filepath.SkipDir
	}
	if !info.IsDir() {
		if strings.HasSuffix(info.Name(), ".go") || strings.HasSuffix(info.Name(), "Dockerfile") {
			if bs, cnt := ContentContains(path, "shaalx"); cnt {
				fmt.Println(info.Name())
				rst_cnt := strings.Replace(goutils.ToString(bs), "shaalx", "toukii", -1)
				OverWrite(path, goutils.ToByte(rst_cnt))
				return true, nil
			}
		}
	}
	return false, nil
}
Exemplo n.º 15
0
func init() {
	Spor = string(os.PathSeparator)
	// flag
	flag.BoolVar(&redo, "r", false, "-r [true]")
	flag.StringVar(&filename, "f", "README.md", "-f readme.md")
	flag.StringVar(&targetDir, "d", "./", "-d ./static")

	// theme
	thm_b := readFile(thm_file)
	if nil == thm_b {
		thm_b = goutils.ToByte(thm_s)
	}
	var err error
	theme, err = template.New("theme.thm").Parse(goutils.ToString(thm_b))
	if goutils.CheckErr(err) {
		panic(err.Error())
	}
}
Exemplo n.º 16
0
Arquivo: main.go Projeto: everfore/exc
func ContentContains(path string, stuff string) ([]byte, bool) {
	abs_path, err := filepath.Abs(path)
	if goutils.CheckErr(err) {
		return nil, false
	}
	fr, err := os.OpenFile(abs_path, os.O_RDONLY, 0644)
	defer fr.Close()
	if goutils.CheckErr(err) {
		return nil, false
	}
	bs, err := ioutil.ReadAll(fr)
	if goutils.CheckErr(err) {
		return nil, false
	}
	if strings.Contains(goutils.ToString(bs), stuff) {
		return bs, true
	}
	return nil, false
}
Exemplo n.º 17
0
func markdownCB(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	rawContent := req.Form.Get("rawContent")
	fmt.Println(req.RemoteAddr, req.Referer())
	// fmt.Println(rawContent)
	out := make([]byte, 0, 100)
	in := goutils.ToByte(rawContent)
	RPC_Client = rpcsv.RPCClient(rpc_tcp_server)
	err := rpcsv.Markdown(RPC_Client, &in, &out)
	if goutils.CheckErr(err) {
		rw.Write(goutils.ToByte(err.Error()))
		return
	}
	if len(out) <= 0 {
		rw.Write(goutils.ToByte("{response:nil}"))
		return
	}
	writeCrossDomainHeaders(rw, req)
	fmt.Println(req.RemoteAddr)
	CallbackFunc := fmt.Sprintf("CallbackFunc(%v);", string(Json(goutils.ToString(out))))
	fmt.Fprint(rw, CallbackFunc)
}
Exemplo n.º 18
0
// @router /job [post]
func (c *MainController) PJob() {
	req := c.Ctx.Request
	req.ParseForm()
	title := req.Form.Get("name")
	content := req.Form.Get("target")
	fmt.Printf("%s,%s", title, content)
	job := rpcsv.Job{Name: title, Target: content}
	b := make([]byte, 10)
	_, RPC_Client = checkNilThenReLoop(RPC_Client, false)
	err := RPC_Client.Call("RPC.AJob", &job, &b)
	if goutils.CheckErr(err) {
		c.Data["json"] = err
		_, RPC_Client = checkNilThenReLoop(RPC_Client, true)
	} else {
		// fmt.Println(goutils.ToString(b))
		// job.Result = b
		job.TargetContent = goutils.ToString(b)
		job.Target = fmt.Sprintf(`<a href="http://upload.daoapp.io/loadfile/%s/%s.html" target="blank">%s</a><br>`, forbidden_dir, job.Name, job.Target)
		c.Data["json"] = job //goutils.ToString(b)
	}
	c.ServeJSON(true)
}
Exemplo n.º 19
0
func AnalyseBytes(input_bs []byte) *Result {
	// fmt.Println("=============================")
	result := Result{}
	input := goutils.ToString(input_bs)
	result.Content = input
	splt := strings.Split(input, "\n")
	length := len(splt)
	if length >= 5 {
		if splt[length-3] == "PASS" {
			result.Pass = AnalysePass(splt)
			result.State = "PASS"
		}
	}
	// fmt.Println(splt)
	// fmt.Println(splt[length-2])
	if strings.Contains(splt[length-2], "FAIL") {
		result.Fail = AnalyseFail(splt)
		result.State = "FAIL"
	}
	// fmt.Println(result)
	// fmt.Println("=============================")
	return &result
}
Exemplo n.º 20
0
func searchDir(dir string) {
	file, err := os.Open(dir)
	if exc.Checkerr(err) {
		return
	}
	subdirs, err := file.Readdir(-1)
	if exc.Checkerr(err) {
		return
	}
	for _, it := range subdirs {
		if strings.EqualFold(it.Name(), ".git") {
			continue
		}
		if it.IsDir() {
			/*go*/ searchDir(filepath.Join(dir, it.Name()))
		}
		if strings.HasSuffix(it.Name(), ".md") {
			absName := filepath.Join(dir, it.Name())
			cmd := fmt.Sprintf("md -r -f %s", absName)
			b, err := command.Cd(dir).Reset(cmd).Do()

			subName := absName[len(wd):]
			target := filepath.Join(wd, "MDFs", subName[:len(subName)-3])
			target_dir := filepath.Dir(target)
			cmd = fmt.Sprintf("mkdir -p %s", target_dir)
			command.Reset(cmd).Execute()
			cmd = fmt.Sprintf("mv -u %s.html %s.html", absName, target)
			command.Reset(cmd).Execute()
			if nil != err {
				installInfo <- NewInfo(dir, false, goutils.ToString(b))
			} else {
				installInfo <- NewInfo(dir, true, "")
			}
			command.Cd("..")
		}
	}
}
Exemplo n.º 21
0
// @router /test [post]
func (c *PuzzleController) Test() {
	submit_LOCKER.Lock()
	defer submit_LOCKER.Unlock()
	var model model_util.Model
	model.Id = "1"
	model.Desc = c.GetString("descr")
	model.Title = c.GetString("title")
	model.FuncName = c.GetString("func_name")
	model.ArgsType = c.GetString("args_type")
	model.Content = c.GetString("content")
	model.RetsType = c.GetString("rets_type")
	model.TestCases = c.GetString("test_cases")
	/*if len(model.FuncName) <= 0 {
		c.Abort("403")
		return
	}*/
	fmt.Printf("%#v\n", model)
	path_ := strings.Split(c.Ctx.Request.RemoteAddr, ":")[1]
	if len(path_) < 1 {
		path_ = "goojt"
	}
	beego.Debug("path_:", path_)
	err := model_util.GenerateOjModle(path_, &model)
	if goutils.CheckErr(err) {
		/*c.Ctx.ResponseWriter.Write(goutils.ToByte(err.Error()))
		return*/
	}
	cmd := exc.NewCMD("go test -v")
	ret, err := cmd.Wd().Cd(path_).Debug().Do()
	if goutils.CheckErr(err) {
		/*c.Ctx.ResponseWriter.Write(goutils.ToByte(err.Error()))
		return*/
	}
	c.Ctx.ResponseWriter.Write(ret)
	fmt.Println(goutils.ToString(ret))
	go cmd.Reset(fmt.Sprintf("rm -rf %s", path_)).Cd(defaultpath).ExecuteAfter(1)
}
Exemplo n.º 22
0
// @router /oj/:id:int [post]
func (c *ListController) OJ() {
	var id int
	c.Ctx.Input.Bind(&id, ":id")
	if ojCheck(id) <= 0 {
		c.Data["Content"] = template.HTML("<h3>题目还未审核,如有问题,请联系管理员。</h3>")
		c.TplName = "sorry.html"
		return
	}
	fid := c.GetString("fid")
	path_ := c.GetString("rid")
	content := c.GetString("puzzle")
	if strings.Contains(content, `"os`) {
		c.Ctx.ResponseWriter.Write(goutils.ToByte("呵呵"))
		return
	}
	beego.Debug(content, path_, fid)

	// inser into db
	slt := models.Solution{}
	cur := c.SessionController.CurUser()
	if cur != nil {
		slt.User = &models.User{Id: cur.Id} //models.UserById(cur.Id)
	} else {
		slt.User = models.UserByName("Anonymous")
	}
	slt.Content = content
	// slt.Create = *goutils.LocNow("Asia/Shanghai")
	// fmt.Println(slt)
	ffid, _ := c.GetInt("fid")
	slt.Puzzle = &models.Puzzle{Id: ffid}
	n, dberr := models.ORM.Insert(&slt)
	if goutils.CheckErr(dberr) {

	}
	// insert into db

	submit_LOCKER.Lock()
	defer submit_LOCKER.Unlock()
	cmd := exc.NewCMD("go test -v").Cd(defaultpath)

	var puzzle models.Puzzle
	models.ORM.QueryTable((*models.Puzzle)(nil)).Filter("Id", id).One(&puzzle)
	m := Puzzle2Model(&puzzle)

	m.Content = content
	beego.Info(m)
	err := model_util.GenerateOjModle(path_, m)
	goutils.CheckErr(err)
	submitID++
	test_result, err := cmd.Wd().Cd(path_).Debug().Do()
	analyse_result := utils.Analyse(goutils.ToString(test_result))
	fmt.Println(analyse_result)
	goutils.CheckErr(err)
	if n > 0 {
		result := models.AnalyseResultParse(analyse_result)
		m, err := models.ORM.Insert(result)
		if !goutils.CheckErr(err) {
			result.Id = int(m)
		}
		slt.Result = result
		slt.Id = int(n)
		go func() {
			models.ORM.Update(&slt)
		}()
	}

	go cmd.Reset(fmt.Sprintf("rm -rf %s", path_)).Cd(defaultpath).Execute()

	c.Ctx.ResponseWriter.Write(goutils.ToByte(analyse_result.String()))
}