diff --git a/backend/app/rest/api/middleware.go b/backend/app/rest/api/middleware.go index 77185eb1..59d53eac 100644 --- a/backend/app/rest/api/middleware.go +++ b/backend/app/rest/api/middleware.go @@ -14,13 +14,14 @@ import ( "time" "github.com/go-chi/chi/middleware" - "github.com/umputun/remark/backend/app/rest" ) // JSON is a map alias, just for convenience type JSON map[string]interface{} +type contextKey string + // AppInfo adds custom app-info to the response header func AppInfo(app string, version string) func(http.Handler) http.Handler { f := func(h http.Handler) http.Handler { diff --git a/backend/app/rest/api/rest.go b/backend/app/rest/api/rest.go index 37b17b71..8d27e2a5 100644 --- a/backend/app/rest/api/rest.go +++ b/backend/app/rest/api/rest.go @@ -5,8 +5,10 @@ import ( "context" "encoding/json" "fmt" + "io/ioutil" "log" "net/http" + "path" "strings" "sync" "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))). Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) { 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") }) + // 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 addFileServer(router, "/web", http.Dir(s.WebRoot)) return router diff --git a/backend/app/rest/api/rest_test.go b/backend/app/rest/api/rest_test.go index 324ab9dd..7826920d 100644 --- a/backend/app/rest/api/rest_test.go +++ b/backend/app/rest/api/rest_test.go @@ -25,6 +25,7 @@ import ( var testDb = "/tmp/test-remark.db" var testHTML = "/tmp/test-remark.html" +var getStartedHTML = "/tmp/getstarted.html" func TestRest_FileServer(t *testing.T) { srv, ts := prep(t) @@ -36,6 +37,24 @@ func TestRest_FileServer(t *testing.T) { 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) { srv := Rest{Authenticator: auth.Authenticator{}, AvatarProxy: &proxy.Avatar{Store: proxy.NewFSAvatarStore("/tmp", 300), RoutePath: "/api/v1/avatar"}, ImageProxy: &proxy.Image{}}