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"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
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{
|
|
|
|
config: config,
|
|
|
|
store: store,
|
|
|
|
tokenMaker: tokenMaker,
|
|
|
|
}
|
|
|
|
|
|
|
|
server.getRoutes()
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) getRoutes() {
|
|
|
|
router := gin.Default()
|
|
|
|
|
2023-09-12 17:07:57 +07:00
|
|
|
router.POST("/user/signup", server.createUser)
|
2023-09-08 22:25:38 +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
|
|
|
}
|