Exemplo n.º 1
0
Arquivo: web.go Projeto: Jackong/web
func Go() {
	log.Init(config.Project.Dir.Log)
	defer log.Close()

	log.Info("setup server...")
	tpl.Init(config.Project.Dir.Tpl.Path, config.Project.Dir.Tpl.Suffix)
	defer tpl.Close()

	json.Init()

	static.Init(config.Project.Dir.Static.Root, config.Project.Dir.Static.Paths)

	log.Info("collecting controller")
	log.Info("setting handler for home")
	http.HandleFunc("/", HomeHandler)
	log.Info("listening")
	http.ListenAndServe(config.Project.Server.Addr, nil)
}
Exemplo n.º 2
0
Arquivo: web.go Projeto: Jackong/web
func HomeHandler(writer http.ResponseWriter, req *http.Request) {
	defer func() {
		if e := recover(); e != nil {
			err := e.(i.InputError)
			log.Error(err)
			http.NotFound(writer, req)
			return
		}
	}()

	log.Info(req.RemoteAddr, req.Method, req.URL.Path)
	accept := req.Header.Get("Accept")
	acceptor := o.Acceptor(accept)
	if acceptor == nil {
		log.Error("not acceptable", accept)
		http.Error(writer, "406 Not Acceptable", http.StatusNotAcceptable)
		return
	}

	ctrl := mapper.Get(req.URL.Path)
	if ctrl == nil {
		log.Warning("page not found", req.URL.Path)
		http.NotFound(writer, req)
		return
	}

	ctx := io.New(&i.Input{Req: req}, &o.Output{Writer: writer})
	switch req.Method {
	case method.GET:
		ctrl.Read(ctx)
	case method.POST:
		ctrl.Create(ctx)
	case method.PUT:
		ctrl.Update(ctx)
	case method.DEL:
		ctrl.Delete(ctx)
	default:
		http.Error(writer, "405 Method Not Allowed", http.StatusMethodNotAllowed)
		return
	}

	if _, output := ctx.Output.Get(); output == nil {
		http.Error(writer, "405 Method Not Allowed", http.StatusMethodNotAllowed)
		return
	}

	if err := acceptor.Present(ctx.Output); err != nil {
		log.Error("present error", err)
		http.Error(writer, "500 Internal Server Error", http.StatusInternalServerError)
	}
}
Exemplo n.º 3
0
Arquivo: tpl.go Projeto: Jackong/web
func Init(tplPath, suffix string) {
	log.Info("loading templates")
	filepath.Walk(tplPath, visit)
	tpls = template.Must(template.ParseFiles(tplFiles...))
	o.Register("text/html", &Tpl{suffix: suffix})
}
Exemplo n.º 4
0
func Init(root string, paths []string) {
	log.Info("setting static resource")
	for _, static := range paths {
		http.Handle("/"+static+"/", http.FileServer(http.Dir(root)))
	}
}