douchun1948 2015-10-30 00:07
浏览 42

显示带有处理程序响应的HTML模板(Golang)

I have a simple login system written in Go. Right now I have the login screen and internal page stored as constants. I would like to use .html files instead of the constants, as I cannot use CSS styling right now and I would like to style the login screen with Bootstrap.

main.go file right now:

    package main

    import (
        "fmt"
        "github.com/gorilla/mux"
        "github.com/gorilla/securecookie"
        "net/http"
    )

    // cookie handling

    var cookieHandler = securecookie.New(
        securecookie.GenerateRandomKey(64),
        securecookie.GenerateRandomKey(32))

    func getUserName(request *http.Request) (userName string) {
        if cookie, err := request.Cookie("session"); err == nil {
            cookieValue := make(map[string]string)
            if err = cookieHandler.Decode("session", cookie.Value, &cookieValue); err == nil {
                userName = cookieValue["name"]
            }
        }
        return userName
    }

    func setSession(userName string, response http.ResponseWriter) {
        value := map[string]string{
            "name": userName,
        }
        if encoded, err := cookieHandler.Encode("session", value); err == nil {
            cookie := &http.Cookie{
                Name:  "session",
                Value: encoded,
                Path:  "/",
            }
            http.SetCookie(response, cookie)
        }
    }

    func clearSession(response http.ResponseWriter) {
        cookie := &http.Cookie{
            Name:   "session",
            Value:  "",
            Path:   "/",
            MaxAge: -1,
        }
        http.SetCookie(response, cookie)
    }

    // login handler

    func loginHandler(response http.ResponseWriter, request *http.Request) {
        name := request.FormValue("name")
        pass := request.FormValue("password")
        redirectTarget := "/"
        if name != "" && pass != "" {
            // .. check credentials ..
            setSession(name, response)
            redirectTarget = "/internal"
        }
        http.Redirect(response, request, redirectTarget, 302)
    }

    // logout handler

    func logoutHandler(response http.ResponseWriter, request *http.Request) {
        clearSession(response)
        http.Redirect(response, request, "/", 302)
    }

    // index page

    const indexPage = `
    <h1>Login</h1>
    <form method="post" action="/login">
        <label for="name">User name</label>
        <input type="text" id="name" name="name">
        <label for="password">Password</label>
        <input type="password" id="password" name="password">
        <button type="submit">Login</button>
    </form>
    `

    func indexPageHandler(response http.ResponseWriter, request *http.Request) {
        fmt.Fprintf(response, indexPage)
    }

    // internal page

    const internalPage = `
    <h1>Internal</h1>
    <hr>
    <small>User: %s</small>
    <form method="post" action="/logout">
        <

button type="submit">Logout</button>
</form>
`

func internalPageHandler(response http.ResponseWriter, request *http.Request) {
    userName := getUserName(request)
    if userName != "" {
        fmt.Fprintf(response, internalPage, userName)
    } else {
        http.Redirect(response, request, "/", 302)
    }
}

// server main method

var router = mux.NewRouter()

func main() {

    router.HandleFunc("/", indexPageHandler)
    router.HandleFunc("/internal", internalPageHandler)

    router.HandleFunc("/login", loginHandler).Methods("POST")
    router.HandleFunc("/logout", logoutHandler).Methods("POST")

    http.Handle("/", router)
    http.ListenAndServe(":8000", nil)
}

How can I use indexPage.html and internalPage.html instead of the indexPage and internalPage constants?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测