Uses narco with logging
We have a finner control of what we serve
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -28,3 +28,4 @@ _testmain.go
|
|||||||
/cover.out
|
/cover.out
|
||||||
|
|
||||||
*.bar.satellite/
|
*.bar.satellite/
|
||||||
|
tmp/
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -50,6 +50,11 @@ func newAppData(opts Options) (*appData, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = os.MkdirAll(filepath.Join("tmp", "log"), 0755)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
res := &appData{
|
res := &appData{
|
||||||
opts: opts,
|
opts: opts,
|
||||||
errors: make(chan error, 10),
|
errors: make(chan error, 10),
|
||||||
|
|||||||
74
router.go
74
router.go
@@ -3,62 +3,79 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"ponyo.epfl.ch/gitlab/alexandre.tuleu/narco"
|
||||||
|
|
||||||
bleve_http "github.com/blevesearch/bleve/http"
|
bleve_http "github.com/blevesearch/bleve/http"
|
||||||
"github.com/gorilla/mux"
|
"github.com/codemodus/chain"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
var rxExt = regexp.MustCompile(`[0-9]+`)
|
var rxExt = regexp.MustCompile(`[0-9]+`)
|
||||||
|
|
||||||
func (a *appData) buildRouter() http.Handler {
|
func (a *appData) buildRouter() http.Handler {
|
||||||
router := mux.NewRouter()
|
router := httprouter.New()
|
||||||
|
|
||||||
|
logger := narco.NewLogger()
|
||||||
|
f, err := os.Create(filepath.Join("tmp", "log", "access.log"))
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
logger.SetOutput(f)
|
||||||
|
recoverer := narco.NewRecoverer()
|
||||||
|
ctx := narco.WithErrorFormatter(context.Background(), narco.BasicHTMLErrorFormatter)
|
||||||
|
ch := chain.New(logger.Wrap(), recoverer.Wrap()).SetContext(ctx)
|
||||||
|
|
||||||
bleve_http.RegisterIndexName("album", a.index)
|
bleve_http.RegisterIndexName("album", a.index)
|
||||||
searchHandler := bleve_http.NewSearchHandler("album")
|
searchHandler := bleve_http.NewSearchHandler("album")
|
||||||
|
|
||||||
router.Handle("/api/search", searchHandler).Methods("POST")
|
router.POST("/api/search", narco.EndChain(ch,
|
||||||
|
narco.HandlerFunc(func(_ context.Context, w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
|
||||||
|
searchHandler.ServeHTTP(w, req)
|
||||||
|
})))
|
||||||
|
|
||||||
router.Handle("/api/recents", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
router.GET("/api/recents", narco.EndChain(ch,
|
||||||
|
narco.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||||
albums, err := a.db.ByPurchaseDate()
|
albums, err := a.db.ByPurchaseDate()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
narco.Error(ctx, w, err, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
err = enc.Encode(albums)
|
err = enc.Encode(albums)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
narco.Error(ctx, w, err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
})).Methods("GET")
|
})))
|
||||||
|
|
||||||
router.Handle("/api/albums/{id:[0-9]+}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
router.GET("/api/albums/:id", narco.EndChain(ch,
|
||||||
vars := mux.Vars(r)
|
narco.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
var id uint64
|
var id uint64
|
||||||
var err error
|
var err error
|
||||||
var idStr string
|
idStr := ps.ByName("id")
|
||||||
var ok bool
|
if len(idStr) > 0 {
|
||||||
if len(vars) != 0 {
|
|
||||||
idStr, ok = vars["id"]
|
|
||||||
if ok == true {
|
|
||||||
id, err = strconv.ParseUint(idStr, 10, 64)
|
id, err = strconv.ParseUint(idStr, 10, 64)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ok == false || err != nil {
|
if len(idStr) == 0 || err != nil {
|
||||||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
narco.Error(ctx, w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
albumUnsafe, err := a.db.Get(AlbumID(id))
|
albumUnsafe, err := a.db.Get(AlbumID(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
narco.Error(ctx, w, err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
album := *albumUnsafe
|
album := *albumUnsafe
|
||||||
@@ -68,11 +85,26 @@ func (a *appData) buildRouter() http.Handler {
|
|||||||
|
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
if err := enc.Encode(album); err != nil {
|
if err := enc.Encode(album); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
narco.Error(ctx, w, err, http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
})).Methods("GET")
|
})))
|
||||||
|
|
||||||
router.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))
|
dirs := []string{"css", "js", "img"}
|
||||||
|
for _, d := range dirs {
|
||||||
|
router.ServeFiles(path.Join("/", d, "/*filepath"), http.Dir(filepath.Join("static", d)))
|
||||||
|
}
|
||||||
|
|
||||||
|
router.GET("/", narco.EndChain(ch, narco.HandlerFunc(
|
||||||
|
func(ctx context.Context, w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
|
||||||
|
f, err := os.Open(filepath.Join("static", "index.html"))
|
||||||
|
if err != nil {
|
||||||
|
narco.Error(ctx, w, err, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer closeOrPanic(f, filepath.Join("static", "index.html"))
|
||||||
|
|
||||||
|
io.Copy(w, f)
|
||||||
|
})))
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user