Skip to content

Commit

Permalink
chore(api): register route
Browse files Browse the repository at this point in the history
  • Loading branch information
berryhe committed Dec 13, 2021
1 parent 45ba668 commit 757ed18
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 4 deletions.
4 changes: 4 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"github.com/gin-gonic/gin"
"github.com/netless-io/flat-server/api/middleware"

apiV1 "github.com/netless-io/flat-server/api/v1"
)

func New(env string) *gin.Engine {
Expand All @@ -18,5 +20,7 @@ func New(env string) *gin.Engine {

app.Use(middleware.Logger())

apiV1.RegisterAPIv1Routes(app)

return app
}
12 changes: 8 additions & 4 deletions api/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ func Logger() gin.HandlerFunc {
start := time.Now()
path := c.Request.URL.Path

requestID := strings.ReplaceAll(uuid.New().String(), "-", "")
log := logger.NewTraceLog(requestID)
traceLog := NewTraceLog()

c.Set("logger", log)
c.Set("logger", traceLog)

// Process request
c.Next()
Expand All @@ -61,7 +60,12 @@ func Logger() gin.HandlerFunc {
param := make(map[string]PayLoad)
param[path] = payload

log.Infow("router info", zap.Any("payload", param))
traceLog.Infow("router info", zap.Any("payload", param))

}
}

func NewTraceLog() *logger.TraceLog {
requestID := strings.ReplaceAll(uuid.New().String(), "-", "")
return logger.NewTraceLog(requestID)
}
22 changes: 22 additions & 0 deletions api/v1/agora/agora.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package agora

import (
"github.com/gin-gonic/gin"
)

func RegisterRoutes(agoraGroup *gin.RouterGroup) {

agoraRoute := agoraGroup.Group("/agora")
{
agoraRoute.POST("/token/generate/rtc", HandleGenRTCToken)
agoraRoute.POST("/token/generate/rtm", HandleGenRTMToken)
}
}

func HandleGenRTCToken(gCtx *gin.Context) {
// TODO code
}

func HandleGenRTMToken(gCtx *gin.Context) {
// TODO code
}
20 changes: 20 additions & 0 deletions api/v1/cloud_storage/alibaba_cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cloudstorage

import (
"github.com/gin-gonic/gin"
)

func HandleAlibabaCloudRemove(gCtx *gin.Context) {
// TODO code
}
func HandleAlibabaCloudRename(gCtx *gin.Context) {
// TODO code
}

func HandleAlibabaCloudUploadStart(gCtx *gin.Context) {
// TODO code
}

func HandleAlibabaCloudUploadFinish(gCtx *gin.Context) {
// TODO code
}
26 changes: 26 additions & 0 deletions api/v1/cloud_storage/cloud_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cloudstorage

import "github.com/gin-gonic/gin"

func RegisterRoutes(storageGroup *gin.RouterGroup) {

storageRoute := storageGroup.Group("/cloud-storage")
{
storageRoute.POST("/url-cloud/add", HandleUrlCloudAdd)
storageRoute.POST("/url-cloud/remove", HandleUrlCloudRemove)
storageRoute.POST("/url-cloud/rename", HandleUrlCloudRename)

storageRoute.POST("/upload/cancel", HandleUploadCancel)
storageRoute.POST("/list", HandleList)

storageRoute.POST("/convert/start", handleConvertStart)
storageRoute.POST("/convert/finish", handleConvertFinish)

storageRoute.POST("/alibaba-cloud/upload/start", HandleAlibabaCloudUploadStart)
storageRoute.POST("/alibaba-cloud/upload/finish", HandleAlibabaCloudUploadFinish)

storageRoute.POST("/alibaba-cloud/remove", HandleAlibabaCloudRemove)
storageRoute.POST("/alibaba-cloud/rename", HandleAlibabaCloudRename)

}
}
13 changes: 13 additions & 0 deletions api/v1/cloud_storage/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cloudstorage

import (
"github.com/gin-gonic/gin"
)

func handleConvertStart(gCtx *gin.Context) {
// TODO code
}

func handleConvertFinish(gCtx *gin.Context) {
// TODO code
}
7 changes: 7 additions & 0 deletions api/v1/cloud_storage/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cloudstorage

import "github.com/gin-gonic/gin"

func HandleList(gCtx *gin.Context) {
// TODO code
}
7 changes: 7 additions & 0 deletions api/v1/cloud_storage/upload_cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cloudstorage

import "github.com/gin-gonic/gin"

func HandleUploadCancel(gCtx *gin.Context) {
// TODO code
}
15 changes: 15 additions & 0 deletions api/v1/cloud_storage/url_cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cloudstorage

import "github.com/gin-gonic/gin"

func HandleUrlCloudAdd(gCtx *gin.Context) {
// TODO code
}

func HandleUrlCloudRemove(gCtx *gin.Context) {
// TODO code
}

func HandleUrlCloudRename(gCtx *gin.Context) {
// TODO code
}
7 changes: 7 additions & 0 deletions api/v1/login/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package login

import "github.com/gin-gonic/gin"

func HandleGithubCallback(gCtx *gin.Context) {
// TODO code
}
13 changes: 13 additions & 0 deletions api/v1/login/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package login

import "github.com/gin-gonic/gin"

func RegisterRoutes(loginGroup *gin.RouterGroup) {
loginRoutes := loginGroup.Group("/login")
{
loginRoutes.GET("/github/callback", HandleGithubCallback)

loginRoutes.GET("login/weChat/web/callback", HandleWechatWebCallback)
loginRoutes.GET("login/weChat/mobile/callback", HandleWechatMobileCallback)
}
}
13 changes: 13 additions & 0 deletions api/v1/login/wechat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package login

import (
"github.com/gin-gonic/gin"
)

func HandleWechatMobileCallback(gCtx *gin.Context) {

}

func HandleWechatWebCallback(gCtx *gin.Context) {

}
28 changes: 28 additions & 0 deletions api/v1/room/room.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package room

import "github.com/gin-gonic/gin"

func RegisterRoutes(roomGroup *gin.RouterGroup) {
// TODO register route. handle func
roomRoute := roomGroup.Group("/room")
{
roomRoute.POST("/cancel/history")
roomRoute.POST("/cancel/ordinary")
roomRoute.POST("/cancel/periodic")
roomRoute.POST("/cancel/periodic-sub-room")

roomRoute.POST("/create/ordinary")
roomRoute.POST("/create/periodic")

roomRoute.POST("/info/ordinary")
roomRoute.POST("/info/periodic")
roomRoute.POST("/info/periodic-sub-room")
roomRoute.POST("/info/users")

roomRoute.POST("/join")
roomRoute.POST("/list/:type")

roomRoute.POST("/record/info")

}
}
111 changes: 111 additions & 0 deletions api/v1/transport/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package transport

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/netless-io/flat-server/api/middleware"
"github.com/netless-io/flat-server/errors"
"github.com/netless-io/flat-server/logger"
)

