Example #1
0
func renderInstall(ctx echo.Context, filename string, data map[string]interface{}) error {
	objLog := getLogger(ctx)

	if data == nil {
		data = make(map[string]interface{})
	}

	filename = config.TemplateDir + filename

	requestURI := ctx.Request().URI()
	tpl, err := template.ParseFiles(filename)
	if err != nil {
		objLog.Errorf("解析模板出错(ParseFiles):[%q] %s\n", requestURI, err)
		return err
	}

	buf := new(bytes.Buffer)
	err = tpl.Execute(buf, data)
	if err != nil {
		objLog.Errorf("执行模板出错(Execute):[%q] %s\n", requestURI, err)
		return err
	}

	return ctx.HTML(http.StatusOK, buf.String())
}
Example #2
0
// 后台 query 查询返回结果
func RenderQuery(ctx echo.Context, contentTpl string, data map[string]interface{}) error {
	objLog := logic.GetLogger(ctx)

	contentTpl = "common_query.html," + contentTpl
	contentTpls := strings.Split(contentTpl, ",")
	for i, contentTpl := range contentTpls {
		contentTpls[i] = config.TemplateDir + "admin/" + strings.TrimSpace(contentTpl)
	}

	requestURI := Request(ctx).RequestURI
	tpl, err := template.New("common_query.html").Funcs(funcMap).ParseFiles(contentTpls...)
	if err != nil {
		objLog.Errorf("解析模板出错(ParseFiles):[%q] %s\n", requestURI, err)
		return err
	}

	buf := new(bytes.Buffer)
	err = tpl.Execute(buf, data)
	if err != nil {
		objLog.Errorf("执行模板出错(Execute):[%q] %s\n", requestURI, err)
		return err
	}

	return ctx.HTML(http.StatusOK, buf.String())
}
Example #3
0
func upload(c echo.Context) error {
	// Read form fields
	name := c.FormValue("name")
	email := c.FormValue("email")

	//-----------
	// Read file
	//-----------

	// Source
	file, err := c.FormFile("file")
	if err != nil {
		return err
	}
	src, err := file.Open()
	if err != nil {
		return err
	}
	defer src.Close()

	// Destination
	dst, err := os.Create(file.Filename)
	if err != nil {
		return err
	}
	defer dst.Close()

	// Copy
	if _, err = io.Copy(dst, src); err != nil {
		return err
	}

	return c.HTML(http.StatusOK, fmt.Sprintf("<p>File %s uploaded successfully with fields name=%s and email=%s.</p>", file.Filename, name, email))
}
Example #4
0
// Handler
func hello(c *echo.Context) error {
	var text, temp string

	posts := data.GetPostList(con, 0, 10)

	for _, post := range posts {
		temp = fmt.Sprintf("%v", post)
		text = text + "<br />" + temp
	}

	return c.HTML(http.StatusOK, "<html><body><h1>Blog posts:</h1>"+text+"</body></html>")
}
Example #5
0
// ArticleList 所有文章(分页)
func (ArticleController) ArticleList(ctx echo.Context) error {
	curPage, limit := parsePage(ctx)
	articles, total := logic.DefaultArticle.FindArticleByPage(ctx, nil, curPage, limit)

	if articles == nil {
		return ctx.HTML(http.StatusInternalServerError, "500")
	}

	data := map[string]interface{}{
		"datalist":   articles,
		"total":      total,
		"totalPages": (total + limit - 1) / limit,
		"page":       curPage,
		"limit":      limit,
	}

	return render(ctx, "article/list.html,article/query.html", data)
}
Example #6
0
func requestAddRsvp(c echo.Context) error {
	name := c.Form("name")
	email := c.Form("email")
	res := c.Form("response")
	if name == "" || email == "" {
		c.String(http.StatusBadRequest, "Name and Email must be given. Please go back and try again.")
		return nil
	}

	rsvp := Rsvp{0, name, email, res, time.Now()}
	if err := addRsvp(rsvp); err != nil {
		c.String(http.StatusInternalServerError,
			"Failed to add RSVP at this time. Please try again later")
	}

	c.HTML(http.StatusOK, saveSuccess)
	return nil
}
Example #7
0
// ReadingList 所有晨读(分页)
func (ReadingController) ReadingList(ctx echo.Context) error {
	curPage, limit := parsePage(ctx)

	readings, total := logic.DefaultReading.FindReadingByPage(ctx, nil, curPage, limit)
	if readings == nil {
		return ctx.HTML(http.StatusInternalServerError, "500")
	}

	data := map[string]interface{}{
		"datalist":   readings,
		"total":      total,
		"totalPages": (total + limit - 1) / limit,
		"page":       curPage,
		"limit":      limit,
	}

	return render(ctx, "reading/list.html,reading/query.html", data)
}
Example #8
0
// Query
func (RuleController) Query(ctx echo.Context) error {
	curPage, limit := parsePage(ctx)
	conds := parseConds(ctx, []string{"domain"})

	rules, total := logic.DefaultRule.FindBy(ctx, conds, curPage, limit)

	if rules == nil {
		return ctx.HTML(http.StatusInternalServerError, "500")
	}

	data := map[string]interface{}{
		"datalist":   rules,
		"total":      total,
		"totalPages": (total + limit - 1) / limit,
		"page":       curPage,
		"limit":      limit,
	}
	return render(ctx, "rule/query.html", data)
}
Example #9
0
// ProjectQuery
func (ProjectController) ProjectQuery(ctx echo.Context) error {
	curPage, limit := parsePage(ctx)
	conds := parseConds(ctx, []string{"id", "domain", "title"})

	articles, total := logic.DefaultArticle.FindArticleByPage(ctx, conds, curPage, limit)

	if articles == nil {
		return ctx.HTML(http.StatusInternalServerError, "500")
	}

	data := map[string]interface{}{
		"datalist":   articles,
		"total":      total,
		"totalPages": (total + limit - 1) / limit,
		"page":       curPage,
		"limit":      limit,
	}

	return renderQuery(ctx, "article/query.html", data)
}
Example #10
0
func upload(c echo.Context) error {
	// Read form fields
	name := c.FormValue("name")
	email := c.FormValue("email")

	//------------
	// Read files
	//------------

	// Multipart form
	form, err := c.MultipartForm()
	if err != nil {
		return err
	}
	files := form.File["files"]

	for _, file := range files {
		// Source
		src, err := file.Open()
		if err != nil {
			return err
		}
		defer src.Close()

		// Destination
		dst, err := os.Create(file.Filename)
		if err != nil {
			return err
		}
		defer dst.Close()

		// Copy
		if _, err = io.Copy(dst, src); err != nil {
			return err
		}

	}

	return c.HTML(http.StatusOK, fmt.Sprintf("<p>Uploaded successfully %d files with fields name=%s and email=%s.</p>", len(files), name, email))
}
Example #11
0
func executeTpl(ctx echo.Context, tpl *template.Template, data map[string]interface{}) error {
	objLog := logic.GetLogger(ctx)

	// 如果没有定义css和js模板,则定义之
	if jsTpl := tpl.Lookup("js"); jsTpl == nil {
		tpl.Parse(`{{define "js"}}{{end}}`)
	}
	if jsTpl := tpl.Lookup("css"); jsTpl == nil {
		tpl.Parse(`{{define "css"}}{{end}}`)
	}

	// 当前用户信息
	curUser, ok := ctx.Get("user").(*model.Me)
	if ok {
		data["me"] = curUser
	} else {
		data["me"] = map[string]interface{}{}
	}

	// websocket主机
	if global.OnlineEnv() {
		data["wshost"] = config.ConfigFile.MustValue("global", "domain")
	} else {
		data["wshost"] = global.App.Host + ":" + global.App.Port
	}
	global.App.SetUptime()
	data["app"] = global.App

	data["online_users"] = map[string]int{"online": logic.Book.Len(), "maxonline": logic.MaxOnlineNum()}

	buf := new(bytes.Buffer)
	err := tpl.Execute(buf, data)
	if err != nil {
		objLog.Errorln("excute template error:", err)
		return err
	}

	return ctx.HTML(http.StatusOK, buf.String())
}
Example #12
0
func request(c echo.Context) error {
	req := c.Request().(*standard.Request).Request
	format := "<pre><strong>Request Information</strong>\n\n<code>Protocol: %s\nHost: %s\nRemote Address: %s\nMethod: %s\nPath: %s\n</code></pre>"
	return c.HTML(http.StatusOK, fmt.Sprintf(format, req.Proto, req.Host, req.RemoteAddr, req.Method, req.URL.Path))
}
Example #13
0
func Change(c *echo.Context) error {
	var changes []ChangeRequest
	err := json.Unmarshal([]byte(c.Form("changes")), &changes)
	if err != nil {
		return err
	}
	log.Println("Received changes", changes)
	domain := c.Get("domain").(Domain)

	// serialize map file rewrites
	rewrite_lock.Lock()
	defer func() {
		rewrite_lock.Unlock()
	}()
	// find all existing local addresses, but exclude command delivery
	a := readMapFile(domain.MapFile, nil)
	local := make(map[string]bool)
	for i := 0; i < len(a); i++ {
		if strings.Contains(a[i].Email, "@"+domain.Name) && !strings.ContainsAny(a[i].Target, "@|") {
			local[a[i].Target] = true
		}
	}
	// dedupe and normalize changes
	remap := make(map[string]string)
	for _, change := range changes {
		address, err := mail.ParseAddress(change.Alias)
		if err != nil || !strings.HasSuffix(address.Address, "@"+domain.Name) {
			log.Println("invalid alias:", change.Alias)
			return c.Render(http.StatusBadRequest, "error", struct {
				Error string
			}{"invalid alias: " + change.Alias})
		}
		switch change.Op {
		case "remove":
			remap[address.Address] = ""
		case "add":
			if validate(change.Target, local) {
				remap[address.Address] = change.Target
			} else {
				return c.Render(http.StatusBadRequest, "error", struct {
					Error string
				}{"invalid email address list for " + address.Address + ": " + change.Target})
			}
		default:
			log.Fatal("unexpected change request: ", change)
		}
	}
	// rewrite the map file atomically
	tmp_file := domain.MapFile + ".web.new"
	f, err := os.OpenFile(tmp_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		log.Fatal("could not rewrite map file ", tmp_file, " due to ", err)
	}
	fw := bufio.NewWriter(f)
	readMapFile(domain.MapFile, func(line string, email string) {
		if email == "" {
			fw.WriteString(line + "\n")
		} else {
			dest, ok := remap[email]
			if ok {
				if dest != "" {
					new_line(fw, email, dest)
					// remove any further references
					remap[email] = ""
				}
			} else {
				fw.WriteString(line + "\n")
			}
		}
	})
	for email, dest := range remap {
		if dest != "" {
			new_line(fw, email, dest)
		}
	}
	fw.Flush()
	f.Close()
	err = os.Rename(domain.MapFile, domain.MapFile+".old")
	if err != nil {
		return err
	}
	err = os.Rename(tmp_file, domain.MapFile)
	if err != nil {
		os.Rename(domain.MapFile+".old", domain.MapFile)
		return err
	}
	cmd := exec.Command("postmap", domain.MapFile)
	err = cmd.Run()
	if err != nil {
		os.Rename(domain.MapFile+".old", domain.MapFile)
		return err
	}
	cmd = exec.Command("postfix", "reload")
	err = cmd.Run()
	if err != nil {
		log.Println("postfix reload failed:", err)
	}

	// HTTP 303 is specifically for POST/Redirect/GET
	// see: https://en.wikipedia.org/wiki/Post/Redirect/Get
	c.Response().Header().Set("Location", "/")
	return c.HTML(303, "<script>document.location.href = \"/\";</script>")
}
Example #14
0
// path /set/sqlitefile/:DeviceID
// example /set/sqlitefile/18333
func htmlSqliteFile(c *echo.Context) error {
	return c.HTML(http.StatusOK, fmt.Sprintf(g_html_str, c.Param("DeviceID")))
}