api.go 737 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package cmd
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/spf13/cobra"
  5. "ppgo/common"
  6. "ppgo/module/api/controller"
  7. )
  8. var WebCmd = &cobra.Command{
  9. Use: "api",
  10. Short: "start api service",
  11. Long: `service`,
  12. Run: runCmd,
  13. }
  14. func init() {
  15. RootCmd.AddCommand(WebCmd)
  16. }
  17. func runCmd(cmd *cobra.Command, args []string) {
  18. r := newEngine()
  19. setupRouter(r)
  20. startHttp(r, common.GetConfig().Service.Httpport)
  21. }
  22. func setupRouter(r *gin.Engine) {
  23. gr := r.Group("/api")
  24. config := controller.ConfigCtrl{}
  25. gr.GET("/config/all", config.All)
  26. }
  27. func newEngine() *gin.Engine {
  28. isDebug := common.GetConfig().Service.IsDebug
  29. if isDebug {
  30. gin.SetMode(gin.DebugMode)
  31. } else {
  32. gin.SetMode(gin.ReleaseMode)
  33. }
  34. return gin.Default()
  35. }