-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil.go
107 lines (92 loc) · 2.29 KB
/
util.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"golang.org/x/crypto/bcrypt"
)
func get_secure_random_string(length int) (string, error) {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func hash_string(input string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(input), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(hash), nil
}
func check_hash(input string, hashed string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashed), []byte(input))
return err == nil
}
// func generate_log(input string) {
// datetime :=
// }
func checkFileExists(filepath string) bool {
_, err := os.Stat(filepath)
return !os.IsNotExist(err)
}
func get_env(key string) string {
// if constant[key] == "" {
// constant[key] = os.Getenv(key)
// }
// return constant[key]
return os.Getenv(key)
}
func parameter_to_int(input string, default_int int) int {
if input == "" {
return default_int
}
value, err := strconv.Atoi(input)
if err != nil {
return default_int
}
return value
}
func update_setting(setting_key string, setting_value string) {
_, err := db_execute("UPDATE settings SET value = $1 WHERE key = $2", setting_value, setting_key)
if err != nil {
fmt.Println("Settings is not updated: ", err)
}
}
func make_folder_if_not_exists(folder string) {
if !checkFileExists(folder) {
err := os.MkdirAll(folder, 0750)
if err != nil {
log.Fatal("Fatal Error on make folder:", err)
}
}
}
func generate_screenshot_url(request *http.Request, screenshot_id string) string {
if get_env("SCREENSHOTS_REQUIRE_AUTH") == "true" {
return ""
// return get_host(request) + "/screenshot/" + screenshot_id + "?auth=" +
}
return get_host(request) + "/screenshots/" + screenshot_id + ".png"
}
func get_client_ip(request *http.Request) string {
clientIP := request.Header.Get("X-Forwarded-For")
if clientIP == "" {
return request.RemoteAddr
}
ips := strings.Split(clientIP, ",")
if len(ips) > 0 {
clientIP = ips[0]
}
return clientIP
}
// func remember(variable *string, reload bool, function func() string) string {
// if reload || *variable == "" {
// *variable = function()
// }
// return *variable
// }