main.go 3 KB
Newer Older
1
2
3
4
// =======================================================
// Author Cedric Pump, Student, Technische Hochschule Lübeck
// =======================================================

CedricPump's avatar
- init    
CedricPump committed
5
6
7
package main

import (
8
9
	"compress/gzip"
	"encoding/json"
CedricPump's avatar
- init    
CedricPump committed
10
	"fmt"
11
12
13
	"github.com/gorilla/mux"
	"io"
	"io/ioutil"
CedricPump's avatar
- init    
CedricPump committed
14
	"log"
15
16
17
	"net/http"
	"os"
	"path/filepath"
CedricPump's avatar
- init    
CedricPump committed
18
	"pvs_tsp/tspreader"
19
	"pvs_tsp/tspsolver"
20
	"strings"
CedricPump's avatar
- init    
CedricPump committed
21
22
)

23
// main executable
CedricPump's avatar
- init    
CedricPump committed
24
func main() {
25
26
27
	startServer(8000)
}

28
29
30
// ------------------------------------
// startServer starts Rest server
// ------------------------------------
31
32
33
34
func startServer(port int)  {

	r := mux.NewRouter()

35
36
	r.HandleFunc("/tsp", postTsp).Methods("POST")
	r.HandleFunc("/", getUpload).Methods("GET")
37
38
	r.HandleFunc("/upload", getUpload).Methods("GET")

39
40
41
42
43
	e := http.ListenAndServe(fmt.Sprintf(":%d",port), r)
	if e != nil {
		log.Fatal(e)
		return
	}
44
	log.Printf("server running on http://localhost:%d", port)
45
	log.Printf("usage: http://localhost:#{port}/upload")
46
47
}

48
49
50
// ------------------------------------
// getUpload returns static html page for uploading tsp file
// ------------------------------------
51
52
53
54
func getUpload(w http.ResponseWriter, r *http.Request)  {
	http.ServeFile(w, r, "./index.html")
}

55
56
57
// ------------------------------------
// postTsp is endpoint to upload tsp file
// ------------------------------------
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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)

81
82
	spec := readTsp(filepath)
	sol := solveTsp(spec)
83
84
85

	// Response
	w.WriteHeader(http.StatusOK)
CedricPump's avatar
CedricPump committed
86
	json.NewEncoder(w).Encode(sol.StringFull())
87
88
}

89
90
91
92
93
// ------------------------------------
// readTsp reads tsp file and returns specification struct
// unzipping gz zip files is possible
// ------------------------------------
func readTsp(path string) tspreader.TspSpec {
94
95
96
97
98
99

	if strings.HasSuffix(path,".gz") {
		_ = UnGzip(path, filepath.Dir(path))
	}

	spec, e := tspreader.Read(strings.TrimSuffix(path,".gz"))
CedricPump's avatar
- init    
CedricPump committed
100
101
102
	if e != nil {
		log.Fatal(e.Error())
	}
103

104
	return *spec
105
106
}

107
108
109
110
111
112
// ------------------------------------
// solveTsp solves tsp
// ------------------------------------
func solveTsp(spec tspreader.TspSpec) tspsolver.TspSolution{
	return tspsolver.Solve(spec)
}
113

114
115
116
117
118
119
120
121
// UnGzip
// ------------------------------------
// unzips .gz files
// ------------------------------------
func UnGzip(file, targetpath string) error {
	reader, e := os.Open(file)
	if e != nil {
		return e
122
123
124
	}
	defer reader.Close()

125
126
127
	r, e := gzip.NewReader(reader)
	if e != nil {
		return e
128
	}
129
	defer r.Close()
130

131
132
133
134
	targetpath = filepath.Join(targetpath, r.Name)
	w, e := os.Create(targetpath)
	if e != nil {
		return e
135
	}
136
	defer w.Close()
137

138
139
	_, e = io.Copy(w, r)
	return e
CedricPump's avatar
- init    
CedricPump committed
140
}