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"
|
2024-01-17 06:34:08 +00:00
|
|
|
"code.smartsheep.studio/goatworks/roadsign/pkg/navi"
|
2023-12-13 11:52:56 +00:00
|
|
|
"code.smartsheep.studio/goatworks/roadsign/pkg/sideload"
|
2023-11-24 16:34:51 +00:00
|
|
|
"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")
|
2024-01-24 16:09:39 +00:00
|
|
|
viper.SetConfigType("toml")
|
2023-11-16 15:06:59 +00:00
|
|
|
|
|
|
|
// Load settings
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
log.Panic().Err(err).Msg("An error occurred when loading settings.")
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:34:51 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-01-25 16:11:44 +00:00
|
|
|
// Load & init navigator
|
2024-01-17 06:34:08 +00:00
|
|
|
if err := navi.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 {
|
2024-01-24 16:09:39 +00:00
|
|
|
log.Info().Int("count", len(navi.R.Regions)).Msg("All configuration has been loaded.")
|
2023-11-16 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
// Init warden
|
|
|
|
navi.InitializeWarden(navi.R.Regions)
|
2023-12-13 05:16:00 +00:00
|
|
|
|
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)
|
|
|
|
}
|