GO LANG GIN BUILD FOR WEB is put ur web compiled code in GO src dir like
src/front_out/
then in go main.go
router.Use(static.Serve("/", static.LocalFile("./views", true)))
https://forum.freecodecamp.org/t/how-to-build-a-web-app-with-go-gin-and-react/190045
GO LANG GIN JWT TOKEN APIS
https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
Login
this route will be used to authenticate the user by providing a username and password then generate and gives JSON Web Token in return
/api/login
Register
of course, because we have a login route earlier we need a way to register our login information, so it can be verified.
/api/register
for the sake of this tutorial, we will leave the registration route open. If you don't want people to be able to gain login access easily you might not want to do this.
Protected Route
this will be the route for our protected endpoints
/api/admin/user
Getting Started
to initiate our project we want to create our project dir folder and going into the directory
mkdir jwt-gin
cd jwt-gin
we can start by initiating our go.mod file to manage packages that we will be installing later on.
go mod init <your_project_name>
here’s the list of packages that we will need to install for this project.
// gin framework
go get -u github.com/gin-gonic/gin// ORM library
go get -u github.com/jinzhu/gorm// package that we will be used to authenticate and generate our JWT
go get -u github.com/dgrijalva/jwt-go// to help manage our environment variables
go get -u github.com/joho/godotenv// to encrypt our user's password
go get -u golang.org/x/crypto
Alright.. now that everything is set, we can start coding our application!
Creating our first endpoint
we can start by creating our main.go file in our root directory
touch main.go
here is the code for our starter public endpoint
package main import ( "net/http" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() public := r.Group("/api") public.POST("/register", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"data": "this is the register endpoint!"}) }) r.Run(":8080") }
Creating Register Endpoint
Now that our register endpoint is ready, we can start creating our controller file that will contain our logic for the registration process.
create controller package dir
mkdir controllers
create controller file for our login that will be named auth.go
touch ./controllers/auth.go
No comments:
Post a Comment