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
|
|
|
|
|
|
|
"git.xdrm.io/example/aicra/service/auth"
|
|
|
|
"git.xdrm.io/example/aicra/service/shortener"
|
|
|
|
|
|
|
|
"git.xdrm.io/example/aicra/storage"
|
|
|
|
"git.xdrm.io/go/aicra"
|
|
|
|
"git.xdrm.io/go/aicra/typecheck/builtin"
|
2018-06-10 20:49:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2018-10-01 17:28:54 +00:00
|
|
|
listenTo := "127.0.0.1:4242"
|
2018-07-11 17:08:09 +00:00
|
|
|
|
2019-05-01 14:42:17 +00:00
|
|
|
// 1. build server
|
|
|
|
log.Printf("[server] building")
|
2018-10-01 17:28:54 +00:00
|
|
|
server, err := aicra.New("api.json")
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. add type checkers
|
|
|
|
server.Checkers.Add(builtin.NewAny())
|
|
|
|
server.Checkers.Add(builtin.NewString())
|
|
|
|
server.Checkers.Add(builtin.NewFloat64())
|
|
|
|
|
|
|
|
// 3. storage connect
|
|
|
|
log.Printf("[storage] connecting")
|
2019-05-02 05:54:06 +00:00
|
|
|
db, err := storage.New()
|
2019-05-01 14:42:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("/!\\ cannot connect %v", err)
|
2018-06-10 20:49:32 +00:00
|
|
|
}
|
2019-05-02 05:54:06 +00:00
|
|
|
defer db.Close()
|
2019-05-01 14:42:17 +00:00
|
|
|
|
|
|
|
// 4. init services
|
2019-05-02 18:50:58 +00:00
|
|
|
authService, err := auth.New(db)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("cannot create auth service: %v", err)
|
|
|
|
}
|
|
|
|
shortenerService, err := shortener.New(db, authService)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("cannot create auth service: %v", err)
|
|
|
|
}
|
2019-05-01 14:42:17 +00:00
|
|
|
|
|
|
|
// 5. wire services
|
|
|
|
shortenerService.Wire(server)
|
|
|
|
authService.Wire(server)
|
2018-06-10 20:49:32 +00:00
|
|
|
|
2019-05-01 14:42:17 +00:00
|
|
|
// 6. listen and serve
|
2019-05-02 18:50:58 +00:00
|
|
|
log.Printf("[server] listening\n\n")
|
2019-05-02 19:31:49 +00:00
|
|
|
log.Fatal(http.ListenAndServe(listenTo, server))
|
2018-06-10 20:49:32 +00:00
|
|
|
|
|
|
|
}
|