Skip to content

Commit

Permalink
mody: go mod
Browse files Browse the repository at this point in the history
refine the code

update: make check

motify api

modify return values

rename
  • Loading branch information
huaxiabuluo authored and veeding committed May 18, 2022
1 parent 8aa2aa6 commit a0deae0
Show file tree
Hide file tree
Showing 31 changed files with 362 additions and 364 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ dist/
!app/**/*.js
tmp
server/data
server-v2/api/*/data
assets/
bin/
5 changes: 4 additions & 1 deletion server-v2/api/studio/etc/studio-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ Auth:
AccessSecret: "login_secret"
AccessExpire: 1800
File:
UploadDir: "./upload/"
UploadDir: "./data/upload/"
TasksDir: "./data/tasks"
SqliteDbFilePath: "./data/tasks.db"
TaskIdPath: "./data/taskId.data"
29 changes: 29 additions & 0 deletions server-v2/api/studio/internal/common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package common

import (
"net/http"
"regexp"
"strings"
)

func ReserveRequest(r *http.Request) bool {
if strings.HasPrefix(r.URL.Path, "/api/files") {
return true
}
if strings.HasPrefix(r.URL.Path, "/api/import-tasks") {
return true
}
return false
}

func ReserveResponse(r *http.Request) bool {
if strings.HasPrefix(r.URL.Path, "/api/import-tasks") {
return true
}
return false
}

func IgnoreHandlerBody(r *http.Request) bool {
pattern := regexp.MustCompile(`/api/import-tasks/\d/download.*`)
return pattern.MatchString(r.URL.Path)
}
77 changes: 76 additions & 1 deletion server-v2/api/studio/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package config

import (
"io/ioutil"
"os"
"path/filepath"

"github.com/zeromicro/go-zero/rest"
"go.uber.org/zap"
)

type Config struct {
Expand All @@ -15,6 +20,76 @@ type Config struct {
}

File struct {
UploadDir string
UploadDir string
TasksDir string
SqliteDbFilePath string
TaskIdPath string
}
}

const (
DefaultFilesDataDir = "data"
DefaultTaskIdPath = "data/taskId.data"
DefaultUploadDir = "data/upload"
DefaultTasksDir = "data/tasks"
DefaultSqlitedbFilePath = "data/tasks.db"
)

func (c *Config) Validate() error {
return nil
}

func (c *Config) Complete() {
if c.File.TaskIdPath == "" {
_, err := os.Stat(DefaultFilesDataDir)
if os.IsNotExist(err) {
os.MkdirAll(DefaultFilesDataDir, 0o766)
}
abs, _ := filepath.Abs(DefaultTaskIdPath)
_, err = ioutil.ReadFile(abs)
if err != nil {
if os.IsNotExist(err) {
_, err := os.Create(abs)
if err != nil {
zap.L().Fatal("DefaultTaskIdPath Init fail", zap.Error(err))
} else {
zap.L().Fatal("DefaultTaskIdPath Init fail", zap.Error(err))
}
}
}
c.File.TaskIdPath = abs
}

if c.File.UploadDir == "" {
abs, _ := filepath.Abs(DefaultTasksDir)
c.File.UploadDir = abs
_, err := os.Stat(abs)
if os.IsNotExist(err) {
os.MkdirAll(abs, 0o776)
}
}

if c.File.TasksDir == "" {
abs, _ := filepath.Abs(DefaultTasksDir)
c.File.TasksDir = abs
_, err := os.Stat(abs)
if os.IsNotExist(err) {
os.MkdirAll(abs, 0o766)
}
}

if c.File.SqliteDbFilePath == "" {
_, err := os.Stat(DefaultFilesDataDir)
if os.IsNotExist(err) {
os.MkdirAll(DefaultFilesDataDir, 0o766)
}
abs, _ := filepath.Abs(DefaultSqlitedbFilePath)
c.File.SqliteDbFilePath = abs
}
}

func (c *Config) InitConfig() error {
c.Complete()

return c.Validate()
}
12 changes: 6 additions & 6 deletions server-v2/api/studio/internal/handler/routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ func NewFileDestroyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileD
}

func (l *FileDestroyLogic) FileDestroy(req types.FileDestroyRequest) error {
return service.NewFileService(nil, l.ctx, l.svcCtx).FileDestroy(req.Name)
return service.NewFileService(l.ctx, l.svcCtx).FileDestroy(req.Name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ func NewFilesIndexLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FilesI
}

func (l *FilesIndexLogic) FilesIndex() (resp *types.FilesIndexData, err error) {
return service.NewFileService(nil, l.ctx, l.svcCtx).FilesIndex()
return service.NewFileService(l.ctx, l.svcCtx).FilesIndex()
}
12 changes: 6 additions & 6 deletions server-v2/api/studio/internal/logic/file/fileuploadlogic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package file

import (
"net/http"
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
Expand All @@ -10,18 +10,18 @@ import (

type FileUploadLogic struct {
logx.Logger
r *http.Request
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewFileUploadLogic(r *http.Request, svcCtx *svc.ServiceContext) *FileUploadLogic {
func NewFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileUploadLogic {
return &FileUploadLogic{
Logger: logx.WithContext(r.Context()),
r: r,
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *FileUploadLogic) FileUpload() error {
return service.NewFileService(l.r, nil, l.svcCtx).FileUpload()
return service.NewFileService(l.ctx, l.svcCtx).FileUpload()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importtask

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importtask

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importtask

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importtask

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importtask

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importtask

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importtask

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/types"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importtask

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package importtask

import (
"context"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/service"

"github.com/vesoft-inc/nebula-studio/server/api/studio/internal/svc"
Expand Down
20 changes: 2 additions & 18 deletions server-v2/api/studio/internal/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,6 @@ type (
}
)

func NewFileService(r *http.Request, ctx context.Context, svcCtx *svc.ServiceContext) FileService {
if r != nil {
return &fileService{
Logger: logx.WithContext(r.Context()),
r: r,
svcCtx: svcCtx,
}
} else {
return &fileService{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
}

func NewFileService(ctx context.Context, svcCtx *svc.ServiceContext) FileService {
return &fileService{
Logger: logx.WithContext(ctx),
Expand Down Expand Up @@ -238,7 +222,7 @@ func checkCharset(file *multipart.FileHeader) (string, error) {
func changeFileCharset2UTF8(filePath string, charSet string) error {
fileUTF8Path := filePath + "-copy"
err := func() error {
file, err := os.OpenFile(filePath, os.O_RDONLY, 0666)
file, err := os.OpenFile(filePath, os.O_RDONLY, 0o666)
if err != nil {
zap.L().Warn("open file fail", zap.Error(err))
return err
Expand All @@ -251,7 +235,7 @@ func changeFileCharset2UTF8(filePath string, charSet string) error {
return noCharsetErr
}
decodeReader := decoder.NewReader(reader)
fileUTF8, err := os.OpenFile(fileUTF8Path, os.O_RDONLY|os.O_CREATE|os.O_WRONLY, 0666)
fileUTF8, err := os.OpenFile(fileUTF8Path, os.O_RDONLY|os.O_CREATE|os.O_WRONLY, 0o666)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit a0deae0

Please sign in to comment.