package repository import ( "github.com/gin-gonic/gin" . "goweb/global" "goweb/model" ) type apisRepository struct{} type ApisRepository interface { SaveAllApi(info gin.RoutesInfo) Migrate() error } // NewApisRepository 新建实例 func NewApisRepository() ApisRepository { return apisRepository{} } // SaveAllApi 保存所有gin的路由 func (ar apisRepository) SaveAllApi(routes gin.RoutesInfo) { for _, route := range routes { m := model.Apis{Path: route.Path, Method: route.Method} err := MySQLConn.FirstOrCreate(&m, &m).Error if err != nil { Log.Infof("保存菜单错误: %v", err) } } } // Migrate 初始化数据库结构 func (ar apisRepository) Migrate() error { return MySQLConn.AutoMigrate(&model.Apis{}) }