github.com/cjdelisle/matterfoss@v5.11.1+incompatible/utils/utils.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"net"
     8  	"net/http"
     9  	"net/url"
    10  	"strings"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  func StringInSlice(a string, slice []string) bool {
    16  	for _, b := range slice {
    17  		if b == a {
    18  			return true
    19  		}
    20  	}
    21  	return false
    22  }
    23  
    24  func StringArrayIntersection(arr1, arr2 []string) []string {
    25  	arrMap := map[string]bool{}
    26  	result := []string{}
    27  
    28  	for _, value := range arr1 {
    29  		arrMap[value] = true
    30  	}
    31  
    32  	for _, value := range arr2 {
    33  		if arrMap[value] {
    34  			result = append(result, value)
    35  		}
    36  	}
    37  
    38  	return result
    39  }
    40  
    41  func RemoveDuplicatesFromStringArray(arr []string) []string {
    42  	result := make([]string, 0, len(arr))
    43  	seen := make(map[string]bool)
    44  
    45  	for _, item := range arr {
    46  		if !seen[item] {
    47  			result = append(result, item)
    48  			seen[item] = true
    49  		}
    50  	}
    51  
    52  	return result
    53  }
    54  
    55  func StringSliceDiff(a, b []string) []string {
    56  	m := make(map[string]bool)
    57  	result := []string{}
    58  
    59  	for _, item := range b {
    60  		m[item] = true
    61  	}
    62  
    63  	for _, item := range a {
    64  		if !m[item] {
    65  			result = append(result, item)
    66  		}
    67  	}
    68  	return result
    69  }
    70  
    71  func GetIpAddress(r *http.Request) string {
    72  	address := ""
    73  
    74  	header := r.Header.Get(model.HEADER_FORWARDED)
    75  	if len(header) > 0 {
    76  		addresses := strings.Fields(header)
    77  		if len(addresses) > 0 {
    78  			address = strings.TrimRight(addresses[0], ",")
    79  		}
    80  	}
    81  
    82  	if len(address) == 0 {
    83  		address = r.Header.Get(model.HEADER_REAL_IP)
    84  	}
    85  
    86  	if len(address) == 0 {
    87  		address, _, _ = net.SplitHostPort(r.RemoteAddr)
    88  	}
    89  
    90  	return address
    91  }
    92  
    93  func GetHostnameFromSiteURL(siteURL string) string {
    94  	u, err := url.Parse(siteURL)
    95  	if err != nil {
    96  		return ""
    97  	}
    98  
    99  	return u.Hostname()
   100  }