about summary refs log tree commit diff
path: root/nix/templates/goapp/frontend/src/util.go
blob: 89d28ba80a345ad397d373a60ecb8b978bdf8480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main

import (
	"fmt"
	"net/url"
	"path"
	"strings"
)

func isStringInSlice(s string, slice []string) bool {
	for _, x := range slice {
		if s == x {
			return true
		}
	}

	return false
}

func filterText(input string, filters []string) (output string) {
	if len(filters) == 0 {
		return input
	}

	for _, filter := range filters {
		input = strings.Replace(input, filter, strings.Repeat("*", len(filter)), -1)
	}

	return input
}

func filterSliceOfText(input []string, filters []string) (output []string) {
	for _, item := range input {
		output = append(output, filterText(item, filters))
	}

	return output
}

func getURLs(rootURL string) (publicURL *url.URL, redirectURL *url.URL, err error) {
	if publicURL, err = url.Parse(rootURL); err != nil {
		return nil, nil, err
	}

	if publicURL.Scheme != "http" && publicURL.Scheme != "https" {
		return nil, nil, fmt.Errorf("scheme must be http or https but it is '%s'", publicURL.Scheme)
	}

	if !strings.HasSuffix(publicURL.Path, "/") {
		publicURL.Path += "/"
	}

	redirectURL = &url.URL{}
	*redirectURL = *publicURL
	redirectURL.Path = path.Join(redirectURL.Path, "/oauth2/callback")

	return publicURL, redirectURL, nil
}