update http
This commit is contained in:
parent
2fef06cd91
commit
68843207fc
@ -11,7 +11,7 @@ var (
|
|||||||
//Ent *ent.Client
|
//Ent *ent.Client
|
||||||
Redis *redis.Client
|
Redis *redis.Client
|
||||||
Logger *zap.Logger
|
Logger *zap.Logger
|
||||||
Gin *gin.Engine
|
Router *gin.Engine
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
11
internal/access/http_context.go
Normal file
11
internal/access/http_context.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package access
|
||||||
|
|
||||||
|
import (
|
||||||
|
"framework_v2/internal/providers"
|
||||||
|
)
|
||||||
|
|
||||||
|
type User providers.JwtIDToken
|
||||||
|
|
||||||
|
type Metadata struct {
|
||||||
|
User User
|
||||||
|
}
|
36
internal/cmd/http.go
Normal file
36
internal/cmd/http.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"framework_v2/internal/access"
|
||||||
|
"framework_v2/internal/providers"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var httpCommand = &cobra.Command{
|
||||||
|
Use: "http",
|
||||||
|
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
providers.InitConfig()
|
||||||
|
providers.InitLogger()
|
||||||
|
providers.InitEnt()
|
||||||
|
providers.InitRedis()
|
||||||
|
providers.InitJwksRefresh()
|
||||||
|
providers.InitAsynQClient()
|
||||||
|
providers.InitGin()
|
||||||
|
providers.InitApiRoutes()
|
||||||
|
|
||||||
|
StartHttp()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartHttp() {
|
||||||
|
if providers.Config.HTTP.ListenAddr == "" {
|
||||||
|
providers.Config.GRPC.GrpcListenAddr = "0.0.0.0:8080"
|
||||||
|
}
|
||||||
|
|
||||||
|
err := access.Router.Run(providers.Config.HTTP.ListenAddr)
|
||||||
|
if err != nil {
|
||||||
|
panic("failed to listen: " + err.Error())
|
||||||
|
}
|
||||||
|
providers.Logger.Info("Http Server listening at " + providers.Config.GRPC.GrpcListenAddr)
|
||||||
|
}
|
@ -13,12 +13,12 @@ var rootCmd = &cobra.Command{
|
|||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
rootCmd.AddCommand(rpcCommand)
|
rootCmd.AddCommand(rpcCommand)
|
||||||
//rootCmd.AddCommand(gormGenCommand)
|
|
||||||
rootCmd.AddCommand(migrateCommand)
|
rootCmd.AddCommand(migrateCommand)
|
||||||
rootCmd.AddCommand(workerCommand)
|
rootCmd.AddCommand(workerCommand)
|
||||||
rootCmd.AddCommand(scheduleCommand)
|
rootCmd.AddCommand(scheduleCommand)
|
||||||
rootCmd.AddCommand(dsnCommand)
|
rootCmd.AddCommand(dsnCommand)
|
||||||
rootCmd.AddCommand(createMigrateCommand)
|
rootCmd.AddCommand(createMigrateCommand)
|
||||||
|
rootCmd.AddCommand(httpCommand)
|
||||||
|
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
9
internal/controllers/user/main.go
Normal file
9
internal/controllers/user/main.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"framework_v2/internal/access"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CurrentUser(ctx *gin.Context, metadata *access.Metadata) {
|
||||||
|
}
|
@ -1 +1,49 @@
|
|||||||
package providers
|
package providers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"framework_v2/internal/access"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitGin() {
|
||||||
|
access.Router = gin.Default()
|
||||||
|
access.Router.Use(gin.Recovery())
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpMethod int
|
||||||
|
|
||||||
|
var httpMethodStr = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}
|
||||||
|
|
||||||
|
func (h httpMethod) String() string {
|
||||||
|
return httpMethodStr[h-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
GET httpMethod = iota + 1
|
||||||
|
POST
|
||||||
|
PUT
|
||||||
|
DELETE
|
||||||
|
PATCH
|
||||||
|
HEAD
|
||||||
|
OPTIONS
|
||||||
|
)
|
||||||
|
|
||||||
|
type HandlerFunc func(c *gin.Context, metadata *access.Metadata)
|
||||||
|
|
||||||
|
func HandleRoute(method httpMethod, relativePath string, handlers ...HandlerFunc) {
|
||||||
|
access.Router.Handle(method.String(), relativePath, func(c *gin.Context) {
|
||||||
|
handleWithMetadata(c, handlers...)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUser(c *gin.Context) *access.User {
|
||||||
|
return &access.User{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleWithMetadata(c *gin.Context, handlers ...HandlerFunc) {
|
||||||
|
var metadata = &access.Metadata{}
|
||||||
|
|
||||||
|
for _, handler := range handlers {
|
||||||
|
handler(c, metadata)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
7
internal/providers/routes.go
Normal file
7
internal/providers/routes.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package providers
|
||||||
|
|
||||||
|
import "framework_v2/internal/controllers/user"
|
||||||
|
|
||||||
|
func InitApiRoutes() {
|
||||||
|
HandleRoute(GET, "/", user.CurrentUser)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user