jenkins setup

This commit is contained in:
simon987
2019-03-09 09:31:19 -05:00
parent 6048cfbebc
commit fea86af41d
7 changed files with 128 additions and 48 deletions

View File

@@ -12,7 +12,7 @@ import (
"path/filepath"
)
var WorkDir, _ = filepath.Abs("./data/")
var WorkDir = getWorkDir()
type Info struct {
Name string `json:"name"`
@@ -67,6 +67,10 @@ func LogRequestMiddleware(h fasthttp.RequestHandler) fasthttp.RequestHandler {
func New(db *gorm.DB) *WebApi {
if _, err := os.Stat(WorkDir); err != nil && os.IsNotExist(err) {
_ = os.Mkdir(WorkDir, 0700)
}
api := &WebApi{}
logrus.SetLevel(getLogLevel())
@@ -134,3 +138,14 @@ func getLogLevel() logrus.Level {
return level
}
}
func getWorkDir() string {
workDir := os.Getenv("WS_BUCKET_WORKDIR")
if workDir == "" {
path, _ := filepath.Abs("./data")
return path
} else {
path, _ := filepath.Abs(workDir)
return path
}
}

View File

@@ -1,6 +1,7 @@
package api
import (
"fmt"
"path/filepath"
"strings"
)
@@ -49,6 +50,8 @@ func (req *AllocateUploadSlotRequest) IsValid() bool {
return false
}
if !strings.HasPrefix(pathAbs, WorkDir) {
fmt.Println(pathAbs)
fmt.Println(WorkDir)
return false
}

View File

@@ -25,7 +25,7 @@ func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) {
req := &AllocateUploadSlotRequest{}
err := json.Unmarshal(ctx.Request.Body(), req)
if err != nil {
ctx.Response.SetStatusCode(400)
ctx.Response.Header.SetStatusCode(400)
Json(GenericResponse{
Ok: false,
}, ctx)
@@ -33,7 +33,7 @@ func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) {
}
if !req.IsValid() {
ctx.Response.SetStatusCode(400)
ctx.Response.Header.SetStatusCode(400)
Json(CreateClientResponse{
Ok: false,
}, ctx)
@@ -54,7 +54,7 @@ func (api *WebApi) Upload(ctx *fasthttp.RequestCtx) {
err := api.db.Where("token=?", token).First(&slot).Error
if err != nil {
ctx.Response.Header.SetStatusCode(400)
logrus.WithFields(logrus.Fields{
logrus.WithError(err).WithFields(logrus.Fields{
"token": token,
}).Warning("Upload slot not found")
return
@@ -67,11 +67,6 @@ func (api *WebApi) Upload(ctx *fasthttp.RequestCtx) {
err = upgrader.Upgrade(ctx, func(ws *websocket.Conn) {
defer ws.Close()
err := ws.WritePreparedMessage(api.MotdMessage)
if err != nil {
panic(err)
}
mt, reader, err := ws.NextReader()
if err != nil {
panic(err)
@@ -103,6 +98,8 @@ func (api *WebApi) Upload(ctx *fasthttp.RequestCtx) {
_, _ = fp.Write(buf[:toWrite])
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
totalRead += int64(read)
}
@@ -130,7 +127,7 @@ func (api *WebApi) ReadUploadSlot(ctx *fasthttp.RequestCtx) {
if err != nil {
ctx.Response.Header.SetStatusCode(404)
logrus.WithFields(logrus.Fields{
logrus.WithError(err).WithFields(logrus.Fields{
"token": tokenStr,
}).Warning("Upload slot not found")
return
@@ -161,6 +158,10 @@ func (api *WebApi) ReadUploadSlot(ctx *fasthttp.RequestCtx) {
panic(err)
}
}
err = fp.Close()
if err != nil {
panic(err)
}
mu.(*sync.RWMutex).RUnlock()
}