summary refs log blame commit diff stats
path: root/notification/notify_darwin.go
blob: bddaf9ea3aa77645314f1c6a758908d545f7c575 (plain) (tree)
























                                                                                                             
// +build darwin

package notification

import (
	"fmt"
	"os/exec"
	"strconv"
)

// Notify sends a desktop notification to the user using osascript. It
// handles information in the form of Notif struct. It returns an
// error (if exists).
func (n Notif) Notify() error {
	// This script cuts out parts of notification, this bug was
	// confirmed on macOS Catalina 10.15.3, fix not yet known.
	err := exec.Command("osascript", "-e",
		`display notification `+strconv.Quote(n.Message)+` with title `+strconv.Quote(n.Title)).Run()
	if err != nil {
		err = fmt.Errorf("%s\n%s",
			"notify_darwin.go: failed to sent notification with osascript",
			err.Error())
	}
	return err
}
s="nx">uip := base[0] if _, ok := r.Header["X-Forwarded-For"]; ok { proxied := r.Header["X-Forwarded-For"] base = strings.Split(proxied[len(proxied)-1], ":") uip = base[0] } xRealIP := http.CanonicalHeaderKey("X-Real-IP") if _, ok := r.Header[xRealIP]; ok { proxied := r.Header[xRealIP] base = strings.Split(proxied[len(proxied)-1], ":") uip = base[0] } return context.WithValue(ctx, ctxKey, uip) } // Retrieves a request's IP address from the request's context func getIPFromCtx(ctx context.Context) net.IP { uip, ok := ctx.Value(ctxKey).(string) if !ok { log.Printf("Couldn't retrieve IP from request\n") } return net.ParseIP(uip) } // Shim function to modify/pass context value to a handler func ipMiddleware(hop http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := newCtxUserIP(r.Context(), r) hop.ServeHTTP(w, r.WithContext(ctx)) }) } func log200(r *http.Request) { useragent := r.Header["User-Agent"] uip := getIPFromCtx(r.Context()) log.Printf("*** %v :: 200 :: %v %v :: %v\n", uip, r.Method, r.URL, useragent) } func errHTTP(w http.ResponseWriter, r *http.Request, err error, code int) { useragent := r.Header["User-Agent"] uip := getIPFromCtx(r.Context()) log.Printf("*** %v :: %v :: %v %v :: %v :: %v\n", uip, code, r.Method, r.URL, useragent, err.Error()) http.Error(w, fmt.Sprintf("Error %v: %v", code, err.Error()), code) }