MCPcopy
hub / github.com/cortesi/devd / Router

Method Router

server.go:255–316  ·  view source on GitHub ↗

Router constructs the main Devd router that serves all requests

(logger termlog.TermLog, templates *template.Template)

Source from the content-addressed store, hash-verified

253
254// Router constructs the main Devd router that serves all requests
255func (dd *Devd) Router(logger termlog.TermLog, templates *template.Template) (http.Handler, error) {
256 mux := http.NewServeMux()
257 hasGlobal := false
258
259 ci := inject.CopyInject{}
260 if dd.HasLivereload() {
261 ci = livereload.Injector
262 }
263
264 for match, route := range dd.Routes {
265 if match == "/" {
266 hasGlobal = true
267 }
268 handler := dd.WrapHandler(
269 logger,
270 route.Endpoint.Handler(route.Path, templates, ci),
271 )
272 mux.Handle(match, handler)
273 }
274 if dd.HasLivereload() {
275 lr := livereload.NewServer("livereload", logger)
276 mux.Handle(livereload.EndpointPath, lr)
277 mux.Handle(livereload.ScriptPath, http.HandlerFunc(lr.ServeScript))
278 seen := make(map[string]bool)
279 for _, route := range dd.Routes {
280 if _, ok := seen[route.Host]; route.Host != "" && ok == false {
281 mux.Handle(route.Host+livereload.EndpointPath, lr)
282 mux.Handle(
283 route.Host+livereload.ScriptPath,
284 http.HandlerFunc(lr.ServeScript),
285 )
286 seen[route.Host] = true
287 }
288 }
289 if dd.LivereloadRoutes {
290 err := WatchRoutes(dd.Routes, lr, dd.Excludes, logger)
291 if err != nil {
292 return nil, fmt.Errorf("Could not watch routes for livereload: %s", err)
293 }
294 }
295 if len(dd.WatchPaths) > 0 {
296 err := WatchPaths(dd.WatchPaths, dd.Excludes, lr, logger)
297 if err != nil {
298 return nil, fmt.Errorf("Could not watch path for livereload: %s", err)
299 }
300 }
301 dd.lrserver = lr
302 }
303 if !hasGlobal {
304 mux.Handle(
305 "/",
306 dd.WrapHandler(logger, HandleNotFound(templates)),
307 )
308 }
309 var h = http.Handler(mux)
310 if dd.Credentials != nil {
311 h = httpauth.SimpleBasicAuth(
312 dd.Credentials.username, dd.Credentials.password,

Callers 2

ServeMethod · 0.95
TestDevdHandlerFunction · 0.95

Calls 7

HasLivereloadMethod · 0.95
WrapHandlerMethod · 0.95
WatchRoutesFunction · 0.85
WatchPathsFunction · 0.85
HandleNotFoundFunction · 0.85
hostPortStripFunction · 0.85
HandlerMethod · 0.65

Tested by 1

TestDevdHandlerFunction · 0.76