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

Filemanager #21

Closed
wants to merge 3 commits into from
Closed
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
39 changes: 39 additions & 0 deletions paddlecloud/go/file_manager/pfscommon/pfstrace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package pfscommon

import (
"fmt"
"runtime"
)

// MyCaller returns the caller of the function that called it :)
func CallerFunc() string {

// we get the callers as uintptrs - but we just need 1
fpcs := make([]uintptr, 1)

// skip 3 levels to get to the caller of whoever called Caller()
n := runtime.Callers(3, fpcs)
if n == 0 {
return "n/a" // proper error her would be better
}

// get the info of the actual function that's in the pointer
fun := runtime.FuncForPC(fpcs[0] - 1)
if fun == nil {
return "n/a"
}

// return its name
return fun.Name()
}

func CallerFileLine() string {
_, fileName, fileLine, ok := runtime.Caller(2)
var s string
if ok {
s = fmt.Sprintf("%s:%d", fileName, fileLine)
} else {
s = ""
}
return s
}
1 change: 1 addition & 0 deletions paddlecloud/go/file_manager/pfsmodules/chunks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package pfsmodules
34 changes: 34 additions & 0 deletions paddlecloud/go/file_manager/pfsmodules/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pfsmodules

type GetFilesReq struct {
Method string `json:"Method"`
Options []string `json:"Options"`
FilesPath []string `json:"FilesPath"`
}

type FileMeta struct {
Err string `json:"Err"`
Path string `json:"Path"`
ModTime string `json:"ModTime"`
Size int64 `json:"Size"`
}

type GetFilesResponse struct {
Err string `json:"Err"`
Metas []FileMeta `json:"Metas"`
TotalObjects int `json:"TotalObjects"`
TotalSize int64 `json:"TotalSize"`
}

func NewGetFilesResponse() *GetFilesResponse {
return &GetFilesResponse{
Err: "",
Metas: make([]FileMeta, 10),
TotalObjects: 0,
TotalSize: 0,
}
}

func (o *GetFilesResponse) SetErr(err string) {
o.Err = err
}
1 change: 1 addition & 0 deletions paddlecloud/go/file_manager/pfsmodules/storage_files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package pfsmodules
150 changes: 150 additions & 0 deletions paddlecloud/go/file_manager/pfsserver/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package pfsserver

import (
"encoding/json"
//"fmt"
"github.com/cloud/paddlecloud/go/file_manager/pfscommon"
"github.com/cloud/paddlecloud/go/file_manager/pfsmodules"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
//"strconv"
//"github.com/gorilla/mux"
)

/*
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Welcome!\n")
}

func GetFiles(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(todos); err != nil {
panic(err)
}
}

func TodoShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var todoId int
var err error
if todoId, err = strconv.Atoi(vars["todoId"]); err != nil {
panic(err)
}
todo := RepoFindTodo(todoId)
if todo.Id > 0 {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(todo); err != nil {
panic(err)
}
return
}

// If we didn't find it, 404
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusNotFound)
if err := json.NewEncoder(w).Encode(jsonErr{Code: http.StatusNotFound, Text: "Not Found"}); err != nil {
panic(err)
}

}
*/

func makeResponse(w http.ResponseWriter,
rep pfsmodules.GetFilesResponse,
status int) {

log.SetFlags(log.LstdFlags)

if len(rep.Err) > 0 {
log.Printf("%s error:%s\n", pfscommon.CallerFileLine(), rep.Err)
}

log.SetFlags(log.LstdFlags | log.Lshortfile)

w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(status)

if err := json.NewEncoder(w).Encode(rep); err != nil {
log.Printf("encode err:%s\n", err.Error())
panic(err)
}

}

func lsFiles(paths []string, r bool) ([]pfsmodules.FileMeta, error) {

ret := make([]pfsmodules.FileMeta, 0, 100)

for _, t := range paths {

list, err := filepath.Glob(t)
if err != nil {
log.Printf("glob path:%s error:%s", t, err)
return nil, err
}

for _, v := range list {
filepath.Walk(v, func(path string, info os.FileInfo, err error) error {
if info.IsDir() && r {
return filepath.SkipDir
}
m := pfsmodules.FileMeta{}
m.Path = info.Name()
m.Size = info.Size()
m.ModTime = info.ModTime().Format("2006-01-02 15:04:05")

ret = append(ret, m)
return nil
})
}
}

return ret, nil
}

func GetFiles(w http.ResponseWriter, r *http.Request) {
var req pfsmodules.GetFilesReq
rep := pfsmodules.GetFilesResponse{}

body, err := ioutil.ReadAll(io.LimitReader(r.Body, 2048))
if err != nil {
panic(err)
}

if err := r.Body.Close(); err != nil {
panic(err)
}

if err := json.Unmarshal(body, &req); err != nil {
rep.SetErr(err.Error())
makeResponse(w, rep, 422)
return
}

if req.Method != "ls" {
rep.SetErr("Not surported method:" + req.Method)
makeResponse(w, rep, 422)
return
}

//t := RepoCreateTodo(todo)
log.Print(req)
t, err := lsFiles(req.FilesPath, false)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(t); err != nil {
panic(err)
}
}

func CreateFiles(w http.ResponseWriter, r *http.Request) {
}

func PatchFiles(w http.ResponseWriter, r *http.Request) {
}
23 changes: 23 additions & 0 deletions paddlecloud/go/file_manager/pfsserver/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pfsserver

import (
"log"
"net/http"
"time"
)

func Logger(inner http.Handler, name string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()

inner.ServeHTTP(w, r)

log.Printf(
"%s\t%s\t%s\t%s",
r.Method,
r.RequestURI,
name,
time.Since(start),
)
})
}
23 changes: 23 additions & 0 deletions paddlecloud/go/file_manager/pfsserver/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"flag"
"fmt"
"github.com/cloud/paddlecloud/go/file_manager/pfsserver"
"log"
"net/http"
)

func main() {

router := pfsserver.NewRouter()

portPtr := flag.Int("port", 8080, "listen port")
flag.Parse()

addr := fmt.Sprintf("0.0.0.0:%d", *portPtr)

log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Printf("server on:%s\n", addr)
log.Fatal(http.ListenAndServe(addr, router))
}
26 changes: 26 additions & 0 deletions paddlecloud/go/file_manager/pfsserver/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pfsserver

import (
"github.com/gorilla/mux"
"net/http"
)

func NewRouter() *mux.Router {

router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler

handler = route.HandlerFunc
handler = Logger(handler, route.Name)

router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)

}

return router
}
27 changes: 27 additions & 0 deletions paddlecloud/go/file_manager/pfsserver/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pfsserver

import "net/http"

type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}

type Routes []Route

var routes = Routes{
Route{
"GetFiles",
"GET",
"/api/v1/files",
GetFiles,
},
Route{
"PostFiles",
"Post",
"/api/v1/files",
CreateFiles,
},
}