refdata/cmd/api.go
2018-03-28 04:24:06 +02:00

37 lines
806 B
Go

package cmd
import (
"git.icod.de/dalu/refdata/api/handler"
"git.icod.de/dalu/refdata/api/repo"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo"
"github.com/spf13/cobra"
)
// apiCmd represents the api command
var apiCmd = &cobra.Command{
Use: "api",
Short: "starts serving the api",
Long: `starts serving the api`,
RunE: func(cmd *cobra.Command, args []string) error {
ms, e := mgo.Dial("localhost")
if e != nil {
return e
}
defer ms.Close()
r := gin.Default()
api := r.Group("/api/v1")
dataRepo := repo.NewDataRepository(ms, "apiname", "data")
h := handler.NewHandler(dataRepo)
h.DataRoutes(api)
return r.Run(cmd.Flag("listen").Value.String())
},
}
func init() {
RootCmd.AddCommand(apiCmd)
apiCmd.Flags().String("listen", ":8080", "listen on addr:port")
}