2022-12-07 21:29:01 +03:00
|
|
|
package restserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
2022-12-12 20:34:00 +03:00
|
|
|
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd"
|
2022-12-07 21:29:01 +03:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RestServer struct {
|
|
|
|
config *Config
|
|
|
|
logger *logrus.Logger
|
|
|
|
router *mux.Router
|
2022-12-12 20:34:00 +03:00
|
|
|
db *bd.Bd
|
2022-12-07 21:29:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(config *Config) *RestServer {
|
|
|
|
return &RestServer{
|
|
|
|
config: config,
|
|
|
|
logger: logrus.New(),
|
|
|
|
router: mux.NewRouter(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RestServer) Start() error {
|
|
|
|
if err := r.configureLogger(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-12 20:34:00 +03:00
|
|
|
if err := r.configurebd(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 21:29:01 +03:00
|
|
|
r.configureRouter()
|
|
|
|
|
|
|
|
r.logger.Info("Starting Server")
|
|
|
|
r.logger.Info("Listen server on ", r.config.BindPort, " Port")
|
|
|
|
return http.ListenAndServe(r.config.BindPort, r.router)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RestServer) configureLogger() error {
|
|
|
|
level, err := logrus.ParseLevel(r.config.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.logger.SetLevel(level)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RestServer) configureRouter() {
|
2022-12-31 01:43:30 +03:00
|
|
|
r.router.HandleFunc("/api/films", r.HandleHello())
|
2022-12-07 21:29:01 +03:00
|
|
|
}
|
|
|
|
|
2022-12-12 20:34:00 +03:00
|
|
|
func (r *RestServer) configurebd() error {
|
|
|
|
dataBase := bd.New(r.config.DB)
|
|
|
|
if err := dataBase.Open(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r.db = dataBase
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-07 21:29:01 +03:00
|
|
|
func (r *RestServer) HandleHello() http.HandlerFunc {
|
|
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
io.WriteString(w, "TEsts Hello")
|
|
|
|
}
|
|
|
|
}
|