本文整理匯總了Golang中github.com/gin-gonic/gin.Engine類的典型用法代碼示例。如果您正苦於以下問題:Golang Engine類的具體用法?Golang Engine怎麽用?Golang Engine使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Engine類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: PostLike
func PostLike(db gorm.DB, router *gin.Engine) {
// POST /like
// POST new like to database
router.POST("/like", func(c *gin.Context) {
var likeData model.LikeData
if err := c.BindJSON(&likeData); err == nil {
like := &model.Like{
LikeData: likeData,
}
var x_user uint64
if resp := c.Request.Header.Get(userHeader); resp != "" {
conv_user, _ := strconv.ParseUint(resp, 10, 64)
x_user = conv_user
like.Trace.CreatedBy = x_user
like.Trace.UpdatedBy = x_user
if err := checkDataLike(like.LikeData); err {
if err := db.Create(&like).Error; err != nil {
c.AbortWithError(http.StatusBadRequest, err)
} else {
c.JSON(http.StatusCreated, like)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
} else {
c.AbortWithStatus(http.StatusForbidden)
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}
示例2: prepareRoutes
func prepareRoutes(router *gin.Engine) {
// Web Resources
router.Static("/static", "web/dist")
// API Routes
api := router.Group("/api/v1")
admin := api.Group("/admin")
public := api.Group("/public")
registered := api.Group("/")
sameRegisteredUser := api.Group("/user")
prepareMiddleware(admin, public, registered, sameRegisteredUser)
admin.GET("/users", listUsers)
admin.GET("/places", listPlaces)
admin.POST("/places", createPlace)
admin.POST("/events", createEvent)
admin.PUT("/place/:placeId", updatePlace)
admin.PUT("/event/:eventId", updateEvent)
admin.DELETE("/event/:eventId", cancelEvent)
sameRegisteredUser.GET("/:userId", getUser)
sameRegisteredUser.PUT("/:userId", updateUser)
sameRegisteredUser.DELETE("/:userId", disableUser)
registered.POST("/buy/:seatId", buyTicket)
public.GET("/place/:placeId", getPlace)
public.GET("/events", listEvents)
public.GET("/event/:eventId", getEvent)
public.POST("/users", createUser) // TODO Checar, me huele raro......
public.POST("/login", loginUser)
}
示例3: setTemplate
//setTemplate loads templates from rice box "views"
func setTemplate(router *gin.Engine) {
box := rice.MustFindBox("views")
tmpl := template.New("").Funcs(template.FuncMap{
"isActive": helpers.IsActive,
"stringInSlice": helpers.StringInSlice,
"dateTime": helpers.DateTime,
"recentPosts": helpers.RecentPosts,
"tags": helpers.Tags,
"archives": helpers.Archives,
})
fn := func(path string, f os.FileInfo, err error) error {
if f.IsDir() != true && strings.HasSuffix(f.Name(), ".html") {
var err error
tmpl, err = tmpl.Parse(box.MustString(path))
if err != nil {
return err
}
}
return nil
}
err := box.Walk("", fn)
if err != nil {
panic(err)
}
router.SetHTMLTemplate(tmpl)
}
示例4: PutGroupAdmin
func PutGroupAdmin(db gorm.DB, router *gin.Engine) {
// PUT /group_admin
// Update group_admin data by id
router.PUT("/group_admin/:id", func(c *gin.Context) {
_id := c.Param("id")
if id, err := strconv.ParseUint(_id, 10, 64); err == nil {
var group_adminData model.GroupAdminData
if err := c.BindJSON(&group_adminData); err == nil {
group_admin := &model.GroupAdmin{
GroupAdminData: group_adminData,
GroupAdminId: model.GroupAdminId{Id: id},
}
if err := checkDataGroupAdmin(group_admin.GroupAdminData); err {
checkGroupAdmin := &model.GroupAdmin{
GroupAdminData: group_adminData,
GroupAdminId: model.GroupAdminId{Id: id},
}
if err := db.First(checkGroupAdmin).Error; err == nil {
db.Save(&group_admin)
c.JSON(http.StatusOK, group_admin)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}
示例5: PutWallEntry
func PutWallEntry(db gorm.DB, router *gin.Engine) {
// PUT /wall_entry
// Update wall_entry data by id
router.PUT("/wall_entry/:id", func(c *gin.Context) {
_id := c.Param("id")
if id, err := strconv.ParseUint(_id, 10, 64); err == nil {
var wall_entryData model.WallEntryData
if err := c.BindJSON(&wall_entryData); err == nil {
wall_entry := &model.WallEntry{
WallEntryData: wall_entryData,
WallEntryId: model.WallEntryId{Id: id},
}
if err := checkDataWallEntry(wall_entry.WallEntryData); err {
checkWallEntry := &model.WallEntry{
WallEntryData: wall_entryData,
WallEntryId: model.WallEntryId{Id: id},
}
if err := db.First(checkWallEntry).Error; err == nil {
db.Save(&wall_entry)
c.JSON(http.StatusOK, wall_entry)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}
示例6: AddRepoRoutes
func AddRepoRoutes(s *store.Engine, r *gin.Engine) {
r.GET("/users/:username/repos", func(c *gin.Context) {
var repos []*Repo
for _, repo := range s.State.Repos {
if repo.RepoOwner.Username == c.Param("username") {
repos = append(repos, repo)
}
}
c.JSON(http.StatusOK, repos)
})
r.GET("/users/:username/repos/:reponame", func(c *gin.Context) {
var repo *Repo
for _, rp := range s.State.Repos {
if rp.RepoOwner.Username == c.Param("username") &&
rp.Name == c.Param("reponame") {
repo = rp
break
}
}
c.JSON(http.StatusOK, repo)
})
}
示例7: PutActivity
func PutActivity(db gorm.DB, router *gin.Engine) {
// PUT /activity
// Update activity data by id
router.PUT("/activity/:id", func(c *gin.Context) {
_id := c.Param("id")
if id, err := strconv.ParseUint(_id, 10, 64); err == nil {
var activityData model.ActivityData
if err := c.BindJSON(&activityData); err == nil {
activity := &model.Activity{
ActivityData: activityData,
ActivityId: model.ActivityId{Id: id},
}
if err := checkDataActivity(activity.ActivityData); err {
checkActivity := &model.Activity{
ActivityData: activityData,
ActivityId: model.ActivityId{Id: id},
}
if err := db.First(checkActivity).Error; err == nil {
db.Save(&activity)
c.JSON(http.StatusOK, activity)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}
示例8: PutModuleEnabled
func PutModuleEnabled(db gorm.DB, router *gin.Engine) {
// PUT /module_enabled
// Update module_enabled data by id
router.PUT("/module_enabled/:id", func(c *gin.Context) {
_id := c.Param("id")
if id, err := strconv.ParseUint(_id, 10, 64); err == nil {
var module_enabledData model.ModuleEnabledData
if err := c.BindJSON(&module_enabledData); err == nil {
module_enabled := &model.ModuleEnabled{
ModuleEnabledData: module_enabledData,
ModuleEnabledId: model.ModuleEnabledId{Id: id},
}
if err := checkDataModuleEnabled(module_enabled.ModuleEnabledData); err {
checkModuleEnabled := &model.ModuleEnabled{
ModuleEnabledData: module_enabledData,
ModuleEnabledId: model.ModuleEnabledId{Id: id},
}
if err := db.First(checkModuleEnabled).Error; err == nil {
db.Save(&module_enabled)
c.JSON(http.StatusOK, module_enabled)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}
示例9: NewUserResource
func NewUserResource(e *gin.Engine) {
u := UserResource{}
// Setup Routes
e.GET("/users", u.getAllUsers)
e.GET("/users/:id", u.getUserByID)
}
示例10: PutProfileFieldCategory
func PutProfileFieldCategory(db gorm.DB, router *gin.Engine) {
// PUT /profile_field_category
// Update profile_field_category data by id
router.PUT("/profile_field_category/:id", func(c *gin.Context) {
_id := c.Param("id")
if id, err := strconv.ParseUint(_id, 10, 64); err == nil {
var profile_field_categoryData model.ProfileFieldCategoryData
if err := c.BindJSON(&profile_field_categoryData); err == nil {
profile_field_category := &model.ProfileFieldCategory{
ProfileFieldCategoryData: profile_field_categoryData,
ProfileFieldCategoryId: model.ProfileFieldCategoryId{Id: id},
}
if err := checkDataProfileFieldCategory(profile_field_category.ProfileFieldCategoryData); err {
checkProfileFieldCategory := &model.ProfileFieldCategory{
ProfileFieldCategoryData: profile_field_categoryData,
ProfileFieldCategoryId: model.ProfileFieldCategoryId{Id: id},
}
if err := db.First(checkProfileFieldCategory).Error; err == nil {
db.Save(&profile_field_category)
c.JSON(http.StatusOK, profile_field_category)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}
示例11: setupMiddleware
// setupMiddleware is an internal method where we setup GIN middleware
func setupMiddleware(r *gin.Engine) {
// TODO: CACHE_URL should come from an environment variable but this requires
// validating and parsing of the connection url into it's base components.
store, err := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte(config.Config.Session_Secret))
if err != nil {
log.Fatalln("Failed to connect to Redis.", err)
}
r.Use(
secure.Secure(secure.Options{ // TODO: we should get these from config
AllowedHosts: []string{},
SSLRedirect: false,
SSLHost: "",
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
STSSeconds: 315360000,
STSIncludeSubdomains: true,
FrameDeny: true,
ContentTypeNosniff: true,
BrowserXssFilter: true,
ContentSecurityPolicy: "default-src 'self'",
}),
sessions.Sessions("session", store),
auth.UserMiddleware(),
)
}
示例12: PostProfileFieldCategory
func PostProfileFieldCategory(db gorm.DB, router *gin.Engine) {
// POST /profile_field_category
// POST new profile_field_category to database
router.POST("/profile_field_category", func(c *gin.Context) {
var profile_field_categoryData model.ProfileFieldCategoryData
if err := c.BindJSON(&profile_field_categoryData); err == nil {
profile_field_category := &model.ProfileFieldCategory{
ProfileFieldCategoryData: profile_field_categoryData,
}
var x_user uint64
if resp := c.Request.Header.Get(userHeader); resp != "" {
conv_user, _ := strconv.ParseUint(resp, 10, 64)
x_user = conv_user
profile_field_category.Trace.CreatedBy = x_user
profile_field_category.Trace.UpdatedBy = x_user
if err := checkDataProfileFieldCategory(profile_field_category.ProfileFieldCategoryData); err {
if err := db.Create(&profile_field_category).Error; err != nil {
c.AbortWithError(http.StatusBadRequest, err)
} else {
c.JSON(http.StatusCreated, profile_field_category)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
} else {
c.AbortWithStatus(http.StatusForbidden)
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}
示例13: PutUrlOembed
func PutUrlOembed(db gorm.DB, router *gin.Engine) {
// PUT /url_oembed
// Update url_oembed data by id
router.PUT("/url_oembed/:id", func(c *gin.Context) {
_id := c.Param("id")
if id, err := strconv.ParseUint(_id, 10, 64); err == nil {
var url_oembedData model.UrlOembedData
if err := c.BindJSON(&url_oembedData); err == nil {
url_oembed := &model.UrlOembed{
UrlOembedData: url_oembedData,
UrlOembedId: model.UrlOembedId{Id: id},
}
if err := checkDataUrlOembed(url_oembed.UrlOembedData); err {
checkUrlOembed := &model.UrlOembed{
UrlOembedData: url_oembedData,
UrlOembedId: model.UrlOembedId{Id: id},
}
if err := db.First(checkUrlOembed).Error; err == nil {
db.Save(&url_oembed)
c.JSON(http.StatusOK, url_oembed)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}
示例14: InitRoutesTopics
// InitRoutesTopics initialized routes for Topics Controller
func InitRoutesTopics(router *gin.Engine) {
topicsCtrl := &controllers.TopicsController{}
g := router.Group("/")
g.Use(CheckPassword())
{
g.GET("/topics", topicsCtrl.List)
g.POST("/topic", topicsCtrl.Create)
g.DELETE("/topic/*topic", topicsCtrl.Delete)
g.GET("/topic/*topic", topicsCtrl.OneTopic)
g.PUT("/topic/add/parameter", topicsCtrl.AddParameter)
g.PUT("/topic/remove/parameter", topicsCtrl.RemoveParameter)
g.PUT("/topic/add/rouser", topicsCtrl.AddRoUser)
g.PUT("/topic/remove/rouser", topicsCtrl.RemoveRoUser)
g.PUT("/topic/add/rwuser", topicsCtrl.AddRwUser)
g.PUT("/topic/remove/rwuser", topicsCtrl.RemoveRwUser)
g.PUT("/topic/add/adminuser", topicsCtrl.AddAdminUser)
g.PUT("/topic/remove/adminuser", topicsCtrl.RemoveAdminUser)
g.PUT("/topic/add/rogroup", topicsCtrl.AddRoGroup)
g.PUT("/topic/remove/rogroup", topicsCtrl.RemoveRoGroup)
g.PUT("/topic/add/rwgroup", topicsCtrl.AddRwGroup)
g.PUT("/topic/remove/rwgroup", topicsCtrl.RemoveRwGroup)
g.PUT("/topic/add/admingroup", topicsCtrl.AddAdminGroup)
g.PUT("/topic/remove/admingroup", topicsCtrl.RemoveAdminGroup)
g.PUT("/topic/param", topicsCtrl.SetParam)
}
}
示例15: PostNotification
func PostNotification(db gorm.DB, router *gin.Engine) {
// POST /notification
// POST new notification to database
router.POST("/notification", func(c *gin.Context) {
var notificationData model.NotificationData
if err := c.BindJSON(¬ificationData); err == nil {
notification := &model.Notification{
NotificationData: notificationData,
}
var x_user uint64
if resp := c.Request.Header.Get(userHeader); resp != "" {
conv_user, _ := strconv.ParseUint(resp, 10, 64)
x_user = conv_user
notification.Trace.CreatedBy = x_user
notification.Trace.UpdatedBy = x_user
if err := checkDataNotification(notification.NotificationData); err {
if err := db.Create(¬ification).Error; err != nil {
c.AbortWithError(http.StatusBadRequest, err)
} else {
c.JSON(http.StatusCreated, notification)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
} else {
c.AbortWithStatus(http.StatusForbidden)
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}