From cd7ee015a9e4a0207de55a8040de7a415926c6b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Schr=C3=B6pfer?= Date: Mon, 1 Mar 2021 21:33:43 +0100 Subject: [PATCH] server for tests --- cmd/server/files/upload.htm | 19 ++++++++++++ cmd/server/main.go | 58 +++++++++++++++++++++++++++++++++++++ go.mod | 2 +- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 cmd/server/files/upload.htm create mode 100644 cmd/server/main.go diff --git a/cmd/server/files/upload.htm b/cmd/server/files/upload.htm new file mode 100644 index 0000000..5c82d5b --- /dev/null +++ b/cmd/server/files/upload.htm @@ -0,0 +1,19 @@ + + + + + + + Document + + +
+

+ +
+ + \ No newline at end of file diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..55c5d33 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "bytes" + _ "embed" + "fmt" + "io" + "log" + "net/http" + "time" + + "git.ecogood.org/andreas.schroepfer/excelConverter/pkg/loader" +) + +//go:embed files/upload.htm +var homepage []byte + +func main() { + run() +} + +func run() { + mux := http.NewServeMux() + mux.HandleFunc("/convert/", handleConvert) + mux.HandleFunc("/test/", handleRoot) + mux.HandleFunc("/", handleRoot) + s := &http.Server{ + Addr: ":2727", + Handler: mux, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + MaxHeaderBytes: 1 << 20, + } + fmt.Println("starting server at:") + fmt.Println("http://localhost:2727/") + log.Fatal(s.ListenAndServe()) +} + +func handleRoot(w http.ResponseWriter, r *http.Request) { + buf := bytes.NewBuffer(homepage) + io.Copy(w, buf) +} + +func handleConvert(w http.ResponseWriter, r *http.Request) { + r.ParseMultipartForm(10 << 20) + file, _, err := r.FormFile("myFile") + if err != nil { + fmt.Println("Error Retrieving the File") + fmt.Println(err) + return + } + defer file.Close() + eBalance, err := loader.XLSX(file, nil) + if err != nil { + fmt.Fprintf(w, "convert errors
: %s", err) + } + eBalance.EncodeJSON(w) +} diff --git a/go.mod b/go.mod index 359a147..70b8959 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,5 @@ go 1.16 require ( github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2 github.com/davecgh/go-spew v1.1.1 // indirect - github.com/matryer/is v1.4.0 // indirect + github.com/matryer/is v1.4.0 )