-
Notifications
You must be signed in to change notification settings - Fork 0
/
shorturlserver.go
38 lines (35 loc) · 914 Bytes
/
shorturlserver.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
package shorturlserver
import (
"database/sql"
"github.com/adilku/shorturlservice/internal/app/store/postgrestore"
"github.com/adilku/shorturlservice/internal/app/store/simplestore"
"net/http"
)
func Start(config *Config, dbOption string) error {
if dbOption == "postgres" {
db, err := newDB(config.DatabaseURL)
if err != nil {
return err
}
defer db.Close()
store := postgrestore.New(db)
s := newServer(store)
s.logger.Println("statring at port", config.BindAddr)
return http.ListenAndServe(config.BindAddr, s)
} else {
store := simplestore.New()
s := newServer(store)
s.logger.Println("statring at port", config.BindAddr)
return http.ListenAndServe(config.BindAddr, s)
}
}
func newDB(dabaseURL string) (*sql.DB, error) {
db, err := sql.Open("postgres", dabaseURL)
if err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
return nil, err
}
return db, nil
}