refdata/cmd/api.go

39 lines
813 B
Go
Raw Normal View History

2018-03-28 01:29:06 +02:00
package cmd
import (
"fmt"
"github.com/spf13/cobra"
2018-03-28 01:44:59 +02:00
"github.com/gin-gonic/gin"
"git.icod.de/dalu/refdata/api/handler"
"git.icod.de/dalu/refdata/api/repo"
"github.com/globalsign/mgo"
2018-03-28 01:29:06 +02:00
)
// apiCmd represents the api command
var apiCmd = &cobra.Command{
Use: "api",
2018-03-28 01:44:59 +02:00
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())
2018-03-28 01:29:06 +02:00
},
}
func init() {
RootCmd.AddCommand(apiCmd)
2018-03-28 01:44:59 +02:00
apiCmd.Flags().String("listen", ":8080", "listen on addr:port")
2018-03-28 01:29:06 +02:00
}