51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
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"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Server struct {
|
|
Config util.Config
|
|
Store db.Store
|
|
TokenMaker token.Maker
|
|
Router *gin.Engine
|
|
}
|
|
|
|
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{
|
|
Config: config,
|
|
Store: store,
|
|
TokenMaker: tokenMaker,
|
|
}
|
|
|
|
server.getRoutes()
|
|
return server, nil
|
|
}
|
|
|
|
func (server *Server) getRoutes() {
|
|
router := gin.Default()
|
|
|
|
router.POST("/user/signup", server.createUser)
|
|
|
|
// LOCATION
|
|
router.POST("/locations", server.createLocation)
|
|
router.GET("/locations", server.getListLocations)
|
|
router.GET("/location/:location_id", server.getLocation)
|
|
|
|
server.Router = router
|
|
}
|
|
|
|
func (server *Server) Start(address string) error {
|
|
return server.Router.Run(address)
|
|
}
|