Commit 0f45d4a9 authored by CedricPump's avatar CedricPump
Browse files

added Http server, file upload and unzip

parent de1b05f0
<!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="http://localhost:8000/tsp"
method="post"
>
<input type="file" name="tspspec" />
<input type="submit" value="upload" />
</form>
</body>
</html>
\ No newline at end of file
package main
import (
"compress/gzip"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"pvs_tsp/tspreader"
"strings"
)
func main() {
spec, e := tspreader.Read("./data/swiss42.tsp")
startServer(8000)
}
func startServer(port int) {
r := mux.NewRouter()
r.HandleFunc("/readtsp", postTsp).Methods("POST")
r.HandleFunc("/upload", getUpload).Methods("GET")
http.ListenAndServe(fmt.Sprintf(":%d",port), r)
log.Printf("server running on http://localhost:%d", port)
}
func getUpload(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./index.html")
}
func postTsp(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// get file from Request
file, handler, err := r.FormFile("tspspec")
if err != nil {
fmt.Println("Error reading file")
fmt.Println(err)
return
}
defer file.Close()
filepath := fmt.Sprintf("./data/web/%s", handler.Filename)
// Write File
f, err := os.Create(filepath)
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
}
defer f.Close()
f.Write(fileBytes)
spec := readtsp(filepath)
// Response
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(spec.String())
}
func readtsp(path string) *tspreader.TspSpec {
if strings.HasSuffix(path,".gz") {
_ = UnGzip(path, filepath.Dir(path))
}
spec, e := tspreader.Read(strings.TrimSuffix(path,".gz"))
if e != nil {
log.Fatal(e.Error())
}
fmt.Println(spec)
return spec
}
func UnGzip(source, target string) error {
reader, err := os.Open(source)
if err != nil {
return err
}
defer reader.Close()
archive, err := gzip.NewReader(reader)
if err != nil {
return err
}
defer archive.Close()
target = filepath.Join(target, archive.Name)
writer, err := os.Create(target)
if err != nil {
return err
}
defer writer.Close()
_, err = io.Copy(writer, archive)
return err
}
\ No newline at end of file
......@@ -34,7 +34,11 @@ func NewTspDataArray() *TspData {
}
func (t TspSpec) String() string {
return fmt.Sprintf("%s - %s", t.name, t.problemType)
return fmt.Sprintf("%s [%s] - %s %s %d", t.name, t.problemType, t.edgeWeightType, t.edgeWeightFormat, t.dimension)
}
func (t TspData) String() string {
return fmt.Sprintf("Datatype: %s", t.datatype)
}
// Read Reads TSP data from .tsp file
......@@ -52,9 +56,10 @@ func Read(path string) (*TspSpec, error) {
for scanner.Scan() {
line := scanner.Text()
// handle Specification Headers
if strings.Contains(line,":") {
strs := strings.Split(line, ":")
fmt.Println(strs)
switch strings.ReplaceAll(strs[0]," ","") {
case "NAME": {
spec.name = strings.ReplaceAll(strs[1]," ","")
......@@ -79,6 +84,7 @@ func Read(path string) (*TspSpec, error) {
}
}
} else {
// Sace Data as Multi Dimensional Array
if dataLine == -1 {
spec.data.datatype = strings.ReplaceAll(line," ","")
dataLine++
......@@ -90,7 +96,6 @@ func Read(path string) (*TspSpec, error) {
for _, e := range entries{
f, err := strconv.ParseFloat(e, 64)
if err == nil {
log.Println(f)
arr[dataLine] = append(arr[dataLine], f)
}
}
......@@ -100,5 +105,7 @@ func Read(path string) (*TspSpec, error) {
}
spec.data.values = &arr
// return specification
return spec, nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment