-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (107 loc) · 3 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
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"context"
"log"
"net/http"
"os"
"text/template"
"time"
"github.com/0xhjohnson/clacksy/models"
"github.com/0xhjohnson/clacksy/ui"
"github.com/alexedwards/scs/pgxstore"
"github.com/alexedwards/scs/v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/jackc/pgtype"
pgtypeuuid "github.com/jackc/pgtype/ext/gofrs-uuid"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type application struct {
errorLog *log.Logger
infoLog *log.Logger
sessionManager *scs.SessionManager
templateCache map[string]*template.Template
users *models.UserModel
soundtests *models.SoundTestModel
parts *models.PartsModel
votes *models.VoteModel
s3Client *s3.S3
}
func main() {
databaseURL := os.Getenv("DATABASE_URL")
port := os.Getenv("PORT")
if port == "" {
port = "4000"
}
addr := ":" + port
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
dbpool, err := openDbPool(databaseURL)
if err != nil {
errorLog.Fatal(err)
}
defer dbpool.Close()
templateCache, err := newTemplateCache(ui.Files)
if err != nil {
errorLog.Fatal(err)
}
sessionManager := scs.New()
sessionManager.Store = pgxstore.New(dbpool)
sessionManager.Lifetime = 12 * time.Hour
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(os.Getenv("B2_KEY_ID"), os.Getenv("B2_APP_KEY"), ""),
Endpoint: aws.String("s3.us-west-004.backblazeb2.com"),
Region: aws.String("us-west-004"),
S3ForcePathStyle: aws.Bool(true),
}
sess := session.Must(session.NewSession(s3Config))
s3Client := s3.New(sess)
app := &application{
errorLog: errorLog,
infoLog: infoLog,
sessionManager: sessionManager,
templateCache: templateCache,
users: &models.UserModel{DB: dbpool},
soundtests: &models.SoundTestModel{DB: dbpool},
parts: &models.PartsModel{DB: dbpool},
votes: &models.VoteModel{DB: dbpool},
s3Client: s3Client,
}
srv := &http.Server{
Addr: addr,
ErrorLog: errorLog,
Handler: app.routes(),
IdleTimeout: 2 * time.Minute,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
infoLog.Printf("Starting server on %s", addr)
err = srv.ListenAndServe()
errorLog.Fatal(err)
}
func openDbPool(dsn string) (*pgxpool.Pool, error) {
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, err
}
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
conn.ConnInfo().RegisterDataType(pgtype.DataType{
Value: &pgtypeuuid.UUID{},
Name: "uuid",
OID: pgtype.UUIDOID,
})
return nil
}
pool, err := pgxpool.ConnectConfig(context.Background(), config)
if err != nil {
return nil, err
}
err = pool.Ping(context.Background())
if err != nil {
return nil, err
}
return pool, nil
}