mirror of
https://github.com/tnb-labs/panel.git
synced 2026-09-01 14:55:12 +08:00
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"net/http"
|
|
"path/filepath"
|
|
|
|
"github.com/bddjr/hlfhr"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
"github.com/acepanel/panel/v3/internal/app"
|
|
"github.com/acepanel/panel/v3/internal/http/middleware"
|
|
"github.com/acepanel/panel/v3/internal/route"
|
|
"github.com/acepanel/panel/v3/pkg/config"
|
|
"github.com/acepanel/panel/v3/pkg/tlscert"
|
|
)
|
|
|
|
func NewRouter(t *gotext.Locale, middlewares *middleware.Middlewares, http *route.Http, ws *route.Ws) (*chi.Mux, error) {
|
|
r := chi.NewRouter()
|
|
|
|
// add middleware
|
|
r.Use(middlewares.Globals(t, r)...)
|
|
// add http route
|
|
http.Register(r)
|
|
// add ws route
|
|
ws.Register(r)
|
|
|
|
return r, nil
|
|
}
|
|
|
|
func NewTLSReloader(conf *config.Config) (*tlscert.Reloader, error) {
|
|
if !conf.HTTP.IsHTTPS() {
|
|
return nil, nil
|
|
}
|
|
|
|
certFile := filepath.Join(app.Root, "panel/storage/cert.pem")
|
|
keyFile := filepath.Join(app.Root, "panel/storage/cert.key")
|
|
reloader, err := tlscert.NewReloader(certFile, keyFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to load certificate: %w", err)
|
|
}
|
|
return reloader, nil
|
|
}
|
|
|
|
func NewHttp(conf *config.Config, mux *chi.Mux, reloader *tlscert.Reloader) (*hlfhr.Server, error) {
|
|
srv := hlfhr.New(&http.Server{
|
|
Addr: fmt.Sprintf(":%d", conf.HTTP.Port),
|
|
Handler: mux,
|
|
MaxHeaderBytes: 4 << 20,
|
|
})
|
|
srv.Listen80RedirectTo443 = true
|
|
|
|
if conf.HTTP.IsHTTPS() && reloader != nil {
|
|
srv.TLSConfig = &tls.Config{
|
|
MinVersion: tls.VersionTLS12,
|
|
GetCertificate: reloader.GetCertificate,
|
|
}
|
|
}
|
|
|
|
return srv, nil
|
|
}
|