66 lines
1.4 KiB
Go
66 lines
1.4 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
|
|
userService.Wire(server)
|
|
articleService.Wire(server)
|
|
|
|
// 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
|
|
}
|