70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"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"
|
|
|
|
"git.xdrm.io/go/aicra"
|
|
)
|
|
|
|
func main() {
|
|
|
|
listenTo := ":4242"
|
|
|
|
// 1. build server
|
|
log.Printf("[server] create server")
|
|
server, err := aicra.New("api.json", buildDataTypes()...)
|
|
if err != nil {
|
|
log.Fatalf("/!\\ cannot init server: %v\n", err)
|
|
}
|
|
|
|
// 2. connect to storage
|
|
db, err := gorm.Open("sqlite3", "test.db")
|
|
if err != nil {
|
|
log.Fatalf("/!\\ cannot open database: %v\n", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
// 3. init services
|
|
userService := &user.Service{DB: db}
|
|
articleService := &article.Service{DB: db}
|
|
|
|
// 4. wire services
|
|
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)
|
|
}
|
|
|
|
// 5. create http server
|
|
httpServer, err := server.ToHTTPServer()
|
|
if err != nil {
|
|
log.Fatalf("cannot get http server: %s", err)
|
|
}
|
|
|
|
// 5. listen and serve
|
|
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{})
|
|
dtypes = append(dtypes, builtin.StringDataType{})
|
|
|
|
return dtypes
|
|
}
|