route /index.html #103

This commit is contained in:
Umputun
2018-07-01 11:50:02 -05:00
parent e753346255
commit 70649b2712
3 changed files with 35 additions and 1 deletions
+2 -1
View File
@@ -14,13 +14,14 @@ import (
"time" "time"
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
"github.com/umputun/remark/backend/app/rest" "github.com/umputun/remark/backend/app/rest"
) )
// JSON is a map alias, just for convenience // JSON is a map alias, just for convenience
type JSON map[string]interface{} type JSON map[string]interface{}
type contextKey string
// AppInfo adds custom app-info to the response header // AppInfo adds custom app-info to the response header
func AppInfo(app string, version string) func(http.Handler) http.Handler { func AppInfo(app string, version string) func(http.Handler) http.Handler {
f := func(h http.Handler) http.Handler { f := func(h http.Handler) http.Handler {
+14
View File
@@ -5,8 +5,10 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"path"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -174,6 +176,7 @@ func (s *Rest) routes() chi.Router {
}) })
}) })
// respond to /robots.tx with the list of allowed paths
router.With(tollbooth_chi.LimitHandler(tollbooth.NewLimiter(50, nil))). router.With(tollbooth_chi.LimitHandler(tollbooth.NewLimiter(50, nil))).
Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) { Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
allowed := []string{"/find", "/last", "/id", "/count", "/counts", "/list", "/config", "/img", "/avatar"} allowed := []string{"/find", "/last", "/id", "/count", "/counts", "/list", "/config", "/img", "/avatar"}
@@ -183,6 +186,17 @@ func (s *Rest) routes() chi.Router {
render.PlainText(w, r, "User-agent: *\nDisallow: /auth/\nDisallow: /api/\n"+strings.Join(allowed, "\n")+"\n") render.PlainText(w, r, "User-agent: *\nDisallow: /auth/\nDisallow: /api/\n"+strings.Join(allowed, "\n")+"\n")
}) })
// respond to /index.html with the content of getstarted.html under /web root
router.With(tollbooth_chi.LimitHandler(tollbooth.NewLimiter(50, nil))).
Get("/index.html", func(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadFile(path.Join(s.WebRoot, "getstarted.html"))
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
render.HTML(w, r, string(data))
})
// file server for static content from /web // file server for static content from /web
addFileServer(router, "/web", http.Dir(s.WebRoot)) addFileServer(router, "/web", http.Dir(s.WebRoot))
return router return router
+19
View File
@@ -25,6 +25,7 @@ import (
var testDb = "/tmp/test-remark.db" var testDb = "/tmp/test-remark.db"
var testHTML = "/tmp/test-remark.html" var testHTML = "/tmp/test-remark.html"
var getStartedHTML = "/tmp/getstarted.html"
func TestRest_FileServer(t *testing.T) { func TestRest_FileServer(t *testing.T) {
srv, ts := prep(t) srv, ts := prep(t)
@@ -36,6 +37,24 @@ func TestRest_FileServer(t *testing.T) {
assert.Equal(t, "some html", body) assert.Equal(t, "some html", body)
} }
func TestRest_GetStarted(t *testing.T) {
srv, ts := prep(t)
assert.NotNil(t, srv)
defer cleanup(ts)
err := ioutil.WriteFile(getStartedHTML, []byte("some html blah"), 0700)
assert.Nil(t, err)
body, code := get(t, ts.URL+"/index.html")
assert.Equal(t, 200, code)
assert.Equal(t, "some html blah", body)
os.Remove(getStartedHTML)
_, code = get(t, ts.URL+"/index.html")
assert.Equal(t, 404, code)
}
func TestRest_Shutdown(t *testing.T) { func TestRest_Shutdown(t *testing.T) {
srv := Rest{Authenticator: auth.Authenticator{}, AvatarProxy: &proxy.Avatar{Store: proxy.NewFSAvatarStore("/tmp", 300), srv := Rest{Authenticator: auth.Authenticator{}, AvatarProxy: &proxy.Avatar{Store: proxy.NewFSAvatarStore("/tmp", 300),
RoutePath: "/api/v1/avatar"}, ImageProxy: &proxy.Image{}} RoutePath: "/api/v1/avatar"}, ImageProxy: &proxy.Image{}}