api.go 765 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. gr.GET("/log", config.Log)
  27. }
  28. func newEngine() *gin.Engine {
  29. isDebug := common.GetConfig().Service.IsDebug
  30. if isDebug {
  31. gin.SetMode(gin.DebugMode)
  32. } else {
  33. gin.SetMode(gin.ReleaseMode)
  34. }
  35. return gin.Default()
  36. }