Beispiel #1
0
func restGuarder(method RestFunc) rest.HandlerFunc {
	return func(w rest.ResponseWriter, r *rest.Request) {
		// begin := time.Now().UnixNano()
		defer func() {
			if e, ok := recover().(error); ok {
				rest.Error(w, e.Error(), http.StatusInternalServerError)
				log.Println("catchable system error occur: ", e.Error())
				debug.PrintStack()
			}
			// log.Printf("the request: %s cost: %d ms\n", r.URL.RequestURI(), ((time.Now().UnixNano() - begin) / 1000000))
		}()

		pathParams := r.PathParams
		var request entity.CommonRequest
		content, err := ioutil.ReadAll(r.Body)
		defer r.Body.Close()
		webUtils.CheckError(err)
		if len(content) == 0 {
			w.WriteJson(method(pathParams, nil))
			return
		}
		err = json.Unmarshal(content, &request)
		webUtils.CheckError(err)
		switch request.SyncType {
		case "sync":
			log.Println("now use sync mode")
			w.WriteJson(method(pathParams, content))
		case "async":
			log.Println("now use async mode")
			go method(pathParams, content)
			w.WriteJson(map[string]string{"status": "ok"})
		default:
			log.Println("now use default mode(sync)", request.SyncType)
			w.WriteJson(method(pathParams, content))

		}

	}
}
Beispiel #2
0
func init() {
	flag.Parse()
	common.InitArgs(*ARGS_PROPERTIES_URL)
	mongo.MongoInit()
	redis.RedisInit()

	htmlPath := *ARGS_STATIC_FILE_URL + string(os.PathSeparator) + common.TMPL_NAME + string(os.PathSeparator)
	fileInfoArray, err := ioutil.ReadDir(htmlPath)
	webUtils.CheckError(err)
	var fileName, filePath string
	for _, fileInfo := range fileInfoArray {
		fileName = fileInfo.Name()
		if ext := path.Ext(fileName); ext != ("." + common.TMPL_NAME) {
			continue
		}
		filePath = htmlPath + fileName
		log.Println("loading template: " + filePath)
		t := template.Must(template.New(fileName).Delims("[[", "]]").ParseFiles(filePath))
		templates[fileName] = t
	}
}
Beispiel #3
0
func renderPage(w http.ResponseWriter, tmpl string, values map[string]interface{}) {
	err := templates[tmpl+"."+common.TMPL_NAME].Execute(w, values)
	webUtils.CheckError(err)
}