-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (50 loc) · 1.65 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 (
"database/sql"
"log"
"net"
"net/http"
"os"
Gorilla "github.com/gorilla/handlers"
"github.com/gorilla/mux"
// registers with database/sql
_ "github.com/lib/pq"
"github.com/spf13/viper"
"github.com/mattgen88/blog/handlers"
"github.com/mattgen88/blog/util"
)
func main() {
viper.AutomaticEnv()
// Gather configuration
viper.BindEnv("dsn")
dsn := viper.GetString("dsn")
viper.BindEnv("port")
viper.SetDefault("port", "8088")
port := viper.GetString("port")
viper.BindEnv("host")
viper.SetDefault("host", "127.0.0.1")
host := viper.GetString("host")
log.Println("Starting on ", host, " port ", port, " dsn ", dsn)
db, err := sql.Open("postgres", dsn)
if err != nil {
log.Fatal(err)
}
defer db.Close()
r := mux.NewRouter()
h := handlers.New(r, db)
r.HandleFunc("/", h.RootHandler).Name("root")
r.HandleFunc("/articles", h.ArticleListHandler)
r.HandleFunc("/articles/", h.ArticleListHandler)
r.HandleFunc("/categories", h.CategoryListHandler)
r.HandleFunc("/categories/", h.CategoryListHandler)
r.HandleFunc("/categories/{category}", h.CategoryHandler)
r.HandleFunc("/categories/{category}/", h.CategoryHandler)
r.HandleFunc("/articles/{id}", h.ArticleHandler)
r.HandleFunc("/articles/{id}/", h.ArticleHandler)
r.HandleFunc("/users", h.UsersListHandler)
r.HandleFunc("/users/", h.UsersListHandler)
r.HandleFunc("/users/{id}", h.UserHandler)
r.HandleFunc("/users/{id}/", h.UserHandler)
r.NotFoundHandler = http.HandlerFunc(handlers.ErrorHandler)
log.Fatal(http.ListenAndServe(net.JoinHostPort(host, port), util.ContentType(Gorilla.LoggingHandler(os.Stdout, Gorilla.CORS()(r)), "application/hal+json")))
}