server for tests

This commit is contained in:
Andreas Schröpfer
2021-03-01 21:33:43 +01:00
parent cef519e3b6
commit cd7ee015a9
3 changed files with 78 additions and 1 deletions

View File

@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<form
enctype="multipart/form-data"
action="/convert/"
method="post"
>
<input type="file" name="myFile" /><br><br>
<input type="submit" value="upload" />
</form>
</body>
</html>

58
cmd/server/main.go Normal file
View File

@@ -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<br>: %s", err)
}
eBalance.EncodeJSON(w)
}

2
go.mod
View File

@@ -5,5 +5,5 @@ go 1.16
require ( require (
github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2 github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/matryer/is v1.4.0 // indirect github.com/matryer/is v1.4.0
) )