Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

lightning: ensure the web interface still works outside server mode #259

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions lightning/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,6 @@ func parseTaskID(req *http.Request) (int64, string, error) {
func (l *Lightning) handleTask(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")

if l.taskCfgs == nil {
// l.taskCfgs is non-nil only if Lightning is started with RunServer().
// Without the server mode this pointer is default to be nil.
writeJSONError(w, http.StatusNotImplemented, "server-mode not enabled", nil)
return
}

switch req.Method {
case http.MethodGet:
taskID, _, err := parseTaskID(req)
Expand Down Expand Up @@ -290,7 +283,9 @@ func (l *Lightning) handleGetTask(w http.ResponseWriter) {
QueuedIDs []int64 `json:"queue"`
}

response.QueuedIDs = l.taskCfgs.AllIDs()
if l.taskCfgs != nil {
response.QueuedIDs = l.taskCfgs.AllIDs()
}

l.cancelLock.Lock()
if l.cancel != nil && l.curTask != nil {
Expand All @@ -312,7 +307,7 @@ func (l *Lightning) handleGetOneTask(w http.ResponseWriter, req *http.Request, t
}
l.cancelLock.Unlock()

if task == nil {
if task == nil && l.taskCfgs != nil {
task, _ = l.taskCfgs.Get(taskID)
}

Expand All @@ -333,6 +328,13 @@ func (l *Lightning) handleGetOneTask(w http.ResponseWriter, req *http.Request, t
func (l *Lightning) handlePostTask(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Cache-Control", "no-store")

if l.taskCfgs == nil {
// l.taskCfgs is non-nil only if Lightning is started with RunServer().
// Without the server mode this pointer is default to be nil.
writeJSONError(w, http.StatusNotImplemented, "server-mode not enabled", nil)
return
}

type taskResponse struct {
ID int64 `json:"id"`
}
Expand Down Expand Up @@ -400,6 +402,11 @@ func (l *Lightning) handleDeleteOneTask(w http.ResponseWriter, req *http.Request
}

func (l *Lightning) handlePatchOneTask(w http.ResponseWriter, req *http.Request) {
if l.taskCfgs == nil {
writeJSONError(w, http.StatusNotImplemented, "server-mode not enabled", nil)
return
}

taskID, verb, err := parseTaskID(req)
if err != nil {
writeJSONError(w, http.StatusBadRequest, "invalid task ID", err)
Expand Down
70 changes: 70 additions & 0 deletions lightning/lightning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,73 @@ func (s *lightningServerSuite) TestGetDeleteTask(c *C) {
Queue: []int64{},
})
}

func (s *lightningServerSuite) TestHTTPAPIOutsideServerMode(c *C) {
s.lightning.globalCfg.App.ServerMode = false

url := "http://" + s.lightning.serverAddr.String() + "/tasks"

errCh := make(chan error)
go func() {
errCh <- s.lightning.RunOnce()
}()
time.Sleep(100 * time.Millisecond)

var curTask struct {
Current int64
Queue []int64
}

// `GET /tasks` should work fine.
resp, err := http.Get(url)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
err = json.NewDecoder(resp.Body).Decode(&curTask)
resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(curTask.Current, Not(Equals), 0)
c.Assert(curTask.Queue, HasLen, 0)

// `POST /tasks` should return 501
resp, err = http.Post(url, "application/toml", strings.NewReader("??????"))
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusNotImplemented)
resp.Body.Close()

// `GET /tasks/(current)` should work fine.
resp, err = http.Get(fmt.Sprintf("%s/%d", url, curTask.Current))
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
resp.Body.Close()

// `GET /tasks/123456` should return 404
resp, err = http.Get(url + "/123456")
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusNotFound)
resp.Body.Close()

// `PATCH /tasks/(current)/front` should return 501
req, err := http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/%d/front", url, curTask.Current), nil)
c.Assert(err, IsNil)
resp, err = http.DefaultClient.Do(req)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusNotImplemented)
resp.Body.Close()

// `DELETE /tasks/123456` should return 404
req.Method = http.MethodDelete
req.URL.Path = "/tasks/123456"
resp, err = http.DefaultClient.Do(req)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusNotFound)
resp.Body.Close()

// `DELETE /tasks/(current)` should return 200
req.URL.Path = fmt.Sprintf("/tasks/%d", curTask.Current)
resp, err = http.DefaultClient.Do(req)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)

// ... and the task should be canceled now.
c.Assert(<-errCh, Equals, context.Canceled)
}