1
This commit is contained in:
Alex Yatskov 2015-09-01 18:41:37 +09:00
parent 7f6535556e
commit deafc3b218
2 changed files with 36 additions and 9 deletions

View File

@ -39,10 +39,8 @@ import (
func main() {
var (
staticDir = flag.String("static", "../static", "static files path")
portNum = flag.Int("port", 8080, "port to serve content on")
dataSrc = flag.String("db", "../build/data/db.sqlite3", "database path")
profile = flag.String("profile", "", "write cpu profile to file")
portNum = flag.Int("port", 8080, "port to serve content on")
profile = flag.String("profile", "", "write cpu profile to file")
)
flag.Parse()
@ -65,6 +63,10 @@ func main() {
}()
}
mux := search.NewSearchApp(*staticDir, *dataSrc)
mux, err := search.NewSearchApp()
if err != nil {
log.Fatal(err)
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *portNum), mux))
}

View File

@ -25,9 +25,12 @@ package search
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"path"
"runtime"
"strings"
"sync"
"time"
@ -391,8 +394,30 @@ func handleClearHistory(rw http.ResponseWriter, req *http.Request) {
fmt.Fprint(rw, "History tables cleared")
}
func NewSearchApp(sd, ds string) *http.ServeMux {
dataSrc = ds
func queryPaths() (staticDat, dataSrc string, err error) {
_, filename, _, ok := runtime.Caller(1)
if !ok {
err = errors.New("unable to capture paths")
return
}
dir := path.Dir(filename)
dataSrc = path.Join(dir, "build/data/db.sqlite3")
staticDat = path.Join(dir, "static")
return
}
func NewSearchApp() (*http.ServeMux, error) {
var (
err error
staticDat string
)
staticDat, dataSrc, err = queryPaths()
if err != nil {
return nil, err
}
mux := http.NewServeMux()
mux.HandleFunc("/query", handleExecuteQuery)
@ -401,7 +426,7 @@ func NewSearchApp(sd, ds string) *http.ServeMux {
mux.HandleFunc("/forget", handleRemoveCategory)
mux.HandleFunc("/access", handleAccessReview)
mux.HandleFunc("/clear", handleClearHistory)
mux.Handle("/", http.FileServer(http.Dir(sd)))
mux.Handle("/", http.FileServer(http.Dir(staticDat)))
return mux
return mux, nil
}