tiny-url-ex/main.go

55 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"log"
"net/http"
"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"
)
func main() {
2018-10-01 17:28:54 +00:00
listenTo := "127.0.0.1:4242"
// 1. build server
log.Printf("[server] building")
2018-10-01 17:28:54 +00:00
server, err := aicra.New("api.json")
if err != nil {
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")
storageClient, err := storage.New()
if err != nil {
log.Fatalf("/!\\ cannot connect %v", err)
}
defer storageClient.Close()
// 4. init services
authService := auth.New(storageClient)
shortenerService := shortener.New(storageClient, authService)
// 5. wire services
shortenerService.Wire(server)
authService.Wire(server)
// 6. listen and serve
log.Printf("[server] listening")
2018-10-01 17:28:54 +00:00
err = http.ListenAndServe(listenTo, server)
if err != nil {
log.Fatalf("/!\\ cannot listen: %v\n", err)
}
}