Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: a simple http server that serves the HUD view (#877)
- Loading branch information
Showing
6 changed files
with
145 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/windmilleng/tilt/internal/store" | ||
) | ||
|
||
type HeadsUpServer struct { | ||
store store.RStore | ||
router *mux.Router | ||
} | ||
|
||
func ProvideHeadsUpServer(store store.RStore) HeadsUpServer { | ||
r := mux.NewRouter().UseEncodedPath() | ||
s := HeadsUpServer{ | ||
store: store, | ||
router: r, | ||
} | ||
|
||
r.HandleFunc("/api/view", s.ViewJSON) | ||
|
||
return s | ||
} | ||
|
||
func (s HeadsUpServer) Router() http.Handler { | ||
return s.router | ||
} | ||
|
||
func (s HeadsUpServer) ViewJSON(w http.ResponseWriter, req *http.Request) { | ||
state := s.store.RLockState() | ||
view := store.StateToView(state) | ||
s.store.RUnlockState() | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
err := json.NewEncoder(w).Encode(view) | ||
if err != nil { | ||
http.Error(w, fmt.Sprintf("Error rendering view payload: %v", err), http.StatusInternalServerError) | ||
} | ||
} |
Oops, something went wrong.