-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
91 lines (68 loc) · 1.64 KB
/
main.go
File metadata and controls
91 lines (68 loc) · 1.64 KB
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
package main
import (
"context"
"log"
"time"
"github.com/dagulv/train-api/internal/adapter/http"
"github.com/dagulv/train-api/internal/adapter/postgres"
"github.com/dagulv/train-api/internal/domain/user"
"github.com/dagulv/train-api/internal/env"
"github.com/dagulv/train-api/internal/utils"
"github.com/go-webauthn/webauthn/webauthn"
jsoniter "github.com/json-iterator/go"
"github.com/rs/xid"
)
func main() {
ctx := context.Background()
if err := start(ctx); err != nil {
log.Fatal(err)
}
}
func start(ctx context.Context) (err error) {
env, err := env.GetEnv()
if err != nil {
return
}
db, err := postgres.Connect(ctx, env)
if err != nil {
return
}
defer db.Close()
userStore := postgres.User(db)
userService := user.Service{
Store: userStore,
}
wconfig := &webauthn.Config{
RPDisplayName: "Train",
RPID: "train.local",
RPOrigins: []string{"https://train.local"},
}
webAuthn, err := webauthn.New(wconfig)
if err != nil {
return
}
json := jsoniter.ConfigFastest
server := http.Server{
Json: json,
WebAuthn: webAuthn,
User: userService,
}
if err = createAdminUser(ctx, userService); err != nil {
return
}
return server.StartServer(ctx)
}
func createAdminUser(ctx context.Context, userService user.Service) (err error) {
var user user.User
email := "admin@admin.admin"
if err = userService.GetByEmail(ctx, email, &user); err == nil {
return
}
user.Id = xid.NewWithTime(time.Now())
user.FirstName = "Admin"
user.LastName = "Admin"
user.Email = email
user.TimeCreated = utils.Timestamptz()
user.TimeUpdated = user.TimeCreated
return userService.Insert(ctx, &user)
}