add https support
This commit is contained in:
3
main.go
3
main.go
@@ -19,7 +19,8 @@ import (
|
|||||||
var core embed.FS
|
var core embed.FS
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
config.Port = flag.Int("p", 3000, "Set the server port")
|
config.Port = flag.Int("p", 3000, "Set the server port for http")
|
||||||
|
config.HTTPSPort = flag.Int("ps", 3000, "Set the server port for https")
|
||||||
config.Color = flag.Bool("no-color", false, "Disable color output")
|
config.Color = flag.Bool("no-color", false, "Disable color output")
|
||||||
config.Core = core
|
config.Core = core
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import "embed"
|
|||||||
|
|
||||||
var Core embed.FS
|
var Core embed.FS
|
||||||
var Port *int
|
var Port *int
|
||||||
|
var HTTPSPort *int
|
||||||
var Color *bool
|
var Color *bool
|
||||||
|
|
||||||
type MyConfig struct {
|
type MyConfig struct {
|
||||||
|
|||||||
@@ -399,7 +399,7 @@ func Start(dir string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
lp, ok := routes[path]
|
lp, ok := routes[path]
|
||||||
|
|
||||||
@@ -457,6 +457,20 @@ func Start(dir string) error {
|
|||||||
w.Write(data)
|
w.Write(data)
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port)
|
addrHTTPS := fmt.Sprintf(":%d", *config.HTTPSPort)
|
||||||
return http.ListenAndServe(fmt.Sprintf(":%d", *config.Port), nil)
|
addrHTTP := fmt.Sprintf(":%d", *config.Port)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
target := "https://" + r.Host + r.RequestURI
|
||||||
|
http.Redirect(w, r, target, http.StatusMovedPermanently)
|
||||||
|
})
|
||||||
|
fmt.Printf("Redirecting HTTP to HTTPS on port %d\n", *config.Port)
|
||||||
|
if err := http.ListenAndServe(addrHTTP, nil); err != nil {
|
||||||
|
fmt.Printf("HTTP redirect server error: %v\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
fmt.Printf("HTTPS server is running on https://localhost:%d\n", *config.HTTPSPort)
|
||||||
|
return http.ListenAndServeTLS(addrHTTPS, "cert.pem", "key.pem", handler)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user