From 6a5c6931ebe949e22aada7d63497bcff1ac6c402 Mon Sep 17 00:00:00 2001 From: "marko.tikvic" Date: Wed, 1 Apr 2020 14:13:19 +0200 Subject: [PATCH] added database map to server --- document/document.go | 48 ++++-------------------------------------------- file_util.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- float_util.go | 10 ++++++++++ server.go | 4 ++++ 4 files changed, 62 insertions(+), 45 deletions(-) diff --git a/document/document.go b/document/document.go index daa0a89..fcbd64b 100644 --- a/document/document.go +++ b/document/document.go @@ -7,7 +7,6 @@ import ( "mime" "net/http" "os" - "strings" "time" web "git.to-net.rs/marko.tikvic/webutility" @@ -49,7 +48,7 @@ func OpenFileAsDocument(path string) (*Document, error) { d.FileName = stats.Name() d.Size = stats.Size() - d.Extension = FileExtension(d.FileName) + d.Extension = web.FileExtension(d.FileName) d.data = make([]byte, d.Size) if _, err = f.Read(d.data); err != nil { @@ -99,18 +98,6 @@ func ServeDocument(w http.ResponseWriter, d *Document, downloadPrompt bool) erro return nil } -// FileExists ... -func FileExists(path string) bool { - temp, err := os.Open(path) - defer temp.Close() - - if err != nil { - return false - } - - return true -} - // ParseDocument ... func ParseDocument(req *http.Request) (doc *Document, err error) { req.ParseMultipartForm(32 << 20) @@ -127,7 +114,7 @@ func ParseDocument(req *http.Request) (doc *Document, err error) { fsize := fheader.Size ftype := fmt.Sprintf("%v", fheader.Header["Content-Type"][0]) - fextn := FileExtension(fname) + fextn := web.FileExtension(fname) if fextn == "" { return doc, errors.New("invalid extension") } @@ -155,31 +142,16 @@ func ParseDocument(req *http.Request) (doc *Document, err error) { return doc, nil } -// DirectoryFromPath ... -func DirectoryFromPath(path string) (dir string) { - parts := strings.Split(path, "/") - if len(parts) == 1 { - return "" - } - - dir = parts[0] - for _, p := range parts[1 : len(parts)-1] { - dir += "/" + p - } - - return dir -} - // SaveToFile ... func (d *Document) SaveToFile(path string) (f *os.File, err error) { d.Path = path - if FileExists(path) { + if web.FileExists(path) { err = fmt.Errorf("file %s alredy exists", path) return nil, err } - if parentDir := DirectoryFromPath(path); parentDir != "" { + if parentDir := web.DirectoryFromPath(path); parentDir != "" { if err = os.MkdirAll(parentDir, os.ModePerm); err != nil { if !os.IsExist(err) { return nil, err @@ -201,10 +173,6 @@ func (d *Document) SaveToFile(path string) (f *os.File, err error) { return f, nil } -func DeleteFile(path string) error { - return os.Remove(path) -} - func DeleteDocuments(docs []*Document) error { for _, d := range docs { if err := d.DeleteFile(); err != nil { @@ -218,11 +186,3 @@ func DeleteDocuments(docs []*Document) error { func (d *Document) DeleteFile() error { return os.Remove(d.Path) } - -func FileExtension(path string) string { - parts := strings.Split(path, ".") // because name can contain dots - if len(parts) < 2 { - return "" - } - return "." + parts[len(parts)-1] -} diff --git a/file_util.go b/file_util.go index d6e4553..b9991b9 100644 --- a/file_util.go +++ b/file_util.go @@ -2,6 +2,7 @@ package webutility import ( "bytes" + "fmt" "io" "io/ioutil" "os" @@ -81,6 +82,48 @@ func ListDir(path string) (fnames []string, err error) { } func WorkingDir() string { - dir, _ := os.Getwd() + path, err := os.Getwd() + if err != nil { + fmt.Printf("couldn't get working directory: %s\n", err.Error()) + } + return path +} + +func FileExtension(path string) string { + parts := strings.Split(path, ".") // because name can contain dots + if len(parts) < 2 { + return "" + } + return "." + parts[len(parts)-1] +} + +func DeleteFile(path string) error { + return os.Remove(path) +} + +// DirectoryFromPath ... +func DirectoryFromPath(path string) (dir string) { + parts := strings.Split(path, "/") + if len(parts) == 1 { + return "" + } + + dir = parts[0] + for _, p := range parts[1 : len(parts)-1] { + dir += "/" + p + } + return dir } + +// FileExists ... +func FileExists(path string) bool { + temp, err := os.Open(path) + defer temp.Close() + + if err != nil { + return false + } + + return true +} diff --git a/float_util.go b/float_util.go index 999a59b..8167f52 100644 --- a/float_util.go +++ b/float_util.go @@ -82,3 +82,13 @@ func FormatFloat64Number(f float64, dec int) string { return res } + +func MaxFlaot64(vars ...float64) (max float64) { + max = vars[0] + for _, v := range vars { + if v > max { + max = v + } + } + return max +} diff --git a/server.go b/server.go index 7e309db..bbfb7ab 100644 --- a/server.go +++ b/server.go @@ -15,6 +15,7 @@ type Server struct { Logger *gologger.Logger Port string UTCOffset int64 + DBs map[string]*sql.DB } func NewODBCServer(dsn, port, logDir string, utcOffset int64) (s *Server, err error) { @@ -34,6 +35,9 @@ func NewODBCServer(dsn, port, logDir string, utcOffset int64) (s *Server, err er s.UTCOffset = utcOffset + s.DBs = make(map[string]*sql.DB) + s.DBs["default"] = s.DB + return s, nil } -- 1.8.1.2