39 lines
813 B
Go
39 lines
813 B
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/gin-gonic/gin"
|
|
"git.icod.de/dalu/refdata/api/handler"
|
|
"git.icod.de/dalu/refdata/api/repo"
|
|
"github.com/globalsign/mgo"
|
|
)
|
|
|
|
// 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")
|
|
}
|