-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cda9244
commit 852cf82
Showing
80 changed files
with
6,163 additions
and
1,844 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package apis | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"runtime/debug" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func (api *API) Uploader() *gin.Engine { | ||
r := gin.Default() | ||
r.Use(func(ctx *gin.Context) { | ||
defer func() { | ||
err := recover() | ||
if err == nil { | ||
return | ||
} | ||
|
||
method := ctx.Request.Method | ||
path := ctx.Request.URL.Path | ||
status := 500 | ||
remoteAddr := ctx.Request.RemoteAddr | ||
clientIP := ctx.ClientIP() | ||
|
||
var e error | ||
switch v := err.(type) { | ||
case error: | ||
e = v | ||
default: | ||
e = fmt.Errorf("%v", v) | ||
} | ||
|
||
logrus.WithContext(ctx). | ||
WithError(e).WithField("stack", string(debug.Stack())). | ||
Errorf( | ||
"panic recover: method= %s path= %s status= %d remote_addr= %s client_ip= %s", | ||
method, path, status, remoteAddr, clientIP, | ||
) | ||
|
||
reason := e.Error() | ||
ctx.JSON(status, gin.H{"reason": reason}) | ||
ctx.Abort() | ||
}() | ||
|
||
ctx.Next() | ||
}) | ||
r.GET("/ping", func(ctx *gin.Context) { | ||
ctx.JSON(http.StatusOK, gin.H{"result": "pong"}) | ||
}) | ||
r.POST("/library/_import", func(ctx *gin.Context) { | ||
logrus.WithContext(ctx).Infof("get library import request, %t %t", ctx == nil, ctx.Request == nil) | ||
|
||
defer ctx.Request.Body.Close() | ||
buf, err := io.ReadAll(ctx.Request.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := api.lib.Import(ctx, buf); err != nil { | ||
panic(err) | ||
} | ||
|
||
ctx.JSON(http.StatusOK, gin.H{"result": "ok"}) | ||
}) | ||
|
||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package apis | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/abc950309/tapewriter/entity" | ||
) | ||
|
||
func (api *API) JobDelete(ctx context.Context, req *entity.JobDeleteRequest) (*entity.JobDeleteReply, error) { | ||
if err := api.exe.DeleteJobs(ctx, req.Ids...); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &entity.JobDeleteReply{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package apis | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/abc950309/tapewriter/entity" | ||
) | ||
|
||
func (api *API) LibraryExport(ctx context.Context, req *entity.LibraryExportRequest) (*entity.LibraryExportReply, error) { | ||
buf, err := api.lib.Export(ctx, req.Types) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &entity.LibraryExportReply{Json: buf}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package apis | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/abc950309/tapewriter/entity" | ||
) | ||
|
||
func (api *API) TapeDelete(ctx context.Context, req *entity.TapeDeleteRequest) (*entity.TapeDeleteReply, error) { | ||
if err := api.lib.DeleteTapes(ctx, req.Ids...); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &entity.TapeDeleteReply{}, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package apis | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/abc950309/tapewriter/entity" | ||
"github.com/abc950309/tapewriter/library" | ||
"github.com/samber/lo" | ||
) | ||
|
||
func (api *API) TapeList(ctx context.Context, req *entity.TapeListRequest) (*entity.TapeListReply, error) { | ||
tapes, err := func() ([]*library.Tape, error) { | ||
switch v := req.GetParam().(type) { | ||
case *entity.TapeListRequest_List: | ||
return api.lib.ListTape(ctx, v.List) | ||
case *entity.TapeListRequest_Mget: | ||
m, err := api.lib.MGetTape(ctx, v.Mget.GetIds()...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return lo.Values(m), nil | ||
default: | ||
return nil, fmt.Errorf("unexpected list tape param, %T", req.GetParam()) | ||
} | ||
}() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
converted := make([]*entity.Tape, 0, len(tapes)) | ||
for _, tape := range tapes { | ||
converted = append(converted, &entity.Tape{ | ||
Id: tape.ID, | ||
Barcode: tape.Barcode, | ||
Name: tape.Name, | ||
Encryption: tape.Encryption, | ||
CreateTime: tape.CreateTime.Unix(), | ||
DestroyTime: convertOptionalTime(tape.DestroyTime), | ||
CapacityBytes: tape.CapacityBytes, | ||
WritenBytes: tape.WritenBytes, | ||
}) | ||
} | ||
|
||
return &entity.TapeListReply{Tapes: converted}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.