/
main.go
71 lines (64 loc) · 1.93 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
package main
import (
"fmt"
"log"
"net/http"
"github.com/ant0ine/go-json-rest/rest"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/lib/pq"
"github.com/mattes/migrate/migrate"
"github.com/spf13/viper"
)
import _ "github.com/mattes/migrate/driver/postgres"
func main() {
readConfig()
applyMigrations()
i := Impl{}
i.connectToDb()
i.startChat()
}
func readConfig() {
viper.SetEnvPrefix("chat")
viper.SetDefault("database_url", "postgres:///chat_development?sslmode=disable")
viper.SetDefault("bind_address", "localhost:8080")
viper.AutomaticEnv()
viper.BindEnv("database_url")
viper.BindEnv("bind_address")
}
func applyMigrations() {
allErrors, ok := migrate.UpSync(viper.GetString("database_url"), "./migrations")
fmt.Println("Database: ", viper.GetString("database_url"))
if !ok {
fmt.Println("Migratinos failed")
fmt.Println("driver: ", viper.GetString("database_url"))
fmt.Println("Errors: ", allErrors)
}
}
func (i *Impl) connectToDb() {
var err error
i.DB, err = gorm.Open("postgres", viper.GetString("database_url"))
if err != nil {
log.Fatalf("Got error when connect database, the error is '%v'", err)
}
i.DB.LogMode(true)
}
func (i *Impl) startChat() {
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Get("/", APIIndex),
rest.Get("/users/:user_id.json", i.APIUserShow),
rest.Get("/users/:user_id/dialogs.json", i.APIDialogIndex),
rest.Get("/users/:user_id/dialogs/:dialog_id/messages.json", i.APIMessageIndex),
rest.Get("/users/:user_id/dialogs/:dialog_id.json", i.APIDialogShow),
rest.Post("/users/:user_id/dialogs.json", i.APIDialogCreate),
rest.Post("/users/:user_id/dialogs/:dialog_id/messages.json", i.APIMessageCreate),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
fmt.Println("address: ", viper.GetString("bind_address"))
log.Fatal(http.ListenAndServe(viper.GetString("bind_address"), api.MakeHandler()))
}