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

时间:2015-10-30 00:07:01

标签: html css go

我有一个用Go编写的简单登录系统。现在我将登录屏幕和内部页面存储为常量。我想使用.html文件而不是常量,因为我现在不能使用CSS样式,我想用Bootstrap设置登录屏幕的样式。

main.go文件:

    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)
}

如何使用indexPage.html和internalPage.html代替indexPage和internalPage常量?

0 个答案:

没有答案