go-repo/main.go

71 lines
1.5 KiB
Go
Raw Normal View History

2020-11-21 09:36:28 +00:00
package main
import (
"net/http"
2020-11-21 11:01:43 +00:00
"github.com/fsnotify/fsnotify"
2020-11-21 09:36:28 +00:00
"github.com/sirupsen/logrus"
2020-11-21 11:01:43 +00:00
"github.com/spf13/cobra"
2020-11-21 12:04:49 +00:00
"github.com/spf13/viper"
2020-11-21 09:36:28 +00:00
)
var (
2020-11-21 11:01:43 +00:00
modules = Modules{}
2020-11-21 09:36:28 +00:00
)
2020-11-21 12:21:22 +00:00
func loadConfig() error {
conf := &Config{}
if err := viper.Unmarshal(conf); err != nil {
return err
}
mu.Lock()
modules = conf.Modules
mu.Unlock()
go func() {
if err := modules.LoadReadme(); err != nil {
logrus.WithError(err).Error("failed to load all readme")
}
}()
return nil
}
2020-11-21 09:36:28 +00:00
func main() {
2020-11-21 11:01:43 +00:00
var address string
var level string
cmd := &cobra.Command{
2020-11-21 12:04:49 +00:00
Use: "go-repo [config]",
Short: "go-repo serve a minimal go module registry web site",
Example: "go-repo config.yml",
Args: cobra.ExactArgs(1),
2020-11-21 11:01:43 +00:00
Run: func(cmd *cobra.Command, args []string) {
if l, err := logrus.ParseLevel(level); err == nil {
logrus.SetLevel(l)
2020-11-21 09:36:28 +00:00
}
2020-11-21 12:04:49 +00:00
viper.SetConfigType("yaml")
viper.SetConfigFile(args[0])
if err := viper.ReadInConfig(); err != nil {
2020-11-21 11:01:43 +00:00
logrus.Fatal(err)
}
2020-11-21 12:21:22 +00:00
if err := loadConfig(); err != nil {
2020-11-21 11:01:43 +00:00
logrus.Fatal(err)
}
2020-11-21 12:04:49 +00:00
viper.WatchConfig()
viper.OnConfigChange(func(event fsnotify.Event) {
2020-11-21 12:21:22 +00:00
if err := loadConfig(); err != nil {
2020-11-21 12:04:49 +00:00
logrus.Fatal(err)
}
logrus.Info("modules reloaded")
})
2020-11-21 11:01:43 +00:00
http.HandleFunc("/", modulesHandler)
logrus.Infof("starting server at %s", address)
if err := http.ListenAndServe(address, nil); err != nil {
logrus.Fatal(err)
2020-11-21 09:36:28 +00:00
}
2020-11-21 11:01:43 +00:00
},
}
cmd.Flags().StringVarP(&address, "address", "a", ":8888", "The server address")
cmd.Flags().StringVar(&level, "logs-level", "info", "")
2020-11-21 11:09:47 +00:00
cmd.Execute()
2020-11-21 09:36:28 +00:00
}