matrix-handler/pkg/server/server.go

45 lines
1.2 KiB
Go
Raw Normal View History

2021-02-04 07:25:16 +00:00
package server
import (
"fmt"
"net/http"
"git.linuxrocker.com/mattburchett/matrix-handler/pkg/config"
"git.linuxrocker.com/mattburchett/matrix-handler/pkg/generic"
"git.linuxrocker.com/mattburchett/matrix-handler/pkg/router"
"github.com/rs/cors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// Run ...
func Run(info *router.BuildInfo) error {
2021-02-04 07:41:12 +00:00
conf, err := config.GetConfig("/config/config.json")
2021-02-04 07:25:16 +00:00
if err != nil {
log.Fatal().Err(err)
}
level, err := zerolog.ParseLevel(conf.LogLevel)
if err != nil {
level = zerolog.ErrorLevel
log.Warn().Err(err).Msgf("unable to parse log level, logging level is set to %s", level.String())
}
zerolog.SetGlobalLevel(level)
log.Logger = log.With().Str("app", conf.Name).Logger()
router := router.NewRouter(info)
router.HandleWithMetrics("/generic/{matrixRoom}/{matrixUser}/{matrixPassword}", generic.Handle(conf)).Methods(http.MethodPost)
2021-02-04 08:29:28 +00:00
router.HandleWithMetrics("/slack/{matrixRoom}/{matrixUser}/{matrixPassword}", generic.Handle(conf)).Methods(http.MethodPost)
2021-02-04 07:25:16 +00:00
srv := http.Server{
Addr: fmt.Sprintf(":%d", conf.Port),
Handler: cors.Default().Handler(router),
}
log.Info().Msgf("Server running on %v", srv.Addr)
return srv.ListenAndServe()
}