111 lines
3 KiB
Go
111 lines
3 KiB
Go
package rest
|
|
|
|
import (
|
|
"context"
|
|
"expvar"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/urec56/pathparams"
|
|
|
|
"git.urec56.ru/urec/chat_back_go/config"
|
|
"git.urec56.ru/urec/chat_back_go/internal/logger"
|
|
"git.urec56.ru/urec/chat_back_go/internal/service"
|
|
"git.urec56.ru/urec/chat_back_go/internal/transport/rest/handler"
|
|
"git.urec56.ru/urec/chat_back_go/internal/transport/rest/middleware"
|
|
)
|
|
|
|
type Server struct {
|
|
httpServer *http.Server
|
|
m *middleware.Middleware
|
|
h *handler.Handler
|
|
}
|
|
|
|
func NewServer(serv service.Serv, l *logger.Logger, cfg config.Config) *Server {
|
|
return &Server{m: middleware.NewMiddleware(serv, l, cfg.Srv), h: handler.NewHandler(serv, l)}
|
|
}
|
|
|
|
func (s *Server) Run(port int) error {
|
|
r := s.getRouter()
|
|
|
|
s.httpServer = &http.Server{
|
|
Addr: fmt.Sprintf(":%d", port),
|
|
Handler: r,
|
|
}
|
|
|
|
return s.httpServer.ListenAndServe()
|
|
}
|
|
|
|
func (s *Server) Shutdown(ctx context.Context) error {
|
|
return s.httpServer.Shutdown(ctx)
|
|
}
|
|
|
|
func (s *Server) getRouter() http.Handler {
|
|
pathparams.SetConfig("", pathparams.DefaultValidationErrorStatusCode)
|
|
ro := pathparams.NewRouter()
|
|
r := ro.PathPrefix("/api")
|
|
|
|
r.Use(s.m.RequestID)
|
|
r.Use(s.m.Log)
|
|
|
|
s.initUsersRouter(r)
|
|
s.initChatRouter(r)
|
|
s.initImagesRouter(r)
|
|
s.initDebug(r)
|
|
|
|
return ro
|
|
}
|
|
|
|
func (s *Server) initUsersRouter(r *pathparams.Router) {
|
|
ur := r.PathPrefix("/users")
|
|
aur := r.PathPrefix("/users")
|
|
|
|
aur.Use(s.m.Auth)
|
|
|
|
ur.Get("", s.h.GetUsers)
|
|
ur.Post("/check_existing_user", s.h.CheckExisting)
|
|
ur.Post("/check_existing_password", s.h.CheckExistingPassword)
|
|
ur.Post("/login", s.h.Login)
|
|
ur.Post("/register", s.h.Register)
|
|
|
|
aur.Post("/resend_email_verification", s.h.ResendEmailVerification)
|
|
aur.Post("/email_verification", s.h.EmailVerification)
|
|
aur.Get("/me", s.h.Get)
|
|
aur.Get("/avatars", s.h.GetAvatarsHistory)
|
|
aur.Post("/send_confirmation_code", s.h.SendConfirmationCode)
|
|
aur.Post("/change_data", s.h.ChangeUserData)
|
|
}
|
|
|
|
func (s *Server) initChatRouter(r *pathparams.Router) {
|
|
chr := r.PathPrefix("/chat")
|
|
|
|
chr.Use(s.m.VerificatedAuth)
|
|
|
|
chr.Get("", s.h.GetChats)
|
|
chr.Post("/create_chat", s.h.Create)
|
|
chr.Post("/change_data", s.h.ChangeChatData)
|
|
chr.Get("/get_last_message/{chat_id}", s.h.GetLastMessage)
|
|
chr.Get("/get_some_messages/{chat_id}", s.h.GetSomeMessages)
|
|
chr.Get("/message/{chat_id}", s.h.GetMessageByID)
|
|
chr.Get("/create_invitation_link", s.h.CreateInvitationLink)
|
|
chr.Get("/invite_to_chat/{invitation_token}", s.h.InviteToChat)
|
|
chr.Delete("/delete_chat/{chat_id}", s.h.DeleteChat)
|
|
chr.Delete("/delete_user_from_chat/{chat_id}", s.h.DeleteUserFromChat)
|
|
chr.Post("/pin_chat", s.h.PinChat)
|
|
chr.Delete("/unpin_chat", s.h.UnpinnChat)
|
|
chr.Get("/get_pinned_chats", s.h.GetPinnedChats)
|
|
chr.Get("/pinned_messages/{chat_id}", s.h.GetPinnedMessages)
|
|
}
|
|
|
|
func (s *Server) initImagesRouter(r *pathparams.Router) {
|
|
ir := r.PathPrefix("/images")
|
|
|
|
ir.Use(s.m.VerificatedAuth)
|
|
|
|
ir.Post("/upload_avatar", s.h.UploadAvatar)
|
|
ir.Post("/upload_image", s.h.UploadImage)
|
|
}
|
|
|
|
func (s *Server) initDebug(r *pathparams.Router) {
|
|
r.Get("/debug/vars", expvar.Handler().(http.HandlerFunc))
|
|
}
|