Using the Gin framework to build a simple login background (Go language)

I. Introduction to Gin framework Framework has always been a sharp tool in agile development, which enables developers to quickly start and make appl...
1. Folder structure
2. Relevant document code
Registration:
Sign in:
I. Introduction to Gin framework

Framework has always been a sharp tool in agile development, which enables developers to quickly start and make applications. Sometimes, without framework, some developers can't write programs. Gin is a micro framework of golang, with elegant encapsulation, friendly API and clear source code annotation. Version 2.0 has been released. It is fast and flexible, fault-tolerant and convenient. Framework is more like a collection of common functions or tools. With the help of framework development, not only can we save a lot of time brought by common encapsulation, but also help the coding style and specification of the team.

II. gin installation

First of all, it needs to be installed. It's easy to install. Just use go get
go get github.com/gin-gonic/gin
If the installation fails, go to Github clone directly and put it in the corresponding directory.

III. brief introduction to the construction process

1. Folder structure

https
--main.go
--data
-----data.go
--gin_router
-----router.go
--gin_api
-----func.go
--func_judge
-----isExist.go

2. Relevant document code

main.go:

package main import ( Main "https/gin_router" ) func main() { Main.InitRouter() }

data.go:

package data //Structure used to store user information, Id,Name,Passwd type User struct { Id int Name string Passwd string } //Slices for storing users var Slice []User //Map for temporary storage of user login information var State = make(map[string]interface{})

route.go

package gin_router import ( api "https/gin_api" "github.com/gin-gonic/gin" ) func InitRouter() { gin.SetMode(gin.ReleaseMode) //Create a routing handler using the Default method of gin router := gin.Default() //Set default route return when visiting a wrong website router.NoRoute(api.NotFound) //Use the Group function provided by the following gin to Group different API s v1 := router.Group("admin") { v1.GET("/register", api.Register) v1.GET("/login", api.Login) } //Listening server port router.Run(":8080") }

func.go:

package gin_regAndlog import ( . "https/data" Func "https/func_judge" "net/http" "github.com/gin-gonic/gin" ) //register func Register(c *gin.Context) { //Get user name and password name := c.Request.FormValue("Name") passwd := c.Request.FormValue("Passwd") //Judge whether the user exists //Output state 1 present //No create user exists, save password and user name Bool := Func.IsExist(name) if Bool { //Registration status State["state"] = 1 State["text"] = "This user already exists!" } else { //Add user if user does not exist AddStruct(name, passwd) State["state"] = 1 State["text"] = "Registration succeeded!" } //Return the status code and registration status to the client c.String(http.StatusOK, "%v", State) } //Sign in func Login(c *gin.Context) { name := c.Request.FormValue("Name") passwd := c.Request.FormValue("Passwd") //Judge whether the user exists first, and then judge whether the password is correct Bool := Func.IsExist(name) if Bool { Bool_Pwd := Func.IsRight(name, passwd) if Bool_Pwd { State["state"] = 1 State["text"] = "Login succeeded!" } else { State["state"] = 0 State["text"] = "Wrong password!" } } else { State["state"] = 2 State["text"] = "Login failed! This user is not registered!" } c.String(http.StatusOK, "%v", State) } //Set default route return when visiting a wrong website func NotFound(c *gin.Context) { c.JSON(http.StatusNotFound, gin.H{ "status": 404, "error": "404 ,page not exists!", }) } //Add user func AddStruct(name string, passwd string) { var user User user.Name = name user.Passwd = passwd user.Id = len(Slice) + 1 Slice = append(Slice, user) }

isExist.go:

package func_judge import ( . "https/data" ) //Judge whether there is a user func IsExist(user string) bool { //If the length is 0, there is no user registration if len(Slice) == 0 { return false } else { //Traversing slice for _, v := range Slice { // return v.Name == user / / at this time, it can only be compared with the first one, so it is all false after the first one if v.Name == user { return true } } } return false } //Determine whether the password is correct func IsRight(user string, passwd string) bool { for _, v := range Slice { if v.Name == user { //Confirm that the names are the same first, and return true if the passwords are the same return v.Passwd == passwd } } return false }
IV. RESTClient test

Registration:

register
Client feedback results
login has failed

Sign in:

Sign in
Login successfully

3 December 2019, 06:46 | Views: 3148

Add new comment

For adding a comment, please log in
or create account

0 comments