首页 > 其他分享 >Go - Web Application 8

Go - Web Application 8

时间:2024-09-05 21:26:34浏览次数:6  
标签:Web http app dynamic mux Application user Go Handle

User authentication

 

 

Open up your handlers.go file and add placeholders for the five new handler functions as follows:

func (app *application) userSignup(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Display a form for signing up a new user...")
}

func (app *application) userSignupPost(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Create a new user...")
}

func (app *application) userLogin(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Display a form for logging in a user...")
}

func (app *application) userLoginPost(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Authenticate and login the user...")
}

func (app *application) userLogoutPost(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Logout the user...")
}

Then when that’s done, let’s create the corresponding routes in the routes.go file:

package main

import (
    "net/http"

    "github.com/justinas/alice"
)

func (app *application) routes() http.Handler {
    mux := http.NewServeMux()

    fileServer := http.FileServer(http.Dir("./ui/static/"))
    mux.Handle("GET /static/", http.StripPrefix("/static", fileServer))

    dynamic := alice.New(app.sessionManager.LoadAndSave)

    mux.Handle("GET /{$}", dynamic.ThenFunc(app.home))
    mux.Handle("GET /snippet/view/{id}", dynamic.ThenFunc(app.snippetView))
    mux.Handle("GET /snippet/create", dynamic.ThenFunc(app.snippetCreate))
    mux.Handle("POST /snippet/create", dynamic.ThenFunc(app.snippetCreatePost))

    // Add the five new routes, all of which use our `dynamic` middleware chain.
    mux.Handle("GET /user/signup", dynamic.ThenFunc(app.userSignup))
    mux.Handle("POST /user/signup", dynamic.ThenFunc(app.userSignupPost))
    mux.Handle("GET /user/login", dynamic.ThenFunc(app.userLogin))
    mux.Handle("POST /user/login", dynamic.ThenFunc(app.userLoginPost))
    mux.Handle("POST /user/logout", dynamic.ThenFunc(app.userLogoutPost))

    standard := alice.New(app.recoverPanic, app.logRequest, commonHeaders)

    return standard.Then(mux)
}

Finally, we’ll also need to update the nav.html partial to include navigation items for the new pages:

{{define "nav"}}
        <nav>
            <div>
                <a href="/">Home</a>
                <a href="/snippet/create">Create snippet</a>
            </div>
            <div>
                <a href="/user/signup">Signup</a>
                <a href="/user/login">Login</a>
                <form action="/user/logout" method="POST">
                    <button>Logout</button>
                </form>
            </div>
        </nav>
{{end}}

 

标签:Web,http,app,dynamic,mux,Application,user,Go,Handle
From: https://www.cnblogs.com/zhangzhihui/p/18399245

相关文章

  • ctfshow-web入门-信息搜集(web1-web10)
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录web1(查看源代码)右击页面查看源代码web2(js前台拦截===无效操作)打开题目地址采用burp抓包并进行重发数据包web3(没思路的时候抓个包看看,可能会有意外收获)打开题目链接查看源码无果采用burp抓包并......
  • SpringBootWeb案例(续)
    书接上回,上篇文章CSDN 复习了部门管理功能的开发。这篇文章来复习员工管理模块功能开发基于以上页面原型,我们可以把员工管理功能分为:分页查询(重点)带条件的分页查询(重点)删除员工新增员工修改员工1分页查询 1.1.基础分页 1.1.1 需求分析之前做的查询功能......
  • Go - Web Application 7
    Thehttp.ServerstructAlthoughhttp.ListenAndServe()isveryusefulinshortexamplesandtutorials,inreal-worldapplicationsit’smorecommontomanuallycreateanduseahttp.Serverstruct instead.Doingthisopensuptheopportunitytocustomizethe......
  • 20240905_182821 python 快速体验正则表达式 获取web的url
    导入正则模块元字符\d,匹配一个数字.,匹配任意符号+,修饰左边的东西让它可以匹配一次或无穷次search方法结果=re.search(规则,目标字符串)如果匹配成功可以有结果如果匹配不成功结果就是Nonesearch的结果如果匹配成功了就会得到一个对象想要拿到匹配的值可以让这个结......
  • Web 服务器怎么测压? 可用什么软件?
    一、测试方法1.确定测试目标•明确要测试的Web服务器的关键性能指标,如响应时间、吞吐量、并发用户数等。•例如,若目标是确保在高并发情况下服务器仍能保持快速响应,就需要重点关注响应时间和并发用户数。2.设计测试场景•模拟不同的用户行为,如正常浏览、提交表单、......
  • golang实现ip地址扫描
    Golang实现IP地址扫描原创 GoOfficialBlog GoOfficialBlog  2024年09月05日18:13 中国香港 听全文你是否想过哪些设备连接到了家里的Wi-Fi网络?无论是出于安全目的还是单纯的好奇心,我们都可以去了解一下家庭网络中的设备情况。在本文中,我们将介绍如何使用......
  • AI Logo制作工具网站——LogoAI.ai
    AILogo制作工具,可通过输入文本生成Logo。可自定义颜色、字体、布局等,生成的Logo可以无水印下载。功能介绍:免费每日使用配额:每天可免费使用AI生成Logo。高级自定义选项:支持对Logo的颜色、字体、布局和图标进行自定义设计,以适应不同需求。无水印下载:生成的Logo无水印,可以直接......
  • 万字解析qinguoyi / TinyWebServer项目的源码
    前言项目地址项目详细介绍本文章适合刚学习完C++基础知识并尝试实现一个网络编程项目的同学,其中包含了该项目的代码逐行注释和解析以及许多刚学习网络编程中会遇到的疑问。项目简介:Linux下C++轻量级Web服务器,助力初学者快速实践网络编程,搭建属于自己的服务器.使用线......
  • WebGL_0020:threejs 加载glb模型,加载图片贴图,加载canvas贴图,创建精灵模型并贴图
    1,import*asTHREEfrom'three';importtype{MapViewer}from'@/utils/map3d/mapViewer';import{STATIC_URL}from'@/config';import{GLTFLoader}from'three/examples/jsm/loaders/GLTFLoader';constgetSpri......
  • 基于django+vue羽毛球俱乐部管理系统设计与实现【开题报告+程序+论文】-计算机毕设
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着全民健身热潮的兴起,羽毛球作为一项低门槛、高趣味性的运动,深受广大运动爱好者的喜爱。羽毛球俱乐部的数量迅速增长,为满足会员的多元化......