-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (77 loc) · 2.09 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
package main
import (
"context"
"embed"
"fmt"
"log"
"net/http"
"os"
"github.com/betterstack-community/go-image-upload/db"
"github.com/betterstack-community/go-image-upload/redisconn"
"github.com/joho/godotenv"
)
var redisConn *redisconn.RedisConn
var dbConn *db.DBConn
//go:embed templates/*
var templates embed.FS
type Config struct {
PostgresDB string
PostgresUser string
PostgresPassword string
PostgresHost string
PostgresURL string
GitHubClientID string
GitHubClientSecret string
GitHubRedirectURI string
RedisAddr string
ServiceName string
CollectorURL string
InsecureMode string
}
var conf Config
func init() {
godotenv.Load()
conf.PostgresDB = os.Getenv("POSTGRES_DB")
conf.PostgresUser = os.Getenv("POSTGRES_USER")
conf.PostgresPassword = os.Getenv("POSTGRES_PASSWORD")
conf.PostgresHost = os.Getenv("POSTGRES_HOST")
connStr := fmt.Sprintf(
"postgres://%s:%s@%s/%s?sslmode=disable",
conf.PostgresUser,
conf.PostgresPassword,
conf.PostgresHost,
conf.PostgresDB,
)
conf.PostgresURL = connStr
conf.RedisAddr = os.Getenv("REDIS_ADDR")
conf.GitHubClientID = os.Getenv("GITHUB_CLIENT_ID")
conf.GitHubClientSecret = os.Getenv("GITHUB_CLIENT_SECRET")
conf.ServiceName = os.Getenv("OTEL_SERVICE_NAME")
}
func main() {
ctx := context.Background()
var err error
redisConn, err = redisconn.NewRedisConn(ctx, conf.RedisAddr)
if err != nil {
log.Fatalf(
"unable to connect to redis: %v",
err,
)
}
dbConn, err = db.NewDBConn(ctx, conf.PostgresDB, conf.PostgresURL)
if err != nil {
log.Fatalf(
"unable to connect to db: %v",
err,
)
}
mux := http.NewServeMux()
mux.HandleFunc("GET /auth/github/callback", completeGitHubAuth)
mux.HandleFunc("GET /auth/github", redirectToGitHubLogin)
mux.HandleFunc("GET /auth/logout", logout)
mux.HandleFunc("GET /auth", renderAuth)
mux.Handle("GET /", requireAuth(http.HandlerFunc(index)))
mux.Handle("POST /upload", requireAuth(http.HandlerFunc(uploadImage)))
log.Println("Server started on port 8000")
log.Fatal(http.ListenAndServe(":8000", mux))
}