Giter Site home page Giter Site logo

webrequest's Introduction

webrequest

Helper package for common web request features

A broken example is better than no example in my eyes. Here's a ~2 year old example that still has to be converted to use the proper templates and web elements.

func (ws *WebServer) Handler(w http.ResponseWriter, r *http.Request) {
	wr := GetWebRequest(r)
	if wr.ID == "" {
		ws.showWebError(errors.New("Missing web request in context"), w, r)
		return
	}
	ws.cachemux.RLock()
	if ws.cache != nil {
		ele, ok := ws.cache[wr.WebRoute]
		ws.cachemux.RUnlock()
		if ok && ele != nil {
			ele.Write(w, r)
			ws.Log(log.Debug, "wrote element from cache")
			return
		}
	} else {
		ws.cachemux.RUnlock()
	}

	var ele dashboard.Element
	var err error
	var template string

	ws.routemux.RLock()
	ele, template, err = ws.routes.Do(wr)
	ws.routemux.RUnlock()
	if err != nil {
		if len(wr.Service) == 0 {
			ws.Log(log.Debug, "Service empty, trying default route")
			ws.defmux.RLock()
			defroute := ws.defroute
			ws.defmux.RUnlock()
			if defroute == nil {
				ws.Log(log.Debug, "no default route, trying ipc")
				f, err := ws.getDefaultFromIpc(wr)
				if err != nil {
					ws.showWebError(err, w, r)
					return
				}
				ws.Log(log.Debug, "got func from ipc")
				ws.defmux.Lock()
				ws.defroute = f
				ws.defmux.Unlock()
				ws.Log(log.Debug, "saved default route")
				defroute = f
			}
			ws.Log(log.Debug, "running default route")
			ele, template, err = defroute(wr)
			if err != nil {
				ws.showWebError(err, w, r)
				return
			}
			ws.Log(log.Debug, "got element from default route")
		} else {
			ws.showWebError(err, w, r)
			return
		}
	}
	if ele == nil {
		ws.showWebError(errors.New("Nil element"), w, r)
		return
	}

	if len(template) > 0 {
		ws.tempmux.RLock()
		temp, ok := ws.templates[template]
		ws.tempmux.RUnlock()
		if ok {
			if !ele.Cacheable() {
				temp.WriteWithChildren(w, r, ele)
				//temp.WriteAndReplace(w, r, ele)
				return
			}
			pg := temp.Copy()
			pg.AppendChildren(ele)
			pg.SetCacheable(true)
			ele = pg
		}
	}

	if ele.Cacheable() {
		ws.Log(log.Debug, "Caching", wr.String())
		ws.cachemux.Lock()
		ws.cache[wr.WebRoute] = ele
		ws.cachemux.Unlock()
	}

	ele.Write(w, r)
}

webrequest's People

Contributors

madman22 avatar madman2233 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.