/Users/song/codelearn/golang/bubble-master/go.mod
module bubble
go 1.13
require (
github.com/0xAX/notificator v0.0.0-20220220101646-ee9b8921e557 // indirect
github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75 // indirect
github.com/codegangsta/envy v0.0.0-20141216192214-4b78388c8ce4 // indirect
github.com/codegangsta/gin v0.0.0-20230218063734-2c98d96c9244 // indirect
github.com/cosmtrek/air v1.42.0 // indirect
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/gin-gonic/gin v1.5.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/jinzhu/gorm v1.9.12
github.com/kardianos/govendor v1.0.9 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/urfave/cli v1.22.12 // indirect
golang.org/x/tools v0.7.0 // indirect
gopkg.in/ini.v1 v1.55.0
)
/Users/song/codelearn/golang/bubble-master/main.go
package main
import (
"bubble/dao"
"bubble/models"
"bubble/routers"
"bubble/setting"
"fmt"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage:./bubble conf/config.ini")
return
}
// 加载配置文件
if err := setting.Init(os.Args[1]); err != nil {
fmt.Printf("load config from file failed, err:%v\n", err)
return
}
// 创建数据库
// sql: CREATE DATABASE bubble;
// 连接数据库
err := dao.InitMySQL(setting.Conf.MySQLConfig)
if err != nil {
fmt.Printf("init mysql failed, err:%v\n", err)
return
}
defer dao.Close() // 程序退出关闭数据库连接
// 模型绑定
dao.DB.AutoMigrate(&models.Todo{})
// 注册路由
r := routers.SetupRouter()
if err := r.Run(fmt.Sprintf("localhost:%d", setting.Conf.Port)); err != nil {
fmt.Printf("server startup failed, err:%v\n", err)
}
}
// # Q:gin框架的整体流程?
// 1. 首先加载配置文件
// 2. 然后连接数据库,绑定模型
// 3. 注册路由
// 4. 启动程序
// # Q:gin框架,注册路由的流程?
// 1. 如果有静态文件夹或者template文件,呢么就先配置
// 2. 然后注册路由,根据路由的多少可以选择分组配置
// 3. 路由中注册控制器,然后在控制器中完成增删改查,返回数据
// 4. 如果在细分的话,呢么控制还可以继续拆分成数据库操作层和service层,等等
// 5. 以上就是一个gin入门框架的大体流程
/Users/song/codelearn/golang/bubble-master/routers/routers.go
package routers
import (
"bubble/controller"
"bubble/setting"
"github.com/gin-gonic/gin"
)
func SetupRouter() *gin.Engine {
if setting.Conf.Release {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
// 告诉gin框架模板文件引用的静态文件去哪里找
r.Static("/static", "static")
// 告诉gin框架去哪里找模板文件
r.LoadHTMLGlob("templates/*")
r.GET("/", controller.IndexHandler)
// v1
v1Group := r.Group("v1")
{
// 待办事项
// 添加
v1Group.POST("/todo", controller.CreateTodo)
// 查看所有的待办事项
v1Group.GET("/todo", controller.GetTodoList)
// 修改某一个待办事项
v1Group.PUT("/todo/:id", controller.UpdateATodo)
// 删除某一个待办事项
v1Group.DELETE("/todo/:id", controller.DeleteATodo)
}
return r
}
/Users/song/codelearn/golang/bubble-master/dao/mysql.go
package dao
import (
"bubble/setting"
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
var (
DB *gorm.DB
)
func InitMySQL(cfg *setting.MySQLConfig) (err error) {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.DB)
DB, err = gorm.Open("mysql", dsn)
if err != nil {
return
}
return DB.DB().Ping()
}
func Close() {
DB.Close()
}
/Users/song/codelearn/golang/bubble-master/models/todo.go
package models
import (
"bubble/dao"
)
// Todo Model
type Todo struct {
ID int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
/*
Todo这个Model的增删改查操作都放在这里
*/
// CreateATodo 创建todo
func CreateATodo(todo *Todo) (err error) {
err = dao.DB.Create(&todo).Error
return
}
func GetAllTodo() (todoList []*Todo, err error) {
if err = dao.DB.Find(&todoList).Error; err != nil {
return nil, err
}
return
}
func GetATodo(id string) (todo *Todo, err error) {
todo = new(Todo)
if err = dao.DB.Debug().Where("id=?", id).First(todo).Error; err != nil {
return nil, err
}
return
}
func UpdateATodo(todo *Todo) (err error) {
err = dao.DB.Save(todo).Error
return
}
func DeleteATodo(id string) (err error) {
err = dao.DB.Where("id=?", id).Delete(&Todo{}).Error
return
}
/Users/song/codelearn/golang/bubble-master/controller/controller.go
package controller
import (
"bubble/models"
"github.com/gin-gonic/gin"
"net/http"
)
/*
url --> controller --> logic --> model
请求来了 --> 控制器 --> 业务逻辑 --> 模型层的增删改查
*/
func IndexHandler(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
}
func CreateTodo(c *gin.Context) {
// 前端页面填写待办事项 点击提交 会发请求到这里
// 1. 从请求中把数据拿出来
var todo models.Todo
c.BindJSON(&todo)
// 2. 存入数据库
err:=models.CreateATodo(&todo)
if err != nil{
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else{
c.JSON(http.StatusOK, todo)
//c.JSON(http.StatusOK, gin.H{
// "code": 2000,
// "msg": "success",
// "data": todo,
//})
}
}
func GetTodoList(c *gin.Context) {
// 查询todo这个表里的所有数据
todoList, err := models.GetAllTodo()
if err!= nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else {
c.JSON(http.StatusOK, todoList)
}
}
func UpdateATodo(c *gin.Context) {
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "无效的id"})
return
}
todo, err := models.GetATodo(id)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
}
c.BindJSON(&todo)
if err = models.UpdateATodo(todo); err!= nil{
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else{
c.JSON(http.StatusOK, todo)
}
}
func DeleteATodo(c *gin.Context) {
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "无效的id"})
return
}
if err := models.DeleteATodo(id);err!=nil{
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else{
c.JSON(http.StatusOK, gin.H{id:"deleted"})
}
}
/Users/song/codelearn/golang/bubble-master/setting/setting.go
package setting
import (
"gopkg.in/ini.v1"
)
var Conf = new(AppConfig)
// AppConfig 应用程序配置
type AppConfig struct {
Release bool `ini:"release"`
Port int `ini:"port"`
*MySQLConfig `ini:"mysql"`
}
// MySQLConfig 数据库配置
type MySQLConfig struct {
User string `ini:"user"`
Password string `ini:"password"`
DB string `ini:"db"`
Host string `ini:"host"`
Port int `ini:"port"`
}
func Init(file string) error {
return ini.MapTo(Conf, file)
}
标签:github,err,todolist,bubble,golang,com,gin,todo
From: https://www.cnblogs.com/zhuoss/p/17224236.html