golang全功能Web框架插件uAdmin的使用
Golang全功能Web框架插件uAdmin的使用
简介
uAdmin是一个易于使用、快速且安全的Golang Web框架,由IntegrityNet Solutions and Services开源。

主要特性
- AB测试系统
 - API配置
 - 审批系统
 - 认证和权限管理
 - 简洁清晰的UI界面
 - 仪表盘定制
 - 数据访问API(dAPI)
 - 数据库模式迁移
 - 错误处理
 - 导出到Excel
 - 表单和列表定制
 - 图像裁剪
 - IP地址和端口配置
 - 日志功能
 - 度量系统
 - 多语言翻译
 - MySQL数据库支持
 - 开发期间提供免费托管
 - 良好的安全特性(SSL、双因素认证、密码重置、哈希盐、数据库加密)
 - 媒体文件公开访问
 - 外键/多对多的自关联
 - 从应用中发送邮件
 - 系统范围的系统设置
 - 字段标签支持
 - 翻译文件预加载
 - 用户输入验证
 - 图像和文件字段的摄像头支持
 
安装
go get -u github.com/uadmin/uadmin/
go install github.com/uadmin/uadmin/cmd/uadmin@latest
测试安装是否成功:
$ uadmin
Usage: uadmin COMMAND [--src]
This tools helps you prepare a folder for a new project or update static files and templates
Commands:
  prepare         Generates folders and prepares static and templates
  version         Shows the version of uAdmin
Arguments:
  --src           If you want to copy static files and templates from src folder
创建第一个应用
让我们创建一个Todo列表应用。首先为项目创建文件夹并准备环境:
$ mkdir -p ~/go/src/github.com/your_name/todo
$ cd ~/go/src/github.com/your_name/todo
$ uadmin prepare
[   OK   ]   Created: /Users/abdullah/go/src/github.com/twistedhardware/test/models
[   OK   ]   Created: /Users/abdullah/go/src/github.com/twistedhardware/test/api
[   OK   ]   Created: /Users/abdullah/go/src/github.com/twistedhardware/test/views
[   OK   ]   Created: /Users/abdullah/go/src/github.com/twistedhardware/test/media
[  INFO  ]   Copying static/templates from: /Users/abdullah/go/pkg/mod/github.com/uadmin/uadmin@v0.6.0
[   OK   ]   Created: /Users/abdullah/go/src/github.com/twistedhardware/test/static
[   OK   ]   Created: /Users/abdullah/go/src/github.com/twistedhardware/test/templates
创建main.go并添加以下代码:
package main
import (
	"github.com/uadmin/uadmin"
	"time"
)
type Todo struct {
	uadmin.Model
	Name        string
	Description string `uadmin:"html"`
	TargetDate  time.Time
	Progress    int `uadmin:"progress_bar"`
}
func main() {
	uadmin.Register(Todo{})
	uadmin.StartServer()
}
初始化模块:
$ go mod init
go: creating new go.mod: module github.com/twistedhardware/test
go: to add module requirements and sums:
	go mod tidy
$ go mod tidy
go: finding module for package github.com/uadmin/uadmin
go: found github.com/uadmin/uadmin in github.com/uadmin/uadmin v0.6.0
运行应用(Linux/macOS):
$ go build; ./todo
[   OK   ]   Initializing DB: [14/14]
[   OK   ]   Initializing Languages: [185/185]
[  INFO  ]   Auto generated admin user. Username:admin, Password:admin.
[   OK   ]   Synching System Settings: [49/49]
[   OK   ]   Server Started: http://0.0.0.0:8080
         ___       __          _
  __  __/   | ____/ /___ ___  (_)___
 / / / / /| |/ __  / __ '__ \/ / __ \
/ /_/ / ___ / /_/ / / / / / / / / / /
\__,_/_/  |_\__,_/_/ /_/ /_/_/_/ /_/
Windows:
> go build && todo.exe
[   OK   ]   Initializing DB: [14/14]
[   OK   ]   Initializing Languages: [185/185]
[  INFO  ]   Auto generated admin user. Username:admin, Password:admin.
[   OK   ]   Synching System Settings: [49/49]
[   OK   ]   Server Started: http://0.0.0.0:8080
         ___       __          _
  __  __/   | ____/ /___ ___  (_)___
 / / / / /| |/ __  / __  __ \/ / __ \
/ /_/ / ___ / /_/ / / / / / / / / / /
\__,_/_/  |_\__,_/_/ /_/ /_/_/_/ /_/
快速参考
重写保存函数
func (m *Model) Save() {
	// 业务逻辑
	uadmin.Save(m)
}
验证
func (m Model) Validate() (ret map[string]string) {
  ret = map[string]string{}
  if m.Name != "test" {
    ret["Name"] = "Error name not found"
  }
  return
}
更多关于golang全功能Web框架插件uAdmin的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
        
          1 回复
        
      
      
        更多关于golang全功能Web框架插件uAdmin的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
