1
This commit is contained in:
Alex Yatskov 2015-08-23 18:56:18 +09:00
parent 02f58a3ed5
commit 65e9036a63
3 changed files with 41 additions and 46 deletions

View File

@ -213,7 +213,7 @@ func dumpData(dbPath string, restaraunts []restaurant) error {
} }
func main() { func main() {
dbPath := flag.String("db", "data/db.sqlite3", "output database") dbPath := flag.String("db", "data/db.sqlite3", "database output path")
urlsPath := flag.String("urls", "data/urls.txt", "index URLs to scrape") urlsPath := flag.String("urls", "data/urls.txt", "index URLs to scrape")
stationsPath := flag.String("stations", "data/stations.json", "station geolocation data") stationsPath := flag.String("stations", "data/stations.json", "station geolocation data")
geocachePath := flag.String("geocache", "cache/geocache.json", "geolocation data cache") geocachePath := flag.String("geocache", "cache/geocache.json", "geolocation data cache")

View File

@ -32,7 +32,6 @@ import (
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"runtime"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
"sync" "sync"
@ -163,7 +162,12 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
} }
defer categoryRows.Close() defer categoryRows.Close()
var categories []jsonCategory type category struct {
Description string `json:"description"`
Id int `json:"id"`
}
var response []category
for categoryRows.Next() { for categoryRows.Next() {
var ( var (
description string description string
@ -174,14 +178,14 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
log.Fatal(err) log.Fatal(err)
} }
categories = append(categories, jsonCategory{description, id}) response = append(response, category{description, id})
} }
if err := categoryRows.Err(); err != nil { if err := categoryRows.Err(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
js, err := json.Marshal(categories) js, err := json.Marshal(response)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -191,15 +195,24 @@ func getCategories(rw http.ResponseWriter, req *http.Request) {
} }
func addCategory(rw http.ResponseWriter, req *http.Request) { func addCategory(rw http.ResponseWriter, req *http.Request) {
var request jsonAddCategoryRequest var request struct {
Description string `json:"description"`
}
var response struct {
Description string `json:"description"`
Id int `json:"id"`
Success bool `json:"success"`
}
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
response := jsonAddCategoryResponse{Description: strings.TrimSpace(request.Description)} response.Description = strings.TrimSpace(request.Description)
if len(request.Description) > 0 { if len(response.Description) > 0 {
result, err := db.Exec("INSERT INTO categories(description) VALUES(?)", request.Description) result, err := db.Exec("INSERT INTO categories(description) VALUES(?)", request.Description)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -229,14 +242,24 @@ func addCategory(rw http.ResponseWriter, req *http.Request) {
} }
func removeCategory(rw http.ResponseWriter, req *http.Request) { func removeCategory(rw http.ResponseWriter, req *http.Request) {
var request jsonRemoveCategoryRequest var request struct {
Id int `json:"id"`
}
var response struct {
Success bool `json:"success"`
}
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
_, err := db.Exec("DELETE FROM categories WHERE id = (?)", request.Id) if _, err := db.Exec("DELETE FROM categories WHERE id = (?)", request.Id); err == nil {
js, err := json.Marshal(jsonRemoveCategoryResponse{err == nil}) response.Success = true
}
js, err := json.Marshal(response)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -246,7 +269,11 @@ func removeCategory(rw http.ResponseWriter, req *http.Request) {
} }
func accessReview(rw http.ResponseWriter, req *http.Request) { func accessReview(rw http.ResponseWriter, req *http.Request) {
var request jsonAccessRequest var request struct {
Id int `json:"id"`
Profile featureMap `json:"profile"`
}
if err := json.NewDecoder(req.Body).Decode(&request); err != nil { if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
@ -308,11 +335,9 @@ func clearHistory(rw http.ResponseWriter, req *http.Request) {
} }
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) staticDir := flag.String("static", "static", "static files path")
staticDir := flag.String("static", "static", "path to static files")
portNum := flag.Int("port", 8080, "port to serve content on") portNum := flag.Int("port", 8080, "port to serve content on")
dataSrc := flag.String("data", "build/data/db.sqlite3", "data source for database") dataSrc := flag.String("db", "build/data/db.sqlite3", "database path")
profile := flag.String("profile", "", "write cpu profile to file") profile := flag.String("profile", "", "write cpu profile to file")
flag.Parse() flag.Parse()
@ -322,8 +347,6 @@ func main() {
} }
defer db.Close() defer db.Close()
db.SetMaxIdleConns(256)
if *profile != "" { if *profile != "" {
f, err := os.Create(*profile) f, err := os.Create(*profile)
if err != nil { if err != nil {

View File

@ -37,11 +37,6 @@ const (
modeTypeDist modeTypeDist
) )
type jsonAccessRequest struct {
Id int `json:"id"`
Profile featureMap `json:"profile"`
}
type jsonGeoData struct { type jsonGeoData struct {
Latitude float64 `json:"latitude"` Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"` Longitude float64 `json:"longitude"`
@ -99,29 +94,6 @@ type jsonQueryResponse struct {
ElapsedTime int64 `json:"elapsedTime"` ElapsedTime int64 `json:"elapsedTime"`
} }
type jsonCategory struct {
Description string `json:"description"`
Id int `json:"id"`
}
type jsonAddCategoryRequest struct {
Description string `json:"description"`
}
type jsonAddCategoryResponse struct {
Description string `json:"description"`
Id int `json:"id"`
Success bool `json:"success"`
}
type jsonRemoveCategoryRequest struct {
Id int `json:"id"`
}
type jsonRemoveCategoryResponse struct {
Success bool `json:"success"`
}
type queryContext struct { type queryContext struct {
geo *geoData geo *geoData
profile featureMap profile featureMap