Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示带有处理程序响应的html模板(Golang)_Html_Css_Go - Fatal编程技术网

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

显示带有处理程序响应的html模板(Golang),html,css,go,Html,Css,Go,我有一个用Go编写的简单登录系统。现在,我将登录屏幕和内部页面存储为常量。我想使用.html文件而不是常量,因为我现在不能使用CSS样式,我想用引导设置登录屏幕的样式 立即打开main.go文件: package main import ( "fmt" "github.com/gorilla/mux" "github.com/gorilla/securecookie" "net/http" ) /

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

立即打开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)
}
主程序包
进口(
“fmt”
“github.com/gorilla/mux”
“github.com/gorilla/securecookie”
“net/http”
)
//饼干处理
var cookieHandler=securecookie.New(
securecookie.GeneratorDomainKey(64),
securecookie.GeneratorDomainKey(32))
func getUserName(请求*http.request)(用户名字符串){
如果是cookie,err:=request.cookie(“会话”);err==nil{
cookieValue:=make(映射[字符串]字符串)
如果err=cookieHandler.Decode(“会话”、cookie.Value和cookieValue”);err==nil{
用户名=cookieValue[“名称”]
}
}
返回用户名
}
func setSession(用户名字符串,响应http.ResponseWriter){
值:=映射[字符串]字符串{
“名称”:用户名,
}
如果编码,err:=cookieHandler.Encode(“会话”,值);err==nil{
cookie:=&http.cookie{
名称:“会话”,
值:已编码,
路径:“/”,
}
http.SetCookie(响应,cookie)
}
}
func清除会话(响应http.ResponseWriter){
cookie:=&http.cookie{
名称:“会话”,
值:“”,
路径:“/”,
MaxAge:-1,
}
http.SetCookie(响应,cookie)
}
//登录处理程序
func loginHandler(响应http.ResponseWriter,请求*http.request){
名称:=request.FormValue(“名称”)
pass:=request.FormValue(“密码”)
重定向目标:=“/”
如果名称!=“”&通过!=“”{
//…检查凭据。。
设置会话(名称、响应)
重定向目标=“/internal”
}
重定向(响应、请求、重定向目标、302)
}
//注销处理程序
func logoutHandler(响应http.ResponseWriter,请求*http.request){
清除会话(响应)
重定向(响应,请求,“/”,302)
}
//索引页
常数索引页=`
登录
用户名
暗语
登录
`
func indexPageHandler(响应http.ResponseWriter,请求*http.request){
fmt.Fprintf(响应,索引扩展)
}
//内部页面
常量内部页=`
内部的

用户:%s < 按钮类型=“提交”>注销 ` func internalPageHandler(响应http.ResponseWriter,请求*http.request){ 用户名:=获取用户名(请求) 如果用户名!“”{ fmt.Fprintf(响应、内部页面、用户名) }否则{ 重定向(响应,请求,“/”,302) } } //服务器主方法 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(“/”,路由器) http.listendServe(“:8000”,无) }

如何使用indexPage.html和internalPage.html而不是indexPage和internalPage常量?

使用Go的
html/template
package-请参见这里的示例:一定要查看html/template包。此外,如果您愿意使用其他软件包,那么类似的软件包是gorilla/mux的一种替代方案,其中包括模板处理。您可以使用gin提供css/js等静态文件以及html等模板文件。