uAdmin - Golang全功能Web框架插件使用指南
uAdmin是一个基于Golang的全功能Web框架插件,它提供了快速开发管理后台和Web应用的能力。下面我将详细介绍uAdmin的核心功能和使用方法。
1. uAdmin主要特性
- 自动生成CRUD管理界面
 - 内置用户认证和权限系统
 - 支持模型关系(一对一、一对多、多对多)
 - 内置表单验证
 - 支持多语言
 - 提供RESTful API
 - 内置文件上传处理
 - 支持主题定制
 
2. 安装uAdmin
go get -u github.com/uadmin/uadmin
3. 基本使用示例
3.1 创建模型和自动生成管理界面
package main
import (
	"github.com/uadmin/uadmin"
)
// 定义模型
type Product struct {
	uadmin.Model
	Name        string
	Description string `uadmin:"html"`
	Price       float64
	Category    string `uadmin:"list_exclude"`
	InStock     bool
}
func main() {
	// 初始化uAdmin
	uadmin.Register(
		Product{},
	)
	
	// 启动服务器
	uadmin.StartServer()
}
3.2 配置数据库和基本设置
func main() {
	// 数据库配置
	uadmin.Database = &uadmin.DBSettings{
		Name:     "mydb",
		User:     "user",
		Password: "password",
		Host:     "localhost",
		Port:     5432,
		Engine:   "postgres", // 也可以是mysql、sqlite等
	}
	
	// 其他配置
	uadmin.SiteName = "My Admin Panel"
	uadmin.Theme = "default"
	
	// 注册模型
	uadmin.Register(
		Product{},
	)
	
	// 启动服务器
	uadmin.StartServer()
}
4. 高级功能
4.1 自定义视图
// 自定义列表视图
func (p *Product) CustomListView() string {
	return `
	<div class="row">
		<div class="col-md-4">{{.Name}}</div>
		<div class="col-md-4">{{.Price}}</div>
		<div class="col-md-4">{{if .InStock}}In Stock{{else}}Out of Stock{{end}}</div>
	</div>
	`
}
// 注册时指定自定义视图
uadmin.Register(
	uadmin.ModelConfig{
		Model: Product{},
		ListDisplay: []string{"CustomListView"},
	},
)
4.2 权限控制
// 自定义权限检查
func (p *Product) UserPermission(u *uadmin.User) map[string]bool {
	permissions := map[string]bool{
		"add":     true,
		"edit":    true,
		"delete":  u.IsAdmin,
		"export":  true,
	}
	return permissions
}
4.3 API开发
// 自定义API端点
func productListAPI(w http.ResponseWriter, r *http.Request) {
	// 获取查询参数
	query := r.URL.Query()
	category := query.Get("category")
	
	// 查询数据库
	products := []Product{}
	filter := uadmin.Filter{}
	if category != "" {
		filter["category"] = category
	}
	
	uadmin.FilterObjects(&products, filter)
	
	// 返回JSON响应
	uadmin.ReturnJSON(w, r, products)
}
func main() {
	// 注册API路由
	uadmin.RegisterAPI("products", productListAPI, "GET")
	
	// 启动服务器
	uadmin.StartServer()
}
5. 实际应用示例
5.1 博客系统模型
type Author struct {
	uadmin.Model
	Name  string
	Email string `uadmin:"email"`
	Bio   string `uadmin:"html"`
}
type Post struct {
	uadmin.Model
	Title    string
	Content  string `uadmin:"html"`
	Author   Author
	AuthorID uint
	Tags     []Tag `gorm:"many2many:post_tags"`
	Published bool
	PublishDate time.Time
}
type Tag struct {
	uadmin.Model
	Name string
}
5.2 自定义仪表板
func customDashboardHandler(w http.ResponseWriter, r *http.Request, session *uadmin.Session) {
	// 获取统计数据
	productCount := uadmin.Count(&Product{}, nil)
	userCount := uadmin.Count(&uadmin.User{}, nil)
	
	// 渲染模板
	data := map[string]interface{}{
		"ProductCount": productCount,
		"UserCount":    userCount,
	}
	
	uadmin.RenderHTML(w, r, "templates/dashboard.html", data)
}
func main() {
	// 注册自定义路由
	uadmin.RegisterCustomHandler("/dashboard/", customDashboardHandler)
	
	// 启动服务器
	uadmin.StartServer()
}
6. 部署建议
- 生产环境建议使用Nginx或Apache作为反向代理
 - 对于高流量应用,考虑使用Redis缓存
 - 定期备份数据库
 - 启用HTTPS
 - 设置适当的监控和日志记录
 
uAdmin是一个功能强大但易于使用的框架,特别适合快速开发管理后台和中等复杂度的Web应用。通过合理利用其提供的功能,可以显著提高开发效率。
        
      
                    
                    
                    
