hiling_go/api/server.go
2026-05-31 12:08:43 +03:00

114 lines
3.7 KiB
Go
Executable File

package api
import (
"fmt"
db "git.nochill.in/nochill/hiling_go/db/repository"
"git.nochill.in/nochill/hiling_go/util"
"git.nochill.in/nochill/hiling_go/util/cloudfare"
"git.nochill.in/nochill/hiling_go/util/token"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/meilisearch/meilisearch-go"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
type Server struct {
Config util.Config
Store db.Store
TokenMaker token.Maker
Router *gin.Engine
MeilisearchClient *meilisearch.Client
R2 *cloudfare.R2Client
}
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)
}
meiliClient := meilisearch.NewClient(meilisearch.ClientConfig{
Host: config.MeilisearchHost,
APIKey: config.MeilisearchKey,
})
r2Client, err := cloudfare.NewR2Client(
config.R2AccountID,
config.R2AccessKeyID,
config.R2SecretAccessKey,
config.R2BucketName,
config.R2PublicApiID,
)
if err != nil {
return nil, fmt.Errorf("cannot create R2 client: %w", err)
}
server := &Server{
Config: config,
Store: store,
TokenMaker: tokenMaker,
MeilisearchClient: meiliClient,
R2: r2Client,
}
server.getRoutes()
return server, nil
}
func (server *Server) getRoutes() {
router := gin.Default()
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:5173", "http://192.168.18.164:5173", "http://192.168.18.150:5173", "http://192.168.1.9:5173"},
AllowCredentials: true,
AllowHeaders: []string{"Content-Type", "Content-Length", "Accept-Encoding", "Authorization", "accept", "origin", "Cache-Control", "X-Requested-With", "X-XSRF-TOKEN"},
AllowMethods: []string{"POST", "PUT", "GET", "DELETE", "PATCH"},
}))
router.POST("/user/signup", server.createUser)
router.POST("/user/login", server.login)
router.POST("/user/logout", server.logout)
router.GET("/regions", server.getListRegions)
router.GET("/region/provinces", server.getListProvinces)
router.GET("/region/regencies", server.getListRegencies)
// LOCATION
router.POST("/locations", server.createLocation)
router.GET("/locations/recent", server.getListRecentLocationsWithRatings)
router.GET("/locations/top-ratings", server.getTopListLocations)
router.GET("/locations", server.getListLocations)
router.GET("/location/:location_id", server.getLocation)
router.GET("/location/tags/:location_id", server.getTagsByLocation)
router.GET("/location/reviews", server.getListLocationReviews)
router.GET("/location/:location_id/review/:review_id", server.getReview)
router.GET("/locations/search", server.searchLocations)
//IMAGES
router.GET("/images/location", server.getAllImagesByLocation)
// NEWS / EVENTS
router.GET("/news-events", server.GetNewsEventsList)
// SWAGGER
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// REQUIRE AUTH TOKEN
authRoutes := router.Use(authMiddleware(server.TokenMaker))
authRoutes.POST("/review/location", server.createReview)
authRoutes.POST("/review/location/images", server.uploadReviewImages)
authRoutes.PATCH("/user", server.updateUser)
authRoutes.GET("/user/review/location/:location_id", server.getUserReviewByLocation)
authRoutes.GET("/user/profile", server.getUserStats)
authRoutes.PATCH("/user/avatar", server.updateUserAvatar)
authRoutes.DELETE("/user/avatar", server.removeAvatar)
authRoutes.POST("/news-events", server.createNews)
server.Router = router
}
func (server *Server) Start(address string) error {
return server.Router.Run(address)
}