-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (46 loc) · 1.08 KB
/
main.go
File metadata and controls
55 lines (46 loc) · 1.08 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
package main
import (
"context"
"net/http"
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/go-redis/redis/v8"
pioj "github.com/piterator-org/pioj/app"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type DatabaseConfiguration struct {
MongoDB struct {
URI string
}
Redis *redis.Options
}
type UserConfiguration struct {
pioj.Configuration
Database DatabaseConfiguration
}
func main() {
var config UserConfiguration
if len(os.Args) > 1 {
if _, err := toml.DecodeFile(os.Args[1], &config); err != nil {
panic(err)
}
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(config.Database.MongoDB.URI))
if err != nil {
panic(err)
}
defer func() {
if err = client.Disconnect(ctx); err != nil {
panic(err)
}
}()
database := client.Database("pioj")
rdb := redis.NewClient(config.Database.Redis)
if err := http.ListenAndServe(":8080", pioj.NewApp(config.Configuration, database, rdb).ServeMux); err != nil {
panic(err)
}
}