Exemplo n.º 1
0
func (this *sqlDBFactory) NewConnect() *sql.DB {
	db, err := sql.Open(this.Driver,
		this.User+":"+this.Pwd+"@tcp("+this.Host+":"+strconv.Itoa(this.Port)+")/"+this.Database)
	if err != nil {
		log.Println(exterror.WrapExtError(err))
		return nil
	}
	return db
}
Exemplo n.º 2
0
Arquivo: gf.go Projeto: goframework/gf
func renderView(context *Context) {

	if context.JsonResponse != nil {
		context.w.Header().Add("Content-Type", "application/json; charset=utf-8")
		jsonBytes, err := json.Marshal(context.JsonResponse)
		if err != nil {
			log.Println(exterror.WrapExtError(err))
			context.w.Write([]byte("{}"))
		}

		context.w.Write(jsonBytes)
		return
	}

	var viewFiles []string
	if context.ViewBases != nil {
		viewFiles = make([]string, len(context.ViewBases))
		for i, tmpl := range context.ViewBases {
			viewFiles[i] = mViewDir + "/" + tmpl
		}
	}
	if context.View != "" {
		viewFile := mViewDir + "/" + context.View
		viewFiles = append(viewFiles, viewFile)
	}
	if len(viewFiles) > 0 {
		tem, err := ParseTemplateFiles(context.TemplateFunc, viewFiles...)
		if err != nil {
			log.Println("Error while parsing template:\n" + err.Error())
			http.Error(context.w, "ParseFiles: "+err.Error(), http.StatusInternalServerError)
			return
		}

		if mEnableGzip == 1 {
			context.w.Header().Set("Content-Encoding", "gzip")

			if context.w.Header().Get("Content-Type") == "" {
				context.w.Header().Set("Content-Type", "text/html; charset=utf-8")
			}

			gzWriter := gzip.NewWriter(context.w)
			err = tem.Execute(gzWriter, context.ViewData)
			gzWriter.Flush()

		} else {
			err = tem.Execute(context.w, context.ViewData)
		}

		if err != nil {
			log.Println("Error while executing template:\n" + err.Error())
		}
	}
}