├── conf └── app.conf ├── bee-go-vue ├── main.go ├── models ├── base.go └── task.go ├── routers └── router.go ├── README.md ├── tests └── default_test.go ├── controllers └── task_controller.go ├── static └── js │ └── app.js └── views └── index.tpl /conf/app.conf: -------------------------------------------------------------------------------- 1 | appname = bee-vue-todos 2 | httpport = 8080 3 | runmode = dev 4 | -------------------------------------------------------------------------------- /bee-go-vue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JGSU-open-source-community/bee-go-vue/HEAD/bee-go-vue -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | _ "bee-go-vue/routers" 5 | "github.com/astaxie/beego" 6 | ) 7 | 8 | func main() { 9 | beego.Run() 10 | } 11 | -------------------------------------------------------------------------------- /models/base.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | // "database/sql" 5 | 6 | // "github.com/astaxie/beego" 7 | "github.com/astaxie/beego/orm" 8 | _ "github.com/go-sql-driver/mysql" 9 | ) 10 | 11 | func init() { 12 | orm.RegisterDriver("mysql", orm.DRMySQL) 13 | orm.RegisterDataBase("default", "mysql", "root:digitalx168@tcp(127.0.0.1:3306)/task?charset=utf8mb4") 14 | } 15 | -------------------------------------------------------------------------------- /routers/router.go: -------------------------------------------------------------------------------- 1 | package routers 2 | 3 | import ( 4 | "bee-go-vue/controllers" 5 | "github.com/astaxie/beego" 6 | ) 7 | 8 | func init() { 9 | beego.Router("/", &controllers.TaskController{}, "Get:ShowIndex") 10 | beego.Router("/tasks", &controllers.TaskController{}, "Get:GetTasks") 11 | beego.Router("/task", &controllers.TaskController{}, "Post:PostTask") 12 | beego.Router("/task", &controllers.TaskController{}, "Put:PutTask") 13 | beego.Router("/task/:id", &controllers.TaskController{}, "Delete:DeleteTask") 14 | 15 | } 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bee-go-vue 2 | Build a simple todo list that use Go and Vue.js to implement. 3 | 4 | ##Stared 5 | you need have installed go env 6 | 7 | ``` 8 | create table 9 | CREATE TABLE `task` ( 10 | `id` int(11) NOT NULL AUTO_INCREMENT, 11 | `name` varchar(255) COLLATE utf8mb4_bin NOT NULL, 12 | `done` tinyint(1) NOT NULL DEFAULT '1', 13 | PRIMARY KEY (`id`) 14 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; 15 | 16 | 17 | git clone https://github.com/JingDa-open-source-community/bee-go-vue.git 18 | cd ~/src/bee-go-vue 19 | go build 20 | ./bee-go-vue 21 | ``` 22 | open you browser input localhost:8080, following you will see 23 |  24 | -------------------------------------------------------------------------------- /tests/default_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | _ "bee-go-vue/routers" 5 | "net/http" 6 | "net/http/httptest" 7 | "path/filepath" 8 | "runtime" 9 | "testing" 10 | 11 | "github.com/astaxie/beego" 12 | . "github.com/smartystreets/goconvey/convey" 13 | ) 14 | 15 | func init() { 16 | _, file, _, _ := runtime.Caller(1) 17 | apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator)))) 18 | beego.TestBeegoInit(apppath) 19 | } 20 | 21 | // TestMain is a sample to run an endpoint test 22 | func TestMain(t *testing.T) { 23 | r, _ := http.NewRequest("GET", "/", nil) 24 | w := httptest.NewRecorder() 25 | beego.BeeApp.Handlers.ServeHTTP(w, r) 26 | 27 | beego.Trace("testing", "TestMain", "Code[%d]\n%s", w.Code, w.Body.String()) 28 | 29 | Convey("Subject: Test Station Endpoint\n", t, func() { 30 | Convey("Status Code Should Be 200", func() { 31 | So(w.Code, ShouldEqual, 200) 32 | }) 33 | Convey("The Result Should Not Be Empty", func() { 34 | So(w.Body.Len(), ShouldBeGreaterThan, 0) 35 | }) 36 | }) 37 | } 38 | -------------------------------------------------------------------------------- /controllers/task_controller.go: -------------------------------------------------------------------------------- 1 | package controllers 2 | 3 | import ( 4 | "encoding/json" 5 | 6 | "bee-go-vue/models" 7 | 8 | "github.com/astaxie/beego" 9 | ) 10 | 11 | type H map[string]interface{} 12 | 13 | type TaskController struct { 14 | beego.Controller 15 | } 16 | 17 | func (t *TaskController) URLMapping() { 18 | t.Mapping("Get", t.GetTasks) 19 | t.Mapping("Post", t.PostTask) 20 | t.Mapping("Put", t.PutTask) 21 | t.Mapping("Delete", t.Delete) 22 | } 23 | 24 | func (t *TaskController) ShowIndex() { 25 | t.TplName = "index.tpl" 26 | } 27 | 28 | func (t *TaskController) GetTasks() { 29 | datas := models.GetTasks() 30 | t.Data["json"] = datas 31 | t.ServeJSON() 32 | } 33 | 34 | func (t *TaskController) PostTask() { 35 | 36 | task := t.bind() 37 | id := models.PostTask(task.Name) 38 | 39 | t.Data["json"] = H{ 40 | "created": id, 41 | } 42 | t.ServeJSON() 43 | } 44 | 45 | func (t *TaskController) PutTask() { 46 | task := t.bind() 47 | 48 | id := models.PutTask(task) 49 | 50 | t.Data["json"] = H{ 51 | "updated": id, 52 | } 53 | 54 | t.ServeJSON() 55 | } 56 | 57 | func (t *TaskController) DeleteTask() { 58 | id := t.Ctx.Input.Param(":id") 59 | models.DeleteTask(id) 60 | 61 | t.Data["json"] = H{ 62 | "deleted": id, 63 | } 64 | t.ServeJSON() 65 | } 66 | 67 | func (t *TaskController) bind() (ta models.Task) { 68 | json.NewDecoder(t.Ctx.Request.Body).Decode(&ta) 69 | return 70 | } 71 | -------------------------------------------------------------------------------- /static/js/app.js: -------------------------------------------------------------------------------- 1 | (function(Vue){ 2 | "use strict"; 3 | 4 | new Vue({ 5 | // A Dom element to mount our view model. 6 | el: 'body', 7 | 8 | // This ia the model (javascript object) 9 | // Define properties and give them initial values, here is empey values. 10 | data:{ 11 | tasks: [], 12 | newTask: {} 13 | }, 14 | 15 | created: function() { 16 | this.$http.get('/tasks').then(function(res) { 17 | this.tasks = res.data.iterms ? res.data.iterms : []; 18 | }); 19 | }, 20 | 21 | // Functions we will be using 22 | methods: { 23 | createTask: function() { 24 | if (!$.trim(this.newTask.name)) { 25 | this.newTask = {}; 26 | return; 27 | }; 28 | 29 | this.newTask.done = false; 30 | 31 | // The http client for vue.js 32 | this.$http.post('/task',this.newTask).success(function(res) { 33 | this.newTask.id = res.created; 34 | this.tasks.push(this.newTask); 35 | 36 | this.newTask = {}; 37 | }).error(function(err) { 38 | console.log(err); 39 | }); 40 | }, 41 | 42 | 43 | 44 | deleteTask : function(index) { 45 | this.$http.delete('/task/'+index).success(function(res) { 46 | this.$http.get('/tasks').then(function(res) { 47 | this.tasks = res.data.iterms ? res.data.iterms : []; 48 | }); 49 | }).error(function(err) { 50 | console.log(err) 51 | }); 52 | }, 53 | 54 | updateTask: function(task, completed) { 55 | if (completed) { 56 | task.done = true; 57 | } 58 | 59 | this.$http.put('/task', task).success(function(res) { 60 | this.$http.get('/tasks').then(function(res) { 61 | this.tasks = res.data.iterms ? res.data.iterms : []; 62 | }); 63 | }).error(function(err) { 64 | console.log(err) 65 | }); 66 | } 67 | } 68 | }); 69 | })(Vue); -------------------------------------------------------------------------------- /models/task.go: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import ( 4 | "github.com/astaxie/beego/orm" 5 | "log" 6 | "strconv" 7 | ) 8 | 9 | type Task struct { 10 | Id int `orm:"pk" json:"id"` 11 | Name string `orm:"size(255)" json:"name"` 12 | Done bool `json:"done"` 13 | } 14 | 15 | type TaskCollection struct { 16 | Tasks []Task `json:"iterms"` 17 | } 18 | 19 | func init() { 20 | orm.RegisterModel(new(Task)) 21 | } 22 | 23 | func GetTasks() TaskCollection { 24 | sql := "select *from task" 25 | 26 | o := orm.NewOrm() 27 | 28 | var maps []orm.Params 29 | if _, err := o.Raw(sql).Values(&maps); err != nil { 30 | log.Fatalf("query task failed that error was: %s", err.Error()) 31 | } 32 | 33 | result := TaskCollection{} 34 | for _, task := range maps { 35 | 36 | id, _ := strconv.Atoi(task["id"].(string)) 37 | done, _ := strconv.ParseBool(task["done"].(string)) 38 | data := Task{ 39 | Id: id, 40 | Name: task["name"].(string), 41 | Done: done, 42 | } 43 | result.Tasks = append(result.Tasks, data) 44 | } 45 | return result 46 | } 47 | 48 | func PostTask(name string) int64 { 49 | sql := `insert into task(name, done) values('` + name + `','` + "0" + `')` 50 | 51 | o := orm.NewOrm() 52 | 53 | result, err := o.Raw(sql).Exec() 54 | 55 | if err != nil { 56 | log.Fatalf("insert task into table failed that error was: %s", err.Error()) 57 | } 58 | 59 | id, err := result.LastInsertId() 60 | 61 | if err != nil { 62 | log.Fatal(err) 63 | } 64 | return id 65 | } 66 | 67 | func PutTask(task Task) int64 { 68 | var done = "0" 69 | 70 | if task.Done { 71 | done = "1" 72 | } 73 | 74 | sql := `update task set name="` + task.Name + `", done=` + done + ` where id = ` + strconv.FormatInt(int64(task.Id), 10) + `` 75 | 76 | o := orm.NewOrm() 77 | 78 | result, err := o.Raw(sql).Exec() 79 | if err != nil { 80 | log.Fatalf("update task table failed that error was: %s", err.Error()) 81 | } 82 | 83 | id, err := result.LastInsertId() 84 | 85 | if err != nil { 86 | log.Fatalln(err) 87 | } 88 | return id 89 | } 90 | 91 | func DeleteTask(id string) { 92 | sql := `delete from task where id=` + id + `` 93 | 94 | o := orm.NewOrm() 95 | 96 | if _, err := o.Raw(sql).Exec(); err != nil { 97 | log.Fatalf("delete task from table failed that error was: %s", err.Error()) 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /views/index.tpl: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |