Golang框架Gin入门实战--(7)Gin中自定义控制器以及实现控制器的继承

Golang框架Gin入门实战–(7)Gin中自定义控制器以及实现控制器的继承

按照图片所示进行创建目录以及文件
Golang框架Gin入门实战--(7)Gin中自定义控制器以及实现控制器的继承_第1张图片

main.go

package main

import (
	"GINDEMO/routers"
	"fmt"

	"html/template"
	"time"

	"github.com/gin-gonic/gin"
)

func UnixToTime(timestamp int) string {
	fmt.Println(timestamp)
	t := time.Unix(int64(timestamp), 0)

	return t.Format("2006-01-02 15:04:05")
}

func main() {
	//创建一个默认的路由引擎
	r := gin.Default()
	//自定义模板函数  注意要把这个函数放在加载模板前
	r.SetFuncMap(template.FuncMap{
		"UnixToTime": UnixToTime,
	})
	//加载模板
	r.LoadHTMLGlob("templates/**/*")
	//配置静态web目录
	r.Static("/xxx", "./static")

	routers.AdminRoutersInit(r)

	routers.ApiRoutersInit(r)

	routers.DefaultRoutersInit(r)

	// }

	r.Run()
}

GINDEMO\controllers\admin/articleController.go

package admin

import "github.com/gin-gonic/gin"

type ArticleController struct {
	BaseController
}

func (con ArticleController) Index(c *gin.Context) {
	// c.String(200, "文章列表--")
	con.success(c)
}

func (con ArticleController) Add(c *gin.Context) {
	c.String(200, "-add--文章")
}

func (con ArticleController) Edit(c *gin.Context) {
	c.String(200, "-edit---文章--")
}

GINDEMO\controllers\admin/baseController.go

package admin

import "github.com/gin-gonic/gin"

type BaseController struct{}

func (con BaseController) success(c *gin.Context) {
	c.String(200, "成功")
}

func (con BaseController) error(c *gin.Context) {
	c.String(200, "失败")
}

GINDEMO\controllers\admin/indexController.go

package admin

import "github.com/gin-gonic/gin"

type IndexController struct {
}

func (con IndexController) Index(c *gin.Context) {
	c.String(200, "用户列表--")
}

GINDEMO\controllers\admin/userController.go

package admin

import "github.com/gin-gonic/gin"

type UserController struct {
	BaseController
}

func (con UserController) Index(c *gin.Context) {
	// c.String(200, "用户列表--")
	con.success(c)
}

func (con UserController) Add(c *gin.Context) {
	c.String(200, "用户列表-add--")
}

func (con UserController) Edit(c *gin.Context) {
	c.String(200, "用户列表-edit-----")
}

GINDEMO\controllers\api\apiController.go

package api

import "github.com/gin-gonic/gin"

type ApiController struct{}

func (con ApiController) Index(c *gin.Context) {
	c.String(200, "我是一个api接口--")
}
func (con ApiController) Userlist(c *gin.Context) {
	c.String(200, "我是一个api接口--Userlist")
}
func (con ApiController) Plist(c *gin.Context) {
	c.String(200, "我是一个api接口--Plist")
}

GINDEMO\controllers\ayu\defaultController.go

package ayu

import "github.com/gin-gonic/gin"

type DefaultController struct{}

func (con DefaultController) Index(c *gin.Context) {
	c.HTML(200, "default/index.html", gin.H{
		"msg": "我是一个msg",
	})
}
func (con DefaultController) News(c *gin.Context) {
	c.String(200, "News")
}

GINDEMO\routers\adminRouters.go

package routers

import (
	"GINDEMO/controllers/admin"

	"github.com/gin-gonic/gin"
)

func AdminRoutersInit(r *gin.Engine) {
	adminRouters := r.Group("/admin")
	{
		adminRouters.GET("/", admin.IndexController{}.Index)
		adminRouters.GET("/user", admin.UserController{}.Index)
		adminRouters.GET("/user/add", admin.UserController{}.Add)
		adminRouters.GET("/user/edit", admin.UserController{}.Edit)

		adminRouters.GET("/article", admin.ArticleController{}.Index)
		adminRouters.GET("/article/add", admin.ArticleController{}.Add)
		adminRouters.GET("/article/edit", admin.ArticleController{}.Edit)
	}
}

GINDEMO\routers\apiRouters.go

package routers

import (
	"GINDEMO/controllers/api"

	"github.com/gin-gonic/gin"
)

func ApiRoutersInit(r *gin.Engine) {
	apiRouters := r.Group("/api")
	{
		apiRouters.GET("/", api.ApiController{}.Index)
		apiRouters.GET("/userlist", api.ApiController{}.Userlist)
		apiRouters.GET("/plist", api.ApiController{}.Plist)
	}
}

GINDEMO\routers\defaultRouters.go

package routers

import (
	"GINDEMO/controllers/ayu"

	"github.com/gin-gonic/gin"
)

func DefaultRoutersInit(r *gin.Engine) {
	defaultRouters := r.Group("/")
	{
		defaultRouters.GET("/", ayu.DefaultController{}.Index)
		defaultRouters.GET("/news", ayu.DefaultController{}.News)
	}
}

GINDEMO\templates\default\index.html

{{define "default/index.html"}}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>我是一个首页--index</h1>
    <br>
    <br>
    {{.msg}}
</body>
</html>
{{end}}

你可能感兴趣的:(Gin框架,golang,开发语言,后端)