cmd

package
Version: v0.0.0-...-31cff5b Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// Root is the root of the commands.
	Root = &cobra.Command{
		Use:           "go-vanity",
		SilenceErrors: true,
		SilenceUsage:  true,
		Short:         "go-vanity handles go vanity redirect requests",
		Long:          `go-vanity handles go vanity redirect requests and is available for deployment via docker. More documentation at https://go.krishnaiyer.dev/go-vanity-docker`,
		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
			err := manager.Unmarshal(config)
			if err != nil {
				panic(err)
			}
			return nil
		},
		Run: func(cmd *cobra.Command, args []string) {
			baseCtx := context.Background()
			ctx, cancel := context.WithCancel(baseCtx)
			defer cancel()

			h, err := handler.Init(ctx, config.Redirects)
			if err != nil {
				log.Fatal(err.Error())
			}
			var address string
			if address = addressRegex.FindString(config.HTTPAddress); address == "" {
				log.Println(fmt.Sprintf("Invalid or empty server address %s using 0.0.0.0:8080", config.HTTPAddress))
				address = "0.0.0.0:8080"
			}

			r := mux.NewRouter()
			r.HandleFunc("/", h.HandleIndex)
			addPath(r, h.HandleImport, config.NoOfSubPaths)
			r.Methods("GET")
			s := &http.Server{
				Handler:      r,
				Addr:         address,
				WriteTimeout: 5 * time.Second,
				ReadTimeout:  5 * time.Second,
				IdleTimeout:  5 * time.Second,
			}

			log.Println(fmt.Sprintf("Serving HTTP requests on %s ", address))
			select {
			case <-ctx.Done():
				s.Close()
			default:
				log.Fatal(s.ListenAndServe().Error())
			}
		},
	}
)

Functions

func Execute

func Execute()

Execute ...

Types

type Config

type Config struct {
	Redirects    string `name:"redirects" short:"r" description:"remote URL or local path to vanity redirects as yml"`
	HTTPAddress  string `name:"http-address" short:"a" description:"host:port for the http server"`
	Debug        bool   `name:"debug" short:"d" description:"print detailed logs for errors"`
	NoOfSubPaths int    `name:"no-of-subpaths" short:"n" description:"number of subpaths"`
}

Config represents the configuration

Source Files

Jump to

Keyboard shortcuts

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