From 086db15aff7e9c860911591ebb159893e135818b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Sun, 10 May 2020 12:43:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=89=93=E5=8C=85=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E8=BF=9B=E4=BA=8C=E8=BF=9B=E5=88=B6=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.go | 10 ++++------ tmpl/{index.html => index.go} | 14 +++++++++++++- tmpl/{login.html => login.go} | 14 +++++++++++++- 3 files changed, 30 insertions(+), 8 deletions(-) rename tmpl/{index.html => index.go} (91%) rename tmpl/{login.html => login.go} (94%) diff --git a/server.go b/server.go index d9fd7bf..42f9e50 100644 --- a/server.go +++ b/server.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/taoshihan1991/imaptool/tmpl" "github.com/taoshihan1991/imaptool/tools" "html/template" "log" @@ -116,9 +117,8 @@ func list(w http.ResponseWriter, r *http.Request) { render.Mails = mails }() - t, _ := template.ParseFiles("./tmpl/index.html") wg.Wait() - t.Execute(w, render) + tmpl.RenderList(w,render) } //登陆界面 @@ -131,8 +131,7 @@ func login(w http.ResponseWriter, r *http.Request) { res := tools.CheckEmailPassword(server, email, password) if !res { errStr = "连接或验证失败" - t, _ := template.ParseFiles("./tmpl/login.html") - t.Execute(w, errStr) + tmpl.RenderLogin(w,errStr) } else { auth := fmt.Sprintf("%s|%s|%s", server, email, password) cookie := http.Cookie{ @@ -143,8 +142,7 @@ func login(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", 302) } } else { - t, _ := template.ParseFiles("./tmpl/login.html") - t.Execute(w, nil) + tmpl.RenderLogin(w,errStr) } } diff --git a/tmpl/index.html b/tmpl/index.go similarity index 91% rename from tmpl/index.html rename to tmpl/index.go index 0fae8d7..0b9a8d2 100644 --- a/tmpl/index.html +++ b/tmpl/index.go @@ -1,3 +1,12 @@ +package tmpl + +import ( + "html/template" + "net/http" +) + +func RenderList(w http.ResponseWriter,render interface{}){ + const html = ` @@ -54,4 +63,7 @@ - +` + t, _ := template.New("list").Parse(html) + t.Execute(w, render) +} diff --git a/tmpl/login.html b/tmpl/login.go similarity index 94% rename from tmpl/login.html rename to tmpl/login.go index 2d8bb76..f81269f 100644 --- a/tmpl/login.html +++ b/tmpl/login.go @@ -1,3 +1,12 @@ +package tmpl + +import ( + "html/template" + "net/http" +) + +func RenderLogin(w http.ResponseWriter,render interface{}){ + const html = ` @@ -87,4 +96,7 @@ - +` + t, _ := template.New("list").Parse(html) + t.Execute(w, render) +}