| 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 |
| } |
| |
| |
| |
| |
| |
| 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) |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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() |
| |
| r.Static("/static", "static") |
| |
| r.LoadHTMLGlob("templates/*") |
| r.GET("/", controller.IndexHandler) |
| |
| |
| 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 |
| } |
| |
| 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() |
| } |
| |
| package models |
| |
| import ( |
| "bubble/dao" |
| ) |
| |
| |
| type Todo struct { |
| ID int `json:"id"` |
| Title string `json:"title"` |
| Status bool `json:"status"` |
| } |
| |
| |
| |
| |
| |
| 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 |
| } |
| |
| package controller |
| |
| import ( |
| "bubble/models" |
| "github.com/gin-gonic/gin" |
| "net/http" |
| ) |
| |
| |
| |
| |
| |
| |
| func IndexHandler(c *gin.Context) { |
| c.HTML(http.StatusOK, "index.html", nil) |
| } |
| |
| |
| func CreateTodo(c *gin.Context) { |
| |
| |
| var todo models.Todo |
| c.BindJSON(&todo) |
| |
| err:=models.CreateATodo(&todo) |
| if err != nil{ |
| c.JSON(http.StatusOK, gin.H{"error": err.Error()}) |
| }else{ |
| c.JSON(http.StatusOK, todo) |
| |
| |
| |
| |
| |
| } |
| } |
| |
| func GetTodoList(c *gin.Context) { |
| |
| 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"}) |
| } |
| } |
| package setting |
| |
| import ( |
| "gopkg.in/ini.v1" |
| ) |
| |
| var Conf = new(AppConfig) |
| |
| |
| type AppConfig struct { |
| Release bool `ini:"release"` |
| Port int `ini:"port"` |
| *MySQLConfig `ini:"mysql"` |
| } |
| |
| |
| 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) |
| } |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战