Initialisation of the project
This commit is contained in:
parent
01306c1b04
commit
a67ef2a608
|
|
@ -0,0 +1,8 @@
|
|||
appname = DataHouse
|
||||
httpport = 8080
|
||||
runmode = dev
|
||||
|
||||
mysqluser = "root"
|
||||
mysqlpass = "toto"
|
||||
mysqlurls = "127.0.0.1"
|
||||
mysqldb = "orm_test"
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
|
||||
// "datahouse/models/temperature"
|
||||
)
|
||||
|
||||
type MainController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *MainController) Get() {
|
||||
// temperature.AddData(5)
|
||||
|
||||
c.Data["Website"] = "beego.me"
|
||||
c.Data["Email"] = "astaxie@gmail.com"
|
||||
c.TplNames = "index.tpl"
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
_ "datahouse/routers"
|
||||
"datahouse/models/database"
|
||||
_ "datahouse/models/temperature"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/astaxie/beego/orm"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
var (
|
||||
log = logs.NewLogger(10000)
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetLogger("console", "")
|
||||
|
||||
orm.RegisterDriver("mysql", orm.DR_MySQL)
|
||||
url := database.UserDB+":"+database.PwdDB+"@/"+database.DataBase+"?charset=utf8"
|
||||
err := orm.RegisterDataBase(database.Alias, "mysql", url)
|
||||
if err != nil {
|
||||
log.Error("Failed to register database", err)
|
||||
}
|
||||
|
||||
force := false
|
||||
verbose := true
|
||||
err = orm.RunSyncdb(database.Alias, force, verbose)
|
||||
if err != nil {
|
||||
log.Error("Failed to initialize database", err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
beego.Run()
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
var (
|
||||
Alias = "default"
|
||||
UserDB = beego.AppConfig.String("mysqluser")
|
||||
PwdDB = beego.AppConfig.String("mysqlpass")
|
||||
HostDB = beego.AppConfig.String("mysqlurls")
|
||||
DataBase = beego.AppConfig.String("mysqldb")
|
||||
)
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package temperature
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/orm"
|
||||
|
||||
"datahouse/models/database"
|
||||
|
||||
"time"
|
||||
)
|
||||
|
||||
type tempTable struct {
|
||||
Id int64
|
||||
HorodateGMT time.Time `orm:"auto_now;type(datetime)"`
|
||||
Value int64
|
||||
}
|
||||
|
||||
func init() {
|
||||
orm.RegisterModel(new(tempTable))
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add the value into the database.
|
||||
*/
|
||||
func AddData(value int64) {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
|
||||
tmpTable := new(tempTable)
|
||||
tmpTable.Value = value
|
||||
o.Insert(tmpTable)
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"datahouse/controllers"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
func init() {
|
||||
beego.Router("/", &controllers.MainController{})
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"runtime"
|
||||
"path/filepath"
|
||||
|
||||
_ "datahouse/routers"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func init() {
|
||||
_, file, _, _ := runtime.Caller(1)
|
||||
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
|
||||
beego.TestBeegoInit(apppath)
|
||||
}
|
||||
|
||||
|
||||
// TestMain is a sample to run an endpoint test
|
||||
func TestMain(t *testing.T) {
|
||||
r, _ := http.NewRequest("GET", "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
beego.BeeApp.Handlers.ServeHTTP(w, r)
|
||||
|
||||
beego.Trace("testing", "TestMain", "Code[%d]\n%s", w.Code, w.Body.String())
|
||||
|
||||
Convey("Subject: Test Station Endpoint\n", t, func() {
|
||||
Convey("Status Code Should Be 200", func() {
|
||||
So(w.Code, ShouldEqual, 200)
|
||||
})
|
||||
Convey("The Result Should Not Be Empty", func() {
|
||||
So(w.Body.Len(), ShouldBeGreaterThan, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue