Browse Source

chore: plaining

main
Evan 7 months ago
parent
commit
41f45ccdf8
  1. 13
      modules/authenticate/middleware.go
  2. 4
      services/auth/auth.go
  3. 35
      services/auth/user.go
  4. 53
      services/calendar/calendar.go
  5. 58
      services/calendar/event.go
  6. 6
      services/services.go
  7. 1
      services/settings.go
  8. 23
      services/system/settings.go

13
modules/authenticate/middleware.go

@ -16,6 +16,8 @@ import (
var db *gorm.DB
var shouldSecure bool
const cookieName = "GCSID"
type Session struct {
ID uint `gorm:"primary_key"`
SessionID string
@ -38,7 +40,7 @@ func Sessions() gin.HandlerFunc {
shouldSecure = true
}
return func(c *gin.Context) {
sessionID, err := c.Cookie("GCSID")
sessionID, err := c.Cookie(cookieName)
if err != nil {
c.Next()
return
@ -48,7 +50,7 @@ func Sessions() gin.HandlerFunc {
c.Next()
return
}
c.Set("GCSID", session)
c.Set(cookieName, session)
c.Next()
}
}
@ -61,11 +63,11 @@ func LoginUser(c *gin.Context, userID uint) {
if err := db.Create(&session).Error; err != nil {
return
}
c.SetCookie("GCSID", session.SessionID, 60*30, "/", c.Request.Host, shouldSecure, true)
c.SetCookie(cookieName, session.SessionID, 60*30, "/", c.Request.Host, shouldSecure, true)
}
func LogoutUser(c *gin.Context) {
s, ok := c.Get("GCSID")
s, ok := c.Get(cookieName)
if !ok {
return
}
@ -73,12 +75,13 @@ func LogoutUser(c *gin.Context) {
if !ok {
return
}
c.SetCookie(cookieName, "", -1, "/", c.Request.Host, shouldSecure, true)
db.Delete(&session)
}
// LoadUser loads user and aborts if permition denied
func LoadUser(c *gin.Context, level ...constants.Role) (user models.User, ok bool) {
s, ok := c.Get("GCSID")
s, ok := c.Get(cookieName)
if !ok {
c.AbortWithStatus(401)
return

4
services/auth/auth.go

@ -23,7 +23,6 @@ func RegisterAuthService(api *gin.RouterGroup) {
// @tags Auth
// @Produce json
// @Success 200
// @Failure 400
// @Router /auth [get]
func getAuthStatus(c *gin.Context) {
user, ok := authenticate.LoadUser(c)
@ -48,7 +47,6 @@ type loginRequest struct {
// @Produce json
// @Param body body loginRequest true "Request Body"
// @Success 200
// @Failure 400
// @Router /auth/login [post]
func login(c *gin.Context) {
var param loginRequest
@ -80,7 +78,6 @@ func login(c *gin.Context) {
// @tags Auth
// @Produce json
// @Success 200
// @Failure 400
// @Router /auth/logout [post]
func logout(c *gin.Context) {
authenticate.LogoutUser(c)
@ -92,7 +89,6 @@ func logout(c *gin.Context) {
// @tags Auth
// @Produce json
// @Success 200
// @Failure 400
// @Router /auth/token [post]
func token(c *gin.Context) {
user, ok := authenticate.LoadUser(c)

35
services/auth/user.go

@ -20,6 +20,7 @@ func RegisterUserService(api *gin.RouterGroup) {
user.GET("/:id", getUser)
user.PUT("/:id", updateUser)
user.DELETE("/:id", deleteUser)
user.PUT(":id/passwd", changeUserPassword)
}
type userInsertResource struct {
@ -35,7 +36,6 @@ type userInsertResource struct {
// @Accept json
// @Produce json
// @Success 200
// @Failure 400 {object} resource.RequestError
// @Router /user [post]
func postUser(c *gin.Context) {
var data userInsertResource
@ -70,7 +70,6 @@ func postUser(c *gin.Context) {
// @tags User
// @Produce json
// @Success 200
// @Failure 400
// @Router /user [get]
func listUsers(c *gin.Context) {
// models.ListUsers
@ -83,9 +82,8 @@ func listUsers(c *gin.Context) {
// @Description Retrieve a specific user.
// @tags User
// @Param id path int true "user id"
// @Produce json
// @Produce json
// @Success 200
// @Failure 400
// @Router /user/{id} [get]
func getUser(c *gin.Context) {
userID, err := strconv.Atoi(c.Param("id"))
@ -108,7 +106,6 @@ func getUser(c *gin.Context) {
// @Accept json
// @Produce json
// @Success 200
// @Failure 400
// @Router /user/{id} [put]
func updateUser(c *gin.Context) {
c.JSON(200, gin.H{
@ -123,10 +120,36 @@ func updateUser(c *gin.Context) {
// @Accept json
// @Produce json
// @Success 200
// @Failure 400
// @Router /user/{id} [delete]
func deleteUser(c *gin.Context) {
c.JSON(200, gin.H{
"message": "delete user",
})
}
type changeUserPasswordRequest struct {
PwdB64 string `json:"password" example:"YWRtaW4=" binding:"required"`
}
// @Summary Change user password.
// @Description Change user password.
// @tags User
// @Param id path int true "user id"
// @Param user body changeUserPasswordRequest true "user"
// @Accept json
// @Produce json
// @Success 200
// @Router /user/{id}/passwd [put]
func changeUserPassword(c *gin.Context) {
var data changeUserPasswordRequest
if err := c.ShouldBindJSON(&data); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"message": err.Error(),
})
return
}
c.JSON(200, gin.H{
"message": "change user password",
"new": data.PwdB64,
})
}

53
services/calendar/calendar.go

@ -9,12 +9,16 @@ import (
)
func RegisterCalendarService(api *gin.RouterGroup) {
auth := api.Group("/calendar")
auth.GET("/", listCalendars)
auth.POST("/", postCalendar)
auth.GET("/:calendarID", getCalendar)
auth.PUT("/:calendarID", putCalendar)
auth.DELETE("/:calendarID", deleteCalendar)
route := api.Group("/calendar")
route.GET("/", listCalendars)
route.POST("/", postCalendar)
route.GET("/:calendarID", getCalendar)
route.PUT("/:calendarID", putCalendar)
route.DELETE("/:calendarID", deleteCalendar)
ics := api.Group("/ics")
ics.POST("/", importCalendarICS)
ics.GET("/:calendarID", getCalendarICS)
}
@ -24,7 +28,6 @@ func RegisterCalendarService(api *gin.RouterGroup) {
// @tags Calendar
// @Produce plain
// @Success 200
// @Failure 400
// @Router /calendar [get]
func listCalendars(c *gin.Context) {
c.String(http.StatusOK, "Hello world")
@ -36,11 +39,13 @@ func listCalendars(c *gin.Context) {
// @Produce json
// @Param calendarID path string true "calendarID" format(uuid)
// @Success 200
// @Failure 400
// @Router /calendar/{calendarID} [get]
func getCalendar(c *gin.Context) {
calendarID := c.Param("calendarID")
logger.Dbg.Println(calendarID)
c.JSON(200, gin.H{
"message": "update user",
})
}
// @Summary Insert new calendar
@ -48,7 +53,6 @@ func getCalendar(c *gin.Context) {
// @tags Calendar
// @Produce json
// @Success 200
// @Failure 400
// @Router /calendar [post]
func postCalendar(c *gin.Context) {
models.InsertCalendar()
@ -60,8 +64,7 @@ func postCalendar(c *gin.Context) {
// @Produce json
// @Param calendarID path string true "calendarID" format(uuid)
// @Success 200
// @Failure 400
// @Router /calendar/{calendarID} [post]
// @Router /calendar/{calendarID} [put]
func putCalendar(c *gin.Context) {
}
@ -71,7 +74,33 @@ func putCalendar(c *gin.Context) {
// @Produce json
// @Param calendarID path string true "calendarID" format(uuid)
// @Success 200
// @Failure 400
// @Router /calendar/{calendarID} [delete]
func deleteCalendar(c *gin.Context) {
}
// @Summary Import Calendar ics file
// @Description Import Calendar ics file
// @tags Calendar
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "this is a test file"
// @Success 200
// @Router /ics [post]
func importCalendarICS(c *gin.Context) {
c.JSON(200, gin.H{
"message": "import ics",
})
}
// @Summary Get Calendar ics file
// @Description Get Calendar ics file
// @tags Calendar
// @Produce text/calendar
// @Param calendarID path string true "calendarID" format(uuid)
// @Success 200
// @Router /ics/{calendarID} [get]
func getCalendarICS(c *gin.Context) {
c.JSON(200, gin.H{
"message": "update user",
})
}

58
services/calendar/event.go

@ -7,11 +7,65 @@ import (
)
func RegisterEventService(api *gin.RouterGroup) {
auth := api.Group("/event")
auth.GET("/", list_event)
route := api.Group("/calendar")
route.GET("/:calendarID/event", list_event)
route.GET("/:calendarID/event/:eventID", get_event)
route.POST("/:calendarID/event", insert_event)
route.PUT("/:calendarID/event/:eventID", update_event)
route.DELETE("/:calendarID/event", delete_event)
}
// @Summary List all events
// @Description List all events
// @tags Event
// @Produce json
// @Param calendarID path string true "calendarID" format(uuid)
// @Success 200
// @Router /calendar/{calendarID}/event [get]
func list_event(c *gin.Context) {
c.String(http.StatusOK, "Hello world")
}
// @Summary Get event
// @Description Get event
// @tags Event
// @Produce json
// @Param calendarID path string true "calendarID" format(uuid)
// @Param eventID path string true "eventID" format(uuid)
// @Success 200
// @Router /calendar/{calendarID}/event/{eventID} [get]
func get_event(c *gin.Context) {
}
// @Summary Insert event
// @Description Insert event
// @tags Event
// @Produce json
// @Param calendarID path string true "calendarID" format(uuid)
// @Success 200
// @Router /calendar/{calendarID}/event [post]
func insert_event(c *gin.Context) {
}
// @Summary Update event
// @Description Update event
// @tags Event
// @Produce json
// @Param calendarID path string true "calendarID" format(uuid)
// @Param eventID path string true "eventID" format(uuid)
// @Success 200
// @Router /calendar/{calendarID}/event/{eventID} [put]
func update_event(c *gin.Context) {
}
// @Summary Delete event
// @Description Delete event
// @tags Event
// @Produce json
// @Param calendarID path string true "calendarID" format(uuid)
// @Param eventID path string true "eventID" format(uuid)
// @Success 200
// @Router /calendar/{calendarID}/event/{eventID} [delete]
func delete_event(c *gin.Context) {
}

6
services/services.go

@ -11,6 +11,7 @@ import (
"github.com/mutsuki333/calendar/modules/static"
"github.com/mutsuki333/calendar/services/auth"
"github.com/mutsuki333/calendar/services/calendar"
"github.com/mutsuki333/calendar/services/system"
"github.com/spf13/viper"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
@ -35,6 +36,10 @@ var Version = "development"
// @tag.description User manage related apis
// @tag.name Calendar
// @tag.description Calendar manage related apis
// @tag.name Event
// @tag.description Event manage related apis
// @tag.name System
// @tag.description System manage related apis
func Start() {
if viper.GetString("app.mode") != "debug" {
gin.SetMode(gin.ReleaseMode)
@ -45,6 +50,7 @@ func Start() {
auth.RegisterUserService(api)
calendar.RegisterCalendarService(api)
calendar.RegisterEventService(api)
system.RegisterSystemService(api)
if !viper.GetBool("ui.disabled") {
if viper.GetBool("swagger.enabled") {

1
services/settings.go

@ -1 +0,0 @@
package services

23
services/system/settings.go

@ -0,0 +1,23 @@
package system
import (
"github.com/gin-gonic/gin"
)
func RegisterSystemService(api *gin.RouterGroup) {
route := api.Group("/system")
route.GET("/status", status)
}
// @Summary Get system status
// @Description Get system status
// @tags System
// @Produce json
// @Success 200
// @Router /system/status [get]
func status(c *gin.Context) {
c.JSON(200, gin.H{
"message": "ready",
})
}