设为首页 加入收藏

TOP

彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-项目结构优化EP05(一)
2023-07-23 13:27:07 】 浏览:78
Tags:高性能 Web 框架 Iris 项目实 项目结 EP05

前文再续,上一回我们完成了用户管理模块的CURD(增删改查)功能,功能层面,无甚大观,但有一个结构性的缺陷显而易见,那就是项目结构过度耦合,项目的耦合性(Coupling),也叫耦合度,进而言之,模块之间的关系,是对项目结构中各模块间相互联系紧密程度的一种量化。耦合的强弱取决于模块间调用的复杂性、调用模块之间的方式以及通过函数或者方法传送数据对象的多少。模块间的耦合度是指模块之间的依赖关系,包括包含关系、控制关系、调用关系、数据传递关系以及依赖关系。项目模块的相互依赖越多,其耦合性越强,同时表明其独立性越差,愈加难以维护。

项目结构优化

目前IrisBlog项目的问题就是独立性太差,截至目前为止,项目结构如下:

.  
├── README.md  
├── assets  
│ ├── css  
│ │ └── style.css  
│ └── js  
│     ├── axios.js  
│     └── vue.js  
├── favicon.ico  
├── go.mod  
├── go.sum  
├── main.go  
├── model  
│ └── model.go  
├── mytool  
│ └── mytool.go  
├── tmp  
│ └── runner-build  
└── views  
    ├── admin  
    │ └── user.html  
    ├── index.html  
    └── test.html

一望而知,前端页面(views)以及静态文件(assets)的工程化尚可,不再需要进行分层操作,但是在后端,虽然模型层(model.go)和工具层(mytool.go)已经分离出主模块,但主要业务代码还是集中在入口文件main.go中:

package main  
  
import (  
	  
	"IrisBlog/model"  
	"IrisBlog/mytool"  
  
	"fmt"  
  
	"github.com/jinzhu/gorm"  
  
	_ "github.com/jinzhu/gorm/dialects/mysql"  
	"github.com/kataras/iris/v12"  
)  
  
func main() {  
  
	db, err := gorm.Open("mysql", "root:root@(localhost)/irisblog?charset=utf8mb4&parseTime=True&loc=Local")  
  
	if err != nil {  
		fmt.Println(err)  
		panic("无法连接数据库")  
	}  
	fmt.Println("连接数据库成功")  
  
	//单数模式  
	db.SingularTable(true)  
  
	// 创建默认表  
	db.AutoMigrate(&model.User{})  
  
	// 逻辑结束后关闭数据库  
	defer func() {  
		_ = db.Close()  
	}()  
  
	app := newApp(db)  
  
	app.HandleDir("/assets", iris.Dir("./assets"))  
	app.Favicon("./favicon.ico")  
	app.Listen(":5000")  
}  
  
func newApp(db *gorm.DB) *iris.Application {  
  
	app := iris.New()  
  
	tmpl := iris.HTML("./views", ".html")  
	// Set custom delimeters.  
	tmpl.Delims("${", "}")  
	// Enable re-build on local template files changes.  
	tmpl.Reload(true)  
  
	app.RegisterView(tmpl)  
  
	  
  
	app.Delete("/admin/user_action/", func(ctx iris.Context) {  
  
		ID := ctx.URLParamIntDefault("id", 0)  
  
		db.Delete(&model.User{}, ID)  
  
		ret := map[string]string{  
			"errcode": "0",  
			"msg":     "删除用户成功",  
		}  
		ctx.JSON(ret)  
  
	})  
  
	app.Put("/admin/user_action/", func(ctx iris.Context) {  
  
		ID := ctx.PostValue("id")  
		Password := ctx.PostValue("password")  
  
		user := &model.User{}  
		db.First(&user, ID)  
  
		user.Password = mytool.Make_password(Password)  
		db.Save(&user)  
  
		ret := map[string]string{  
			"errcode": "0",  
			"msg":     "更新密码成功",  
		}  
		ctx.JSON(ret)  
  
	})  
  
	app.Post("/admin/user_action/", func(ctx iris.Context) {  
  
		username := ctx.PostValue("username")  
		password := ctx.PostValue("password")  
  
		fmt.Println(username, password)  
  
		md5str := mytool.Make_password(password)  
  
		user := &model.User{Username: username, Password: md5str}  
		res := db.Create(user)  
  
		if res.Error != nil {  
  
			fmt.Println(res.Error)  
  
			ret := map[string]string{  
				"errcode": "1",  
				"msg":     "用户名不能重复",  
			}  
			ctx.JSON(ret)  
  
			return  
  
		}  
  
		ret := map[string]string{  
			"errcode": "0",  
			"msg"
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇我的设计模式之旅、07 观察者模式 下一篇仙人指路,引而不发,Go lang1.18入..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目