github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/app/external/api/route.go (about)

     1  package api
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"github.com/lovung/GoCleanArchitecture/app/config"
     8  	"github.com/lovung/GoCleanArchitecture/app/internal/interface/restful/middleware"
     9  	"github.com/lovung/GoCleanArchitecture/app/registry"
    10  	"github.com/lovung/GoCleanArchitecture/pkg/logger"
    11  
    12  	"github.com/gin-contrib/pprof"
    13  	ginzap "github.com/gin-contrib/zap"
    14  	"github.com/gin-gonic/gin"
    15  )
    16  
    17  const (
    18  	envStaging    = "staging"
    19  	envProduction = "production"
    20  )
    21  
    22  //nolint:funlen
    23  // Restful define mapping routes
    24  // @title github.com/lovung/GoCleanArchitecture core service
    25  // @version 1.0
    26  // @description This is the project of github.com/lovung/GoCleanArchitecture
    27  // @termsOfService http://swagger.io/terms/
    28  // @contact.name API Support
    29  // @contact.url http://www.swagger.io/support
    30  // @contact.email support@swagger.io
    31  // @license.name Apache 2.0
    32  // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
    33  // @BasePath /api/v1
    34  func Restful(cfg *config.Config) *gin.Engine {
    35  	router := gin.Default()
    36  
    37  	// Add a ginzap middleware, which:
    38  	//   - Logs all requests, like a combined access and error log.
    39  	//   - Logs to stdout.
    40  	//   - RFC3339 with UTC time format.
    41  	router.Use(ginzap.Ginzap(logger.Instance(), time.RFC3339, true))
    42  
    43  	// Logs all panic to error log
    44  	//   - stack means whether output the stack info.
    45  	router.Use(ginzap.RecoveryWithZap(logger.Instance(), true))
    46  	if cfg.Env != envProduction && cfg.Env != envStaging {
    47  		router.Use(middleware.CorsMiddleware())
    48  	}
    49  	router.Use(middleware.AddTimeout)
    50  	router.GET("/", root)
    51  	router.GET("/api/healthz", healthz)
    52  	router.Use(middleware.JSONWriterMiddleware)
    53  	// pprof: default path is /debug/pprof
    54  	if cfg.EnabledProfiling {
    55  		pprof.Register(router)
    56  	}
    57  
    58  	authHandler := registry.AuthHandler()
    59  	txnMw := registry.TransactionMiddleware()
    60  
    61  	authRoute := router.Group("/auth")
    62  	authRoute.Use(txnMw.StartRequest)
    63  	authRoute.Use(txnMw.EndRequest)
    64  	authRoute.POST("/register", authHandler.Register)
    65  
    66  	return router
    67  }
    68  
    69  func root(ctx *gin.Context) {
    70  	type svcInfo struct {
    71  		JSONAPI struct {
    72  			Version string `json:"version,omitempty"`
    73  			Name    string `json:"name,omitempty"`
    74  		} `json:"jsonapi"`
    75  	}
    76  
    77  	info := svcInfo{}
    78  	info.JSONAPI.Version = "v1"
    79  	info.JSONAPI.Name = "github.com/lovung/GoCleanArchitecture core API"
    80  
    81  	ctx.JSON(http.StatusOK, info)
    82  }
    83  
    84  func healthz(ctx *gin.Context) {
    85  	ctx.JSON(http.StatusOK, "OK")
    86  }