18 changed files with 271 additions and 88 deletions
-
2README.md
-
24api/v1/tokens/route.go
-
43api/v1/tokens/tokens.go
-
24api/v1/users/route.go
-
20api/v1/users/users.go
-
8api/v1/v1.go
-
4cmd/add/add.go
-
15cmd/add/admin/admin.go
-
57cmd/root.go
-
90cmd/server/server.go
-
2lib/common/common.go
-
13lib/database/database.go
-
17lib/utils/utils.go
-
2main.go
-
2models/token.go
-
6models/user.go
-
26repositories/token.go
-
4repositories/user.go
@ -0,0 +1,24 @@ |
|||
package tokens |
|||
|
|||
import "github.com/gin-gonic/gin" |
|||
|
|||
// ApplyRoutes applies router to the gin Engine
|
|||
func ApplyRoutes(r *gin.RouterGroup) *gin.RouterGroup { |
|||
tokens := r.Group("/tokens") |
|||
{ |
|||
tokens.POST("", Create) |
|||
// tokens.GET("/:id", middlewares.Authorized, ReadAction)
|
|||
tokens.GET("/:token", Read) |
|||
// tokens.GET("/:id/logs", middlewares.Authorized, actionlogs.ReadActionLogs)
|
|||
// tokens.PATCH("/:id", middlewares.Authorized, UpdateAction)
|
|||
// tokens.PATCH("", middlewares.Authorized, UpdateActions)
|
|||
// tokens.DELETE("/:id", middlewares.Authorized, DeleteAction)
|
|||
// tokens.DELETE("", middlewares.Authorized, DeleteActions)
|
|||
// tokens.GET("/:id/properties/*path", middlewares.Authorized, actionproperties.ReadActionProperty)
|
|||
// tokens.GET("/:id/properties", middlewares.Authorized, actionproperties.ReadActionProperties)
|
|||
// tokens.PUT("/:id/properties/*path", middlewares.Authorized, actionproperties.UpsertActionProperty)
|
|||
// tokens.PUT("/:id/properties", middlewares.Authorized, actionproperties.ReplaceActionProperties)
|
|||
// tokens.DELETE("/:id/properties/*path", middlewares.Authorized, actionproperties.DeleteActionProperty)
|
|||
} |
|||
return r |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
package tokens |
|||
|
|||
import ( |
|||
"time" |
|||
|
|||
"git.devices.local/mawas/golang-api-skeleton/lib/cache" |
|||
"git.devices.local/mawas/golang-api-skeleton/lib/common" |
|||
"git.devices.local/mawas/golang-api-skeleton/models" |
|||
"git.devices.local/mawas/golang-api-skeleton/repositories" |
|||
"git.devices.local/mawas/golang-api-skeleton/services" |
|||
"github.com/gin-gonic/gin" |
|||
"gorm.io/gorm" |
|||
) |
|||
|
|||
func Create(c *gin.Context) { |
|||
// var requestBody models.Token
|
|||
db := c.MustGet("db").(*gorm.DB) |
|||
cc := c.MustGet("cache").(cache.Cache) |
|||
// if err := c.ShouldBindBodyWith(&requestBody, binding.JSON); err != nil {
|
|||
// return
|
|||
// }
|
|||
tokenRepo := repositories.NewTokenRepository(db, "01F5FSJXDHWT4HK93B9NB8V5G4", "test", cc) |
|||
tokenService := services.NewTokenService(tokenRepo) |
|||
userID, _ := common.StringToGUID("01F5G1W6WJCKQ4PPRS36RYGQ08") |
|||
t := &models.Token{ |
|||
ExpiresAt: time.Now().Add(24 * time.Hour), |
|||
UserID: userID, |
|||
Active: true, |
|||
} |
|||
token, _ := tokenService.Create(t) |
|||
c.JSON(200, token) |
|||
} |
|||
|
|||
func Read(c *gin.Context) { |
|||
db := c.MustGet("db").(*gorm.DB) |
|||
cc := c.MustGet("cache").(cache.Cache) |
|||
repo := repositories.NewTokenRepository(db, "01F5FSJXDHWT4HK93B9NB8V5G4", "test", cc) |
|||
service := services.NewTokenService(repo) |
|||
t := c.Param("token") |
|||
token, _ := service.ReadByKey(t) |
|||
c.JSON(200, token) |
|||
|
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
package users |
|||
|
|||
import "github.com/gin-gonic/gin" |
|||
|
|||
// ApplyRoutes applies router to the gin Engine
|
|||
func ApplyRoutes(r *gin.RouterGroup) *gin.RouterGroup { |
|||
users := r.Group("/users") |
|||
{ |
|||
// actions.POST("", middlewares.Authorized, CreateActions)
|
|||
// actions.GET("/:id", middlewares.Authorized, ReadAction)
|
|||
users.GET("/:username", Read) |
|||
// actions.GET("/:id/logs", middlewares.Authorized, actionlogs.ReadActionLogs)
|
|||
// actions.PATCH("/:id", middlewares.Authorized, UpdateAction)
|
|||
// actions.PATCH("", middlewares.Authorized, UpdateActions)
|
|||
// actions.DELETE("/:id", middlewares.Authorized, DeleteAction)
|
|||
// actions.DELETE("", middlewares.Authorized, DeleteActions)
|
|||
// actions.GET("/:id/properties/*path", middlewares.Authorized, actionproperties.ReadActionProperty)
|
|||
// actions.GET("/:id/properties", middlewares.Authorized, actionproperties.ReadActionProperties)
|
|||
// actions.PUT("/:id/properties/*path", middlewares.Authorized, actionproperties.UpsertActionProperty)
|
|||
// actions.PUT("/:id/properties", middlewares.Authorized, actionproperties.ReplaceActionProperties)
|
|||
// actions.DELETE("/:id/properties/*path", middlewares.Authorized, actionproperties.DeleteActionProperty)
|
|||
} |
|||
return r |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
package users |
|||
|
|||
import ( |
|||
"git.devices.local/mawas/golang-api-skeleton/lib/cache" |
|||
"git.devices.local/mawas/golang-api-skeleton/repositories" |
|||
"git.devices.local/mawas/golang-api-skeleton/services" |
|||
"github.com/gin-gonic/gin" |
|||
"gorm.io/gorm" |
|||
) |
|||
|
|||
func Read(c *gin.Context) { |
|||
db := c.MustGet("db").(*gorm.DB) |
|||
cc := c.MustGet("cache").(cache.Cache) |
|||
userRepo := repositories.NewUserRepository(db, "01F5FSJXDHWT4HK93B9NB8V5G4", "test", cc) |
|||
userService := services.NewUserService(userRepo) |
|||
username := c.Param("username") |
|||
user, _ := userService.ReadByID(username) |
|||
c.JSON(200, user) |
|||
|
|||
} |
|||
@ -1,11 +1,17 @@ |
|||
package v1 |
|||
|
|||
import "github.com/gin-gonic/gin" |
|||
import ( |
|||
"git.devices.local/mawas/golang-api-skeleton/api/v1/tokens" |
|||
"git.devices.local/mawas/golang-api-skeleton/api/v1/users" |
|||
"github.com/gin-gonic/gin" |
|||
) |
|||
|
|||
func ApplyRoutes(r *gin.RouterGroup) *gin.RouterGroup { |
|||
v1 := r.Group("/v1") |
|||
{ |
|||
v1.GET("/ping") |
|||
users.ApplyRoutes(v1) |
|||
tokens.ApplyRoutes(v1) |
|||
} |
|||
return v1 |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue