tiny-url-ex/main.go

71 lines
1.6 KiB
Go
Raw Normal View History

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")
2019-05-02 18:50:58 +00:00
if err != nil {
log.Fatalf("/!\\ cannot open database: %v\n", err)
2019-05-02 18:50:58 +00:00
}
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.IntDataType{})
dtypes = append(dtypes, builtin.StringDataType{})
return dtypes
}