Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(api): register router #446

Open
wants to merge 6 commits into
base: golang
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
6 changes: 2 additions & 4 deletions api/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func Logger() gin.HandlerFunc {
path := c.Request.URL.Path

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

c.Set("logger", log)
c.Set("request_id", requestID)

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

log.Infow("router info", zap.Any("payload", param))
logger.Infow("router info", zap.String("request_id", requestID), zap.Any("payload", param))

}
}
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
}
27 changes: 27 additions & 0 deletions api/v1/cloud_storage/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cloudstorage

import (
"github.com/gin-gonic/gin"
transportV1 "github.com/netless-io/flat-server/api/v1/transport"
"github.com/netless-io/flat-server/services"
)

func HandleList(gCtx *gin.Context) {

resp := transportV1.NewResp(gCtx)
defer resp.JSON()

// userUUID:=resp.GetUserUUID()
ctx := resp.GetContext()

// TODO test
userUUID := gCtx.Query("user_id")

result, err := services.CloudStorageUserFileList(ctx, userUUID, 1, 50)
if err != nil {
resp.Err = err
return
}

resp.Data = result
}
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("/weChat/web/callback", HandleWechatWebCallback)
loginRoutes.GET("/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")

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

import (
"context"
"net/http"

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

type Status int

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

type Response struct {
gCtx *gin.Context
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 {
userUUID, loginSource := getUserIDWithLoginSource(gCtx)

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

func (resp *Response) GetUserUUID() string {
return resp.userUUID
}

func (resp *Response) GetLoginSource() string {
return resp.loginSource
}

func (resp *Response) GetContext() context.Context {

ctx := context.Background()

requestID, exists := resp.gCtx.Get("request_id")
if exists {
ctx = context.WithValue(ctx, "request_id", requestID)
return ctx
}

return ctx
}

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)

if resp.Err == nil {
return
}

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)
}
}
Loading