type Status int

const (
NoLogin Status = -1
Success = iota - 1
Failed
Process
AuthFailed
NotMethod
NoRoute
)

type Response struct {
gCtx *gin.Context
traceLogger logger.Logger
httpCode int
userUUID string
loginSource string

Err error `json:"-"`
Status int `json:"status"`
Code int `json:"code,omitempty"`
Data interface{} `json:"data,omitempty"`
}

func HandleNoMethod(gCtx *gin.Context) {
resp := new(Response)
resp.Status = NotMethod
defer resp.JSONWithHTTPCode(http.StatusMethodNotAllowed)
}

func HandleNoRoute(gCtx *gin.Context) {
resp := new(Response)
resp.Status = NoRoute
defer resp.JSONWithHTTPCode(http.StatusNotFound)
}

func NewResp(gCtx *gin.Context) *Response {
traceLogger := getLogger(gCtx)
userUUID, loginSource := getUserIDWithLoginSource(gCtx)

return &Response{
gCtx: gCtx,
userUUID: userUUID,
loginSource: loginSource,
traceLogger: traceLogger,
httpCode: http.StatusOK,
}
}

func getLogger(gCtx *gin.Context) logger.Logger {
traceLog, exists := gCtx.Get("logger")
if !exists {
return middleware.NewTraceLog()
}

traceLogger, ok := traceLog.(*logger.TraceLog)
if !ok {
return middleware.NewTraceLog()
}

return traceLogger
}

func getUserIDWithLoginSource(gCtx *gin.Context) (string, string) {
userAuth, exists := gCtx.Get("user_auth")
if !exists {
return "", ""
}

if userPayLoad, ok := userAuth.(middleware.UserPayLoad); ok {
return userPayLoad.UserID, userPayLoad.LoginSource
}

return "", ""

}

func (resp *Response) JSONWithHTTPCode(httpCode int) {
resp.httpCode = httpCode
resp.JSON()
}

func (resp *Response) JSON() {
defer resp.gCtx.JSON(resp.httpCode, resp)

errCode, ok := resp.Err.(errors.Code)
if !ok {
resp.httpCode = http.StatusInternalServerError
errCode = errors.ServerFail
resp.Status = int(Failed)
resp.Data = nil
return
}

if resp.Code = errCode.Code(); resp.Code != 0 {
resp.Data = nil
}

}
23 changes: 23 additions & 0 deletions api/v1/v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v1

import (
"github.com/gin-gonic/gin"
"github.com/netless-io/flat-server/api/v1/agora"
cloudstorage "github.com/netless-io/flat-server/api/v1/cloud_storage"
"github.com/netless-io/flat-server/api/v1/login"
"github.com/netless-io/flat-server/api/v1/room"
transportV1 "github.com/netless-io/flat-server/api/v1/transport"
)

func RegisterAPIv1Routes(v1Group *gin.Engine) {
v1Group.NoMethod(transportV1.HandleNoMethod)
v1Group.NoRoute(transportV1.HandleNoRoute)

apiV1 := v1Group.Group("/v1")
{
agora.RegisterRoutes(apiV1)
cloudstorage.RegisterRoutes(apiV1)
room.RegisterRoutes(apiV1)
login.RegisterRoutes(apiV1)
}
}

0 comments on commit 757ed18

Please sign in to comment.