2023-09-08 22:25:38 +07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
db "git.nochill.in/nochill/hiling_go/db/sqlc"
|
|
|
|
"git.nochill.in/nochill/hiling_go/util"
|
|
|
|
"git.nochill.in/nochill/hiling_go/util/token"
|
2023-09-27 21:58:14 +07:00
|
|
|
"github.com/gin-contrib/cors"
|
2023-09-08 22:25:38 +07:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-09-13 21:42:31 +07:00
|
|
|
Config util.Config
|
|
|
|
Store db.Store
|
|
|
|
TokenMaker token.Maker
|
2023-09-12 17:07:57 +07:00
|
|
|
Router *gin.Engine
|
2023-09-08 22:25:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(config util.Config, store db.Store) (*Server, error) {
|
|
|
|
tokenMaker, err := token.NewPasetoMaker(config.TokenSymmetricKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create token maker: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server := &Server{
|
2023-09-13 21:42:31 +07:00
|
|
|
Config: config,
|
|
|
|
Store: store,
|
|
|
|
TokenMaker: tokenMaker,
|
2023-09-08 22:25:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
server.getRoutes()
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) getRoutes() {
|
|
|
|
router := gin.Default()
|
|
|
|
|
2023-09-27 21:58:14 +07:00
|
|
|
router.Use(cors.New(cors.Config{
|
|
|
|
AllowOrigins: []string{"http://localhost:5173"},
|
|
|
|
AllowCredentials: true,
|
2023-10-04 19:51:29 +07:00
|
|
|
AllowHeaders: []string{"Content-Type", "Content-Length", "Accept-Encoding", "Authorization", "accept", "origin", "Cache-Control", "X-Requested-With", "X-XSRF-TOKEN"},
|
2023-09-27 21:58:14 +07:00
|
|
|
AllowMethods: []string{"POST", "PUT", "GET", "DELETE", "PATCH"},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// router.Use(CORSMiddleware())
|
2023-09-14 22:51:01 +07:00
|
|
|
|
2023-09-12 17:07:57 +07:00
|
|
|
router.POST("/user/signup", server.createUser)
|
2023-09-23 15:24:21 +07:00
|
|
|
router.POST("/user/login", server.login)
|
|
|
|
router.POST("/user/logout", server.logout)
|
2023-10-02 08:49:41 +07:00
|
|
|
router.GET("/regions", server.getListRegions)
|
|
|
|
router.GET("/region/provinces", server.getListProvinces)
|
|
|
|
router.GET("/region/regencies", server.getListRegencies)
|
2023-09-08 22:25:38 +07:00
|
|
|
|
2023-09-13 21:42:31 +07:00
|
|
|
// LOCATION
|
|
|
|
router.POST("/locations", server.createLocation)
|
2023-09-17 16:32:48 +07:00
|
|
|
router.GET("/locations/recent", server.getListRecentLocationsWithRatings)
|
|
|
|
router.GET("/locations/top-ratings", server.getTopListLocations)
|
2023-09-13 21:42:31 +07:00
|
|
|
router.GET("/locations", server.getListLocations)
|
|
|
|
router.GET("/location/:location_id", server.getLocation)
|
2023-09-20 13:37:14 +07:00
|
|
|
router.GET("/location/tags/:location_id", server.getTagsByLocation)
|
2023-09-27 21:58:14 +07:00
|
|
|
router.GET("/location/reviews", server.getListLocationReviews)
|
2023-09-13 21:42:31 +07:00
|
|
|
|
2023-09-19 21:49:48 +07:00
|
|
|
//IMAGES
|
|
|
|
router.GET("/images/location", server.getAllImagesByLocation)
|
|
|
|
|
2023-09-27 21:58:14 +07:00
|
|
|
// REQUIRE AUTH TOKEN
|
|
|
|
authRoutes := router.Use(authMiddleware(server.TokenMaker))
|
|
|
|
authRoutes.POST("/review/location", server.createReview)
|
2023-10-09 16:25:39 +07:00
|
|
|
authRoutes.PATCH("/user", server.updateUser)
|
2023-09-27 21:58:14 +07:00
|
|
|
authRoutes.GET("/user/review/location/:location_id", server.getUserReviewByLocation)
|
2023-10-04 19:51:29 +07:00
|
|
|
authRoutes.GET("/user/profile", server.getUserStats)
|
2023-10-09 16:25:39 +07:00
|
|
|
authRoutes.PATCH("/user/avatar", server.updateUserAvatar)
|
|
|
|
authRoutes.DELETE("/user/avatar", server.removeAvatar)
|
2023-09-27 21:58:14 +07:00
|
|
|
|
2023-09-12 17:07:57 +07:00
|
|
|
server.Router = router
|
2023-09-08 22:25:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) Start(address string) error {
|
2023-09-12 17:07:57 +07:00
|
|
|
return server.Router.Run(address)
|
2023-09-08 22:25:38 +07:00
|
|
|
}
|