本文整理汇总了Golang中github.com/unrolled/render.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BaseHandlers
func BaseHandlers() *Handlers {
lr := render.New(render.Options{
Layout: "layout",
})
r := render.New(render.Options{})
return &Handlers{LayoutRender: lr, Render: r}
}
示例2: NewApp
// This function is called from main.go and from the tests
// to create a new application.
func NewApp(root string) *App {
CheckEnv()
// Use negroni for middleware
ne := negroni.New()
// Use gorilla/mux for routing
ro := mux.NewRouter()
// Use Render for template. Pass in path to templates folder
// as well as asset helper functions.
re := render.New(render.Options{
Directory: filepath.Join(root, "templates"),
Layout: "layouts/layout",
Extensions: []string{".html"},
Funcs: []template.FuncMap{
AssetHelpers(root),
},
})
qre := render.New(render.Options{
Directory: filepath.Join(root, "templates"),
Layout: "layouts/message",
Extensions: []string{".html"},
Funcs: []template.FuncMap{
AssetHelpers(root),
},
})
// Establish connection to DB as specificed in database.go
db := NewDB()
// Add middleware to the stack
ne.Use(negroni.NewRecovery())
ne.Use(negroni.NewLogger())
ne.Use(NewAssetHeaders())
ne.Use(negroni.NewStatic(http.Dir("public")))
ne.UseHandler(ro)
train.Config.SASS.DebugInfo = true
train.Config.SASS.LineNumbers = true
train.Config.Verbose = true
train.Config.BundleAssets = true
//ZZZtrain.ConfigureHttpHandler(ro)
// Return a new App struct with all these things.
return &App{ne, ro, re, qre, db}
}
示例3: CreateContact
// CreateContact ... add new contact to a person
func (c PersonController) CreateContact(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
r := render.New(render.Options{})
contact := new(models.Contacts)
errs := binding.Bind(req, contact)
if errs.Handle(res) {
r.JSON(res, 422, errs.Error())
return
}
bindingErr := contact.Validate(req, errs)
if bindingErr != nil {
r.JSON(res, 422, bindingErr.Error())
return
}
p := models.Contacts{
BaseModel: c.BaseModel,
PhoneNo: contact.PhoneNo,
PhoneNo2: contact.PhoneNo2,
PhoneNo3: contact.PhoneNo3,
Email: contact.Email,
Website: contact.Website,
FacebookID: contact.FacebookID,
PersonID: contact.PersonID,
CompanyEntitiesID: contact.CompanyEntitiesID}
err := c.DataStore.SaveDatabaseObject(&p)
if err != nil {
panic(err)
}
// render response
r.JSON(res, 200, p)
}
示例4: NewApp
func NewApp() *App {
db := newDB()
// Use negroni for middleware
ne := negroni.New(
negroni.NewRecovery(),
negroni.NewLogger(),
negroni.NewStatic(http.Dir("public")),
)
// Use gorilla/mux for routing
ro := mux.NewRouter()
// Set StrictSlash to allow /things/ to automatically redirect to /things
ro.StrictSlash(true)
// Use Render for template. Pass in path to templates folder
// as well as asset helper functions.
re := render.New(render.Options{
Layout: "layouts/layout",
Extensions: []string{".html"},
})
ne.UseHandler(ro)
return &App{ne, ro, re, db}
}
示例5: CreatePersonIDType
// CreatePersonIDType ... add new contact to a person
func (c PersonController) CreatePersonIDType(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
r := render.New(render.Options{})
personIDType := new(models.PersonIDType)
errs := binding.Bind(req, personIDType)
idTypes := models.IDType{}
if errs.Handle(res) {
r.JSON(res, 422, errs.Error())
return
}
bindingErr := personIDType.Validate(req, errs)
if bindingErr != nil {
r.JSON(res, 422, bindingErr.Error())
return
}
p := models.PersonIDType{
BaseModel: c.BaseModel,
PersonID: personIDType.PersonID,
IDType: personIDType.IDType,
IDNumber: personIDType.IDNumber,
DateIssued: personIDType.DateIssued,
ExpiryDate: personIDType.ExpiryDate,
ScannedPicture: personIDType.ScannedPicture,
IDTypes: idTypes}
err := c.DataStore.SaveDatabaseObject(&p)
if err != nil {
panic(err)
}
r.JSON(res, 200, p)
}
示例6: TestGetRandomMovie
func TestGetRandomMovie(t *testing.T) {
s := &mockStore{}
s.mockGetRandom = func() (*models.Movie, error) {
return &models.Movie{
ImdbID: "tt090909090",
Title: "The Martian",
Actors: "Matt Damon",
}, nil
}
c := AppConfig{
Store: s,
Render: render.New(),
Options: Options{},
}
router := c.Router()
r, err := http.NewRequest("GET", "/api/", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Error("Should be a 200 OK")
}
}
示例7: RegisterUser
//RegisterUser ...
func (client *ClientController) RegisterUser(res http.ResponseWriter, req *http.Request) {
//Extract the models from the request
render := render.New(render.Options{})
registeredType := new(services.RegisteredUser)
errs := binding.Bind(req, registeredType)
userlogic.ServiceList = client.ServiceList
userlogic.Logger = client.Logger
if errs.Handle(res) {
client.Logger.Crit(errs.Error())
render.JSON(res, 422, errs.Error())
return
}
bindingErr := registeredType.Validate(req, errs)
if bindingErr != nil {
client.Logger.Crit(bindingErr.Error())
render.JSON(res, 422, bindingErr.Error())
return
}
person, user, err := userlogic.RegisterUser(registeredType.Person, registeredType.User)
if err != nil {
client.Logger.Error(err.Error())
panic(err)
}
render.JSON(res, 200, map[string]interface{}{"Person": person, "User": user})
}
示例8: main
func main() {
fmt.Println("jøkulhlaup ", Version)
r := render.New(render.Options{})
m := martini.Classic()
fizz := fizz.New()
// Dashboard
m.Get("/", func(w http.ResponseWriter, req *http.Request) {
data := map[string]string{
"title": "Jøkulhlaup",
"imgsrc": "img/jøkulhlaup.png",
"width": "1440",
"height": "900",
}
// !! Reload template !!
//r = render.New(render.Options{})
// Render the specified templates/.tmpl file as HTML and return
r.HTML(w, http.StatusOK, "black", data)
})
// Activate the permission middleware
m.Use(fizz.All())
// Share the files in static
m.Use(martini.Static("static"))
m.Run() // port 3000 by default
}
示例9: CreateAccount
// CreateAccount ... add new contact to a person
func (acct AccountController) CreateAccount(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
r := render.New(render.Options{})
account := new(models.Account)
errs := binding.Bind(req, account)
if errs.Handle(res) {
r.JSON(res, 422, errs.Error())
return
}
bindingErr := account.Validate(req, errs)
if bindingErr != nil {
r.JSON(res, 422, bindingErr.Error())
return
}
p := models.Account{acct.BaseModel, account.AccountType, account.AccountNo, account.AccountCategoryID, account.AccountFundSource, account.AccountFundSourceID,
account.AccountLimit, account.AccountLimitID, account.MaxBalance, account.MinBalance, account.CurrentBalance, account.CustomerID, account.Customer}
//p := models.User{acct.BaseModel, user.Realm, user.Username, user.Password, user.Credential, user.Challenges, user.Email, user.Emailverified, user.Verificationtoken,
// user.LogInCummulative, user.FailedAttemptedLogin, uuid.New(), user.PersonID, user.PhoneNum, user.VerifiedPhoneNum}
err := acct.DataStore.SaveDatabaseObject(&p)
if err != nil {
panic(err)
}
// render response
r.JSON(res, 200, p)
}
示例10: CreateAddress
// CreateAddress ... add new address to a person
func (c PersonController) CreateAddress(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
r := render.New(render.Options{})
address := new(models.Addresses)
errs := binding.Bind(req, address)
bs, _ := ioutil.ReadAll(req.Body)
if errs.Handle(res) {
c.Logger.Error(fmt.Sprintf("%s, %s", errs.Error(), string(bs)))
r.JSON(res, 422, errs.Error())
return
}
bindingErr := address.Validate(req, errs)
if bindingErr != nil {
r.JSON(res, 422, bindingErr.Error())
return
}
p := &models.Addresses{
BaseModel: c.BaseModel,
AddressType: address.AddressType,
HouseNo: address.HouseNo,
Street: address.Street,
Area: address.Area,
TownsID: address.TownsID,
RegionStateID: address.RegionStateID,
CountryID: address.CountryID,
PersonID: address.PersonID,
CompanyEntitiesID: address.CompanyEntitiesID}
err := c.DataStore.SaveDatabaseObject(p)
if err != nil {
panic(err)
}
c.Logger.Info(fmt.Sprintf("%v", &p))
r.JSON(res, 200, p)
}
示例11: CreateCountry
// CreateCountry ... create new country
func (c CommonController) CreateCountry(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
r := render.New(render.Options{})
country := new(models.Country)
errs := binding.Bind(req, country)
if errs.Handle(res) {
r.JSON(res, 422, errs.Error())
return
}
bindingErr := country.Validate(req, errs)
if bindingErr != nil {
r.JSON(res, 422, bindingErr.Error())
return
}
// save to database
p := models.Country{c.BaseModel, country.ISOCode, country.Name, country.RegionStates, country.Language, country.LanguageID}
err := c.DataStore.SaveDatabaseObject(&p)
if err != nil {
fmt.Println(err.Error())
panic(err)
} else {
r.JSON(res, 200, p)
}
// render response
}
示例12: CreateGood
// CreateGood ... create new merchant
func (mer MerchantController) CreateGood(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
r := render.New(render.Options{})
good := new(models.Goods)
errs := binding.Bind(req, good)
if errs.Handle(res) {
r.JSON(res, 422, errs.Error())
return
}
bindingErr := good.Validate(req, errs)
if bindingErr != nil {
r.JSON(res, 422, bindingErr.Error())
return
}
// save to database
p := models.Goods{mer.BaseModel, good.MerchantID, good.PercentDiscount, good.AvailFrom, good.AvailTo, good.Promo, good.UnitPrice, good.GoodServices, good.GoodserviceID,
good.GoodCategoryID, good.GoodHist}
err := mer.DataStore.SaveDatabaseObject(&p)
if err != nil {
panic(err)
}
// render response
r.JSON(res, 200, p)
}
示例13: Participate
func Participate(config ConfigFactory, logger func(interface{})) func(http.ResponseWriter, *http.Request) {
cfg := *config()
return func(w http.ResponseWriter, req *http.Request) {
r := render.New()
if req.Method != "POST" {
r.JSON(w, http.StatusMethodNotAllowed, "Method not supported")
fmt.Errorf("ERROR: method not supported")
return
}
preq := ParticipateRequest{}
body, rerr := ioutil.ReadAll(req.Body)
if rerr != nil {
r.JSON(w, http.StatusBadRequest, fmt.Sprintf("Error reading body: %s", rerr))
fmt.Errorf("ERROR: reading body: %v", rerr)
return
}
err := json.Unmarshal(body, &preq)
if err != nil {
r.JSON(w, http.StatusBadRequest, fmt.Sprintf("Error decoding json: %s", err))
fmt.Errorf("ERROR: decoding json: %s received: %s", err, string(body))
return
}
presp := new(ParticipateResponse)
presp.Experiments = CheckExperiments(preq.Experiments, preq.Context.Uid, cfg)
presp.Features = CheckFeatures(preq.Features, preq.Context.Uid, cfg)
r.JSON(w, http.StatusOK, presp)
go logger(*presp)
}
}
示例14: CreateMerchant
// CreateMerchant ... create new merchant
func (mer MerchantController) CreateMerchant(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
r := render.New(render.Options{})
merchant := new(models.Merchant)
errs := binding.Bind(req, merchant)
if errs.Handle(res) {
r.JSON(res, 422, errs.Error())
return
}
bindingErr := merchant.Validate(req, errs)
if bindingErr != nil {
r.JSON(res, 422, bindingErr.Error())
return
}
// save to database
p := models.Merchant{mer.BaseModel, merchant.Account, merchant.AccountID, merchant.Customer, merchant.CustomerID, merchant.AlternativeID, merchant.Goods}
err := mer.DataStore.SaveDatabaseObject(&p)
if err != nil {
panic(err)
}
// render response
r.JSON(res, 200, p)
}
示例15: RingApi
func RingApi(w http.ResponseWriter, r *http.Request, worker *Worker) {
var ring interface{}
u, _ := url.Parse(r.URL.String())
queryParams := u.Query()
switch queryParams.Get("tracker") {
case "snowplow":
switch queryParams.Get("ring") {
case "success":
ring = worker.Stats.SnowplowSuccessRing.Display()
case "failed":
ring = worker.Stats.SnowplowFailRing.Display()
}
case "adjust":
switch queryParams.Get("ring") {
case "success":
ring = worker.Stats.AdjustSuccessRing.Display()
case "failed":
ring = worker.Stats.AdjustFailRing.Display()
}
}
rndr := render.New()
b, err := json.MarshalIndent(ring, "", " ")
if err != nil {
rndr.Text(w, http.StatusBadRequest, "Cant draw pretty JSON")
return
}
rndr.Text(w, http.StatusOK, string(b))
}