cmd

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2020 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "gosspks",
	Short: "Serving your Synology Packages.",
	Long:  `Serving your Synology Packages.`,
	Run: func(cmd *cobra.Command, args []string) {
		router := rtr.NewRouter()
		allowedOrigins := handlers.AllowedOrigins([]string{"*"})
		allowedMethods := handlers.AllowedMethods([]string{"GET", "HEAD", "OPTIONS"})
		allowedHeaders := handlers.AllowedHeaders([]string{"Accept", "Accept-Language", "Content-Language", "Origin", "X-Requested-With"})
		corsHandler := handlers.CORS(allowedOrigins, allowedMethods, allowedHeaders)

		if t := cfg.GetPackagesCacheRefreshRate(); t != 0 {
			ticker := time.NewTicker(t)
			go func() {
				for range ticker.C {
					c := cache.Cache
					for k, v := range c.Items() {
						if strings.Contains(k, "-request") {
							bits := strings.Split(k, "-")
							_, e, _ := c.GetWithExpiration(bits[0] + "-" + bits[1])
							if time.Now().Add(t).UnixNano() > e.UnixNano() {
								loggerCmd.Debugf("Packages cache %s-%s expires before next refresh in %s, refreshing", bits[0], bits[1], time.Until(e))
								v1.GetPackagesFromCacheOrFileSystem(v.Object.(*http.Request), bits[1], true)
							}
						}
					}
				}
			}()
		}

		if t := cfg.GetModelsCacheRefreshRate(); t != 0 {
			ticker := time.NewTicker(t)
			go func() {
				for range ticker.C {
					i, e, _ := cache.Cache.GetWithExpiration("synoModels")
					if i != nil && time.Now().Add(t).UnixNano() > e.UnixNano() {
						loggerCmd.Debugf("Models cache expires before next refresh in %s, refreshing", time.Until(e))
						v1.GetModelsFromCacheOrWeb(true)
					}
				}
			}()
		}

		loggerCmd.Info("Using config:")
		settings, _ := yaml.Marshal(viper.AllSettings())
		for _, setting := range strings.Split(string(settings), "\n") {
			loggerCmd.Info(setting)
		}
		loggerCmd.Info("Listening on: ", cfg.GetPort())
		loggerCmd.Fatal(http.ListenAndServe(":"+cfg.GetPort(), corsHandler(router)))
	},
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute()

Execute runs the main command that serves synology packages

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL