-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (96 loc) · 2.33 KB
/
main.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
108
109
110
111
112
package main
import (
"fmt"
"math/rand"
"net"
"net/http"
"os"
"strconv"
"github.com/JeroenoBoy/Shorter/internal/authentication"
"github.com/JeroenoBoy/Shorter/internal/datastore"
"github.com/JeroenoBoy/Shorter/internal/web"
)
const ascii = `
___ _ _
/ __| |_ ___ _ _| |_ ___ _ _
\__ \ ' \/ _ \ '_| _/ -_) '_|
|___/_||_\___/_| \__\___|_|
`
func main() {
var address = ":3000"
println(ascii)
println("Your self-hosted link shortner")
println("")
dataStore, err := datastore.NewPostgresStore(getPostgresConfig())
if err != nil {
panic(err)
}
jwtAuth := authentication.NewJWTAuthenticator(dataStore, getJWTSecret())
handler := web.NewWebserver(jwtAuth, dataStore)
l, err := net.Listen("tcp", address)
if err != nil {
panic(err)
}
fmt.Printf("Listening on port %v 🚀\n", address)
println("Shorter is ready to roll!")
println("")
http.Serve(l, handler)
}
func getPostgresConfig() datastore.PostgresOptions {
return datastore.PostgresOptions{
Host: envVar("SHORTER_POSTGRES_ADDRESS", "127.0.0.1"),
Port: envInt("SHORTER_POSTGRES_PORT", 5432),
Database: envVar("SHORTER_POSTGRES_DATABASE", ""),
UserName: envVar("SHORTER_POSTGRES_USER", ""),
Password: envVar("SHORTER_POSTGRES_PASSWORD", ""),
SSLMode: envVar("SHORTER_POSTGRES_SSLMODE", "require"),
}
}
func envBool(key string, def bool) bool {
env, ok := os.LookupEnv(key)
if ok {
if env == "" {
return false
}
b, err := strconv.ParseBool(env)
if err != nil {
panic(err)
}
return b
} else {
return def
}
}
func envInt(key string, def int) int {
env, ok := os.LookupEnv(key)
if ok {
i, err := strconv.Atoi(env)
if err != nil {
panic(err)
}
return i
} else {
return def
}
}
func envVar(key string, def string) string {
env, ok := os.LookupEnv(key)
if ok {
return env
} else {
return def
}
}
func getJWTSecret() []byte {
secret, ok := os.LookupEnv("SHORTER_JWT_SECRET")
if !ok || len(secret) == 0 {
fmt.Println("No $SHORTER_JWT_SECRET env variable provided! Generating temporary secret")
available := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-=_+[]{}\\|;':\",.<>/?`~")
chars := make([]rune, 120+rand.Intn(16))
for i := range chars {
chars[i] = available[rand.Intn(len(available)-1)]
}
return []byte(string(chars))
}
return []byte(secret)
}