github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/appengine/instance_info/view.go (about) 1 // Package instance_info provides html views; the core is in instance_mgt. 2 package instance_info 3 4 import ( 5 "fmt" 6 "net/http" 7 8 // sc "github.com/pbberlin/tools/dsu/distributed_unancestored" 9 10 "github.com/pbberlin/tools/appengine/instance_mgt" 11 "github.com/pbberlin/tools/net/http/loghttp" 12 "github.com/pbberlin/tools/net/http/tplx" 13 "google.golang.org/appengine" 14 ) 15 16 func view(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 17 18 cntr := 1 19 20 tplAdder, tplExec := tplx.FuncTplBuilder(w, r) 21 tplAdder("n_html_title", "Application, Module and Instance Info", nil) 22 tplAdder("n_cont_1", "<pre>{{.}}</pre>", instance_mgt.GetStatic().String()) 23 tplAdder("n_cont_2", "<p>{{.}} views</p>", cntr) 24 tplAdder("n_cont_0", ` 25 <p>AppID is `+appengine.AppID(appengine.NewContext(r))+`</p> 26 <p>On the development server, call 27 <a href='/instance-info/collect' 28 target='collect' >collect</a> first.</p> 29 30 <p><a href='/instance-info/`+instance_mgt.GetStatic().InstanceID+`'>specific url</a></p> 31 32 `, "") 33 34 tplExec(w, r) 35 36 /* 37 Requests are routed randomly accross instances 38 39 Following is just a futile try to register 40 an instance specific handler. 41 It is only useful, when we request an instance 42 specifically via specific hostname 43 */ 44 SuppressPanicUponDoubleRegistration( 45 w, r, "/instance-info/"+instance_mgt.GetStatic().InstanceID, loghttp.Adapter(view)) 46 47 } 48 49 // SuppressPanicUponDoubleRegistration registers 50 // a request hanlder for a route. 51 // 52 // 53 // Because of asynchronicity we need to 54 // catch the ensuing panic for repeated registration 55 // of the same handler 56 func SuppressPanicUponDoubleRegistration(w http.ResponseWriter, r *http.Request, 57 urlPattern string, handler func(http.ResponseWriter, *http.Request)) string { 58 defer func() { 59 panicSignal := recover() 60 if panicSignal != nil { 61 w.Write([]byte(fmt.Sprintf("panic caught:\n\n %s", panicSignal))) 62 } 63 }() 64 65 http.HandleFunc(urlPattern, handler) 66 return urlPattern 67 68 } 69 70 func collectInfo(w http.ResponseWriter, r *http.Request, m map[string]interface{}) { 71 instance_mgt.Get(r) 72 } 73 74 func init() { 75 76 // InstanceId := appengine.InstanceID() // does not during init, only after a few seconds 77 78 http.HandleFunc("/instance-info/view", loghttp.Adapter(view)) 79 http.HandleFunc("/instance-info/collect", loghttp.Adapter(collectInfo)) 80 81 }