-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
executable file
·76 lines (64 loc) · 1.69 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
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"github.com/joesantosio/example-go-project/config"
"github.com/joesantosio/example-go-project/entity"
"github.com/joesantosio/example-go-project/infrastructure/inmem"
"github.com/joesantosio/example-go-project/infrastructure/sqlite"
"github.com/joesantosio/example-go-project/interfaces/server"
serverchi "github.com/joesantosio/example-go-project/interfaces/server_chi"
serverstd "github.com/joesantosio/example-go-project/interfaces/server_std"
)
func initRepos(dbType string, dbPath string) (repos entity.Repositories, err error) {
switch dbType {
case "sqlite":
if dbPath == "" {
err = errors.New("DB_PATH not provided")
break
}
db, err := sqlite.Connect(dbPath)
if err != nil {
err = errors.New(fmt.Sprintf("error initializing db sqlite: %v", err))
break
}
repos, err = sqlite.InitRepos(db)
if err != nil {
err = errors.New(fmt.Sprintf("error initializing repos sqlite: %v", err))
break
}
default:
repos, err = inmem.InitRepos()
if err != nil {
err = errors.New(fmt.Sprintf("error initializing repos inmem: %v", err))
break
}
}
return repos, nil
}
func main() {
config, err := config.Get(os.Getenv)
if err != nil {
log.Fatal(err)
return
}
repos, err := initRepos(config.DBType, config.DBPath)
if err != nil {
log.Fatal(err)
return
}
defer repos.Close()
var r http.Handler
switch config.ServerPackage {
case "chi":
r = serverchi.GetRouter(config.AuthSecret, repos)
default:
r = serverstd.GetRouter(config.AuthSecret, repos)
}
fmt.Printf("DB type: %s\n", config.DBType)
fmt.Printf("Server package: %s\n", config.ServerPackage)
server.InitServer(":"+config.Port, r)
}