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,9 +39,7 @@ import (
func main() { func main() {
var ( var (
staticDir = flag.String("static", "../static", "static files path")
portNum = flag.Int("port", 8080, "port to serve content on") 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") profile = flag.String("profile", "", "write cpu profile to file")
) )
@ -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)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *portNum), mux))
} }

View File

@ -25,9 +25,12 @@ package search
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"math" "math"
"net/http" "net/http"
"path"
"runtime"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -391,8 +394,30 @@ func handleClearHistory(rw http.ResponseWriter, req *http.Request) {
fmt.Fprint(rw, "History tables cleared") fmt.Fprint(rw, "History tables cleared")
} }
func NewSearchApp(sd, ds string) *http.ServeMux { func queryPaths() (staticDat, dataSrc string, err error) {
dataSrc = ds _, 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 := http.NewServeMux()
mux.HandleFunc("/query", handleExecuteQuery) mux.HandleFunc("/query", handleExecuteQuery)
@ -401,7 +426,7 @@ func NewSearchApp(sd, ds string) *http.ServeMux {
mux.HandleFunc("/forget", handleRemoveCategory) mux.HandleFunc("/forget", handleRemoveCategory)
mux.HandleFunc("/access", handleAccessReview) mux.HandleFunc("/access", handleAccessReview)
mux.HandleFunc("/clear", handleClearHistory) mux.HandleFunc("/clear", handleClearHistory)
mux.Handle("/", http.FileServer(http.Dir(sd))) mux.Handle("/", http.FileServer(http.Dir(staticDat)))
return mux return mux, nil
} }