2018-07-08 13:29:25 +00:00
|
|
|
package main
|
2018-06-10 20:49:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2018-07-11 17:08:09 +00:00
|
|
|
"net/http"
|
2019-05-01 14:42:17 +00:00
|
|
|
|
2020-03-22 12:49:08 +00:00
|
|
|
"git.xdrm.io/go/aicra/datatype"
|
|
|
|
"git.xdrm.io/go/aicra/datatype/builtin"
|
|
|
|
"git.xdrm.io/go/tiny-url-ex/service/article"
|
|
|
|
"git.xdrm.io/go/tiny-url-ex/service/user"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
2019-05-01 14:42:17 +00:00
|
|
|
|
|
|
|
"git.xdrm.io/go/aicra"
|
2018-06-10 20:49:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2020-03-09 17:58:49 +00:00
|
|
|
listenTo := ":4242"
|
2018-07-11 17:08:09 +00:00
|
|
|
|
2019-05-01 14:42:17 +00:00
|
|
|
// 1. build server
|
2020-03-22 12:49:08 +00:00
|
|
|
log.Printf("[server] create server")
|
|
|
|
server, err := aicra.New("api.json", buildDataTypes()...)
|
2018-06-10 20:49:32 +00:00
|
|
|
if err != nil {
|
2019-05-01 14:42:17 +00:00
|
|
|
log.Fatalf("/!\\ cannot init server: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2020-03-22 12:49:08 +00:00
|
|
|
// 2. connect to storage
|
|
|
|
db, err := gorm.Open("sqlite3", "test.db")
|
2019-05-02 18:50:58 +00:00
|
|
|
if err != nil {
|
2020-03-22 12:49:08 +00:00
|
|
|
log.Fatalf("/!\\ cannot open database: %v\n", err)
|
2019-05-02 18:50:58 +00:00
|
|
|
}
|
2020-03-22 12:49:08 +00:00
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// 3. init services
|
|
|
|
userService := &user.Service{DB: db}
|
|
|
|
articleService := &article.Service{DB: db}
|
2019-05-01 14:42:17 +00:00
|
|
|
|
2020-03-03 19:02:15 +00:00
|
|
|
// 4. wire services
|
2020-03-29 15:03:53 +00:00
|
|
|
if err = userService.Wire(server); err != nil {
|
|
|
|
log.Fatalf("/!\\ cannot wire service 'user': %s", err)
|
|
|
|
}
|
|
|
|
if err = articleService.Wire(server); err != nil {
|
|
|
|
log.Fatalf("/!\\ cannot wire service 'article': %s", err)
|
|
|
|
}
|
2020-03-22 12:49:08 +00:00
|
|
|
|
|
|
|
// 5. create http server
|
|
|
|
httpServer, err := server.ToHTTPServer()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("cannot get http server: %s", err)
|
|
|
|
}
|
2018-06-10 20:49:32 +00:00
|
|
|
|
2020-03-03 19:02:15 +00:00
|
|
|
// 5. listen and serve
|
2020-03-22 12:49:08 +00:00
|
|
|
log.Printf("[server] listening to '%s'\n\n", listenTo)
|
|
|
|
log.Fatal(http.ListenAndServe(listenTo, httpServer))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildDataTypes() []datatype.T {
|
|
|
|
dtypes := make([]datatype.T, 0)
|
|
|
|
|
|
|
|
dtypes = append(dtypes, builtin.AnyDataType{})
|
|
|
|
dtypes = append(dtypes, builtin.BoolDataType{})
|
|
|
|
dtypes = append(dtypes, builtin.UintDataType{})
|
2020-03-29 15:30:36 +00:00
|
|
|
dtypes = append(dtypes, builtin.IntDataType{})
|
2020-03-22 12:49:08 +00:00
|
|
|
dtypes = append(dtypes, builtin.StringDataType{})
|
2018-06-10 20:49:32 +00:00
|
|
|
|
2020-03-22 12:49:08 +00:00
|
|
|
return dtypes
|
2018-06-10 20:49:32 +00:00
|
|
|
}
|