RoadSign/pkg/cmd/server/main.go

85 lines
2.5 KiB
Go
Raw Normal View History

2023-11-16 15:06:59 +00:00
package main
import (
2023-12-10 03:30:31 +00:00
"os"
"os/signal"
"strings"
"syscall"
2023-11-16 15:06:59 +00:00
roadsign "code.smartsheep.studio/goatworks/roadsign/pkg"
"code.smartsheep.studio/goatworks/roadsign/pkg/hypertext"
2023-12-13 11:52:56 +00:00
"code.smartsheep.studio/goatworks/roadsign/pkg/sideload"
2023-11-17 17:42:04 +00:00
"code.smartsheep.studio/goatworks/roadsign/pkg/sign"
"github.com/google/uuid"
2023-11-16 15:06:59 +00:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
}
func main() {
// Configure settings
viper.AddConfigPath(".")
viper.AddConfigPath("..")
viper.SetConfigName("settings")
viper.SetConfigType("yaml")
// Load settings
if err := viper.ReadInConfig(); err != nil {
log.Panic().Err(err).Msg("An error occurred when loading settings.")
}
// Present settings
if len(viper.GetString("security.credential")) <= 0 {
credential := strings.ReplaceAll(uuid.NewString(), "-", "")
viper.Set("security.credential", credential)
_ = viper.WriteConfig()
log.Warn().Msg("There isn't any api credential configured in settings.yml, auto generated a credential for api accessing.")
log.Warn().Msgf("RoadSign auto generated api credential is %s", credential)
}
2023-11-17 17:42:04 +00:00
// Load & init sign
if err := sign.ReadInConfig(viper.GetString("paths.configs")); err != nil {
2023-11-16 15:06:59 +00:00
log.Panic().Err(err).Msg("An error occurred when loading configurations.")
} else {
2023-12-10 03:30:31 +00:00
log.Info().Int("count", len(sign.App.Sites)).Msg("All configuration has been loaded.")
2023-11-16 15:06:59 +00:00
}
2023-12-13 05:16:00 +00:00
// Preheat processes
log.Info().Msg("Preheating processes...")
sign.App.PreheatProcesses(func(total int, success int) {
log.Info().Int("requested", total).Int("succeed", success).Msgf("Preheat processes completed!")
})
2023-11-16 15:06:59 +00:00
// Init hypertext server
2023-11-17 16:23:40 +00:00
hypertext.RunServer(
hypertext.InitServer(),
viper.GetStringSlice("hypertext.ports"),
viper.GetStringSlice("hypertext.secured_ports"),
viper.GetString("hypertext.certificate.pem"),
viper.GetString("hypertext.certificate.key"),
)
2023-12-10 03:30:31 +00:00
2023-12-13 11:52:56 +00:00
// Init sideload server
2023-11-17 16:23:40 +00:00
hypertext.RunServer(
2023-12-13 11:52:56 +00:00
sideload.InitSideload(),
viper.GetStringSlice("hypertext.sideload_ports"),
viper.GetStringSlice("hypertext.sideload_secured_ports"),
viper.GetString("hypertext.certificate.sideload_pem"),
viper.GetString("hypertext.certificate.sideload_key"),
2023-11-17 16:23:40 +00:00
)
2023-11-16 15:06:59 +00:00
log.Info().Msgf("RoadSign v%s is started...", roadsign.AppVersion)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Info().Msgf("RoadSign v%s is quitting...", roadsign.AppVersion)
}