Files
glauth/pkg/frontend/webapi.go
T
Chris F Ravenscroft e7195ed983 Dev to Master 2.1.0 RC1 (#243)
Co-authored-by: Jörn Friedrich Dreyer <jfd@butonic.de>
Co-authored-by: Zhou Wenzong <wenzong@users.noreply.github.com>
Co-authored-by: Jairo Llopis <Yajo@users.noreply.github.com>
Co-authored-by: Benedikt Kulmann <benedikt@kulmann.biz>
Co-authored-by: Nicolai Willems <172633+nwillems@users.noreply.github.com>
Co-authored-by: Thibault Soubiran <soubi.thibault@gmail.com>
Co-authored-by: Clement JACOB <clems71@gmail.com>
Co-authored-by: Lutz Horn <code@lhorn.de>
Co-authored-by: Márk Sági-Kazár <sagikazarmark@users.noreply.github.com>
Co-authored-by: Peter Heise <pheise@koprolalie.com>
Co-authored-by: Ben Yanke <ben@benyanke.com>
Co-authored-by: fanlix <fanlix@gmail.com>
Co-authored-by: Hank Donnay <hdonnay@gmail.com>
2021-11-25 17:56:59 -08:00

48 lines
1.1 KiB
Go

package frontend
import (
"net/http"
"github.com/go-logr/logr"
"github.com/glauth/glauth/pkg/assets"
)
// RunAPI provides a basic REST API
func RunAPI(opts ...Option) {
options := newOptions(opts...)
log := options.Logger
cfg := options.Config
register(http.DefaultServeMux, log)
if cfg.TLS {
log.V(3).Info("Starting HTTPS server", "address", cfg.Listen)
err := http.ListenAndServeTLS(cfg.Listen, cfg.Cert, cfg.Key, nil)
if err != nil {
log.Error(err, "Error starting HTTPS server")
return
}
} else {
log.V(3).Info("Starting HTTP server", "address", cfg.Listen)
err := http.ListenAndServe(cfg.Listen, nil)
if err != nil {
log.Error(err, "Error starting HTTP server")
return
}
}
}
func register(mux *http.ServeMux, log logr.Logger) {
fs := http.FileServer(http.FS(assets.Content))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.V(6).Info("Web", "path", r.URL.Path)
if r.URL.Path != "/" {
log.V(6).Info("Web 404")
http.NotFound(w, r)
return
}
fs.ServeHTTP(w, r)
})
mux.Handle("/assets/", http.StripPrefix("/assets/", fs))
}