added option add Access-Control-Allow-Origin header

This commit is contained in:
michael1011 2018-03-29 13:44:08 +02:00
parent 7343edf756
commit d473e7819e
No known key found for this signature in database
GPG Key ID: 84D249BA71685D46
2 changed files with 20 additions and 7 deletions

View File

@ -16,7 +16,8 @@ const (
defaultLogFile = "lightningTip.log" defaultLogFile = "lightningTip.log"
defaultLogLevel = "debug" defaultLogLevel = "debug"
defaultRESTHost = "localhost:8081" defaultRESTHost = "localhost:8081"
defaultAccessDomain = ""
defaultTipExpiry = 3600 defaultTipExpiry = 3600
@ -31,7 +32,8 @@ type config struct {
LogFile string `long:"logfile" Description:"Log file location"` LogFile string `long:"logfile" Description:"Log file location"`
LogLevel string `long:"loglevel" Description:"Log level: debug, info, warning, error"` LogLevel string `long:"loglevel" Description:"Log level: debug, info, warning, error"`
RESTHost string `long:"resthost" Description:"Host for the rest interface of LightningTip"` RESTHost string `long:"resthost" Description:"Host for the rest interface of LightningTip"`
AccessDomain string `long:"accessdomain" Description:"The domain you are using LightningTip from"`
TipExpiry int64 `long:"tipexpiry" Description:"Invoice expiry time in seconds"` TipExpiry int64 `long:"tipexpiry" Description:"Invoice expiry time in seconds"`
@ -51,7 +53,8 @@ func initConfig() {
LogFile: defaultLogFile, LogFile: defaultLogFile,
LogLevel: defaultLogLevel, LogLevel: defaultLogLevel,
RESTHost: defaultRESTHost, RESTHost: defaultRESTHost,
AccessDomain: defaultAccessDomain,
TipExpiry: defaultTipExpiry, TipExpiry: defaultTipExpiry,

View File

@ -68,12 +68,12 @@ func main() {
eventSrv = eventsource.NewServer() eventSrv = eventsource.NewServer()
http.HandleFunc("/", notFoundHandler) http.Handle("/", handleHeaders(notFoundHandler))
http.HandleFunc("/getinvoice", getInvoiceHandler) http.Handle("/getinvoice", handleHeaders(getInvoiceHandler))
http.HandleFunc("/eventsource", eventSrv.Handler(eventChannel)) http.Handle("/eventsource", handleHeaders(eventSrv.Handler(eventChannel)))
// Alternative for browsers which don't support EventSource (Internet Explorer and Edge) // Alternative for browsers which don't support EventSource (Internet Explorer and Edge)
http.HandleFunc("/invoicesettled", invoiceSettledHandler) http.Handle("/invoicesettled", handleHeaders(invoiceSettledHandler))
log.Debug("Starting ticker to clear expired invoices") log.Debug("Starting ticker to clear expired invoices")
@ -255,6 +255,16 @@ func notFoundHandler(writer http.ResponseWriter, request *http.Request) {
writeError(writer, "Not found") writeError(writer, "Not found")
} }
func handleHeaders(handler func(w http.ResponseWriter, r *http.Request)) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if cfg.AccessDomain != "" {
writer.Header().Add("Access-Control-Allow-Origin", cfg.AccessDomain)
}
handler(writer, request)
})
}
func writeError(writer http.ResponseWriter, message string) { func writeError(writer http.ResponseWriter, message string) {
writer.WriteHeader(http.StatusBadRequest) writer.WriteHeader(http.StatusBadRequest)