55 lines
1022 B
Go
55 lines
1022 B
Go
package restserver
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type RestServer struct {
|
|
config *Config
|
|
logger *logrus.Logger
|
|
router *mux.Router
|
|
}
|
|
|
|
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
|
|
}
|
|
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() {
|
|
r.router.HandleFunc("/hello", r.HandleHello())
|
|
}
|
|
|
|
func (r *RestServer) HandleHello() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
io.WriteString(w, "TEsts Hello")
|
|
}
|
|
}
|