From 6debf10a577cbc0d890041fdd91d91cf14a1c0f6 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 24 Jun 2015 19:11:43 +0900 Subject: [PATCH] Cleanup --- server.go | 29 +++++++++++------------------ types.go | 24 ++++++++++++------------ util.go | 3 ++- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/server.go b/server.go index 59b4864..f2616a7 100644 --- a/server.go +++ b/server.go @@ -25,13 +25,13 @@ package main import ( "database/sql" "encoding/json" + "flag" "fmt" - _ "github.com/go-sql-driver/mysql" "log" "net/http" - "os" - "path/filepath" "strings" + + _ "github.com/go-sql-driver/mysql" ) var db *sql.DB @@ -258,21 +258,14 @@ func clearHistory(rw http.ResponseWriter, req *http.Request) { fmt.Fprint(rw, "History tables cleared") } -func staticPath() (string, error) { - if len(os.Args) > 1 { - return os.Args[1], nil - } - - return filepath.Abs(filepath.Join(filepath.Dir(os.Args[0]), "static")) -} - func main() { - dir, err := staticPath() - if err != nil { - log.Fatal(err) - } + staticDir := flag.String("static", "static", "path to static files") + portNum := flag.Int("port", 8080, "port to serve content on") + dataSrc := flag.String("data", "hscd@/hscd", "data source for database") + flag.Parse() - db, err = sql.Open("mysql", "hscd@/hscd") + var err error + db, err = sql.Open("mysql", *dataSrc) if err != nil { log.Fatal(err) } @@ -284,7 +277,7 @@ func main() { http.HandleFunc("/forget", removeCategory) http.HandleFunc("/access", accessReview) http.HandleFunc("/clear", clearHistory) - http.Handle("/", http.FileServer(http.Dir(dir))) + http.Handle("/", http.FileServer(http.Dir(*staticDir))) - log.Fatal(http.ListenAndServe(":8080", nil)) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *portNum), nil)) } diff --git a/types.go b/types.go index cdcc3e0..54d54b5 100644 --- a/types.go +++ b/types.go @@ -140,23 +140,23 @@ type recordSorter struct { key string } -func (sorter recordSorter) sort() { - if sorter.ascending { - sort.Sort(sorter) +func (s recordSorter) sort() { + if s.ascending { + sort.Sort(s) } else { - sort.Sort(sort.Reverse(sorter)) + sort.Sort(sort.Reverse(s)) } } -func (sorter recordSorter) Len() int { - return len(sorter.entries) +func (s recordSorter) Len() int { + return len(s.entries) } -func (sorter recordSorter) Less(i, j int) bool { - entry1 := sorter.entries[i] - entry2 := sorter.entries[j] +func (s recordSorter) Less(i, j int) bool { + entry1 := s.entries[i] + entry2 := s.entries[j] - switch sorter.key { + switch s.key { case "accessCount": return entry1.accessCount < entry2.accessCount case "closestStn": @@ -174,6 +174,6 @@ func (sorter recordSorter) Less(i, j int) bool { } } -func (sorter recordSorter) Swap(i, j int) { - sorter.entries[i], sorter.entries[j] = sorter.entries[j], sorter.entries[i] +func (s recordSorter) Swap(i, j int) { + s.entries[i], s.entries[j] = s.entries[j], s.entries[i] } diff --git a/util.go b/util.go index 96656ef..14567d5 100644 --- a/util.go +++ b/util.go @@ -23,10 +23,11 @@ package main import ( - "github.com/kellydunn/golang-geo" "log" "math" "strconv" + + "github.com/kellydunn/golang-geo" ) func fixFeatures(features featureMap) featureMap {