cmd

package
v0.0.0-...-c6880a1 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2017 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:     "leproxy [PROXY URL]",
	Short:   "Leproxy to easily enable HTTPS",
	Long:    "Leproxy is a single host reverse proxy with Let's Encrypt integration",
	Example: "leproxy -D example.com -D www.example.com http://example.io",
	RunE: func(cmd *cobra.Command, args []string) error {

		if len(args) != 1 {
			return errors.New("must supply one argument, the proxy URL")
		}

		if len(domains) == 0 {
			return errors.New("must supply atleast one domain")
		}

		healthServer := &http.Server{
			Addr: ":8500",
			Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				fmt.Fprint(w, "OK")
			}),
		}
		go func() {
			log.Println("starting health server")
			if err := healthServer.ListenAndServe(); err != nil {
				log.Println("health server error:", err)
			}
		}()

		redirectServer := &http.Server{
			Addr: ":80",
			Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				log.Println("redirecting request", r.Host, r.URL.Path)
				target := "https://" + r.Host + r.URL.Path
				if len(r.URL.RawQuery) > 0 {
					target += "?" + r.URL.RawQuery
				}
				http.Redirect(w, r, target, http.StatusMovedPermanently)
			}),
		}
		go func() {
			log.Println("starting redirect server")
			if err := redirectServer.ListenAndServe(); err != nil {
				log.Println("redirect server error:", err)
			}
		}()

		proxyTarget, err := url.Parse(args[0])
		if err != nil {
			return err
		}

		proxyServer := server.New(server.Options{
			Domains:     domains,
			Port:        ":443",
			CertCache:   "/var/certs",
			ProxyTarget: proxyTarget,
		})

		log.Println("starting up...")
		if err := proxyServer.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
			return err
		}

		return nil
	},
}

RootCmd to be called from main

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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