-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
68 lines (50 loc) · 1.45 KB
/
app.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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
type App struct {
Router *mux.Router
DB *sql.DB
}
// Initialize a database connection
func (a *App) Initialize(user, password, dbname string) {
// docker-compose sets up the postgres database on network host named `db` for now
connStr :=
fmt.Sprintf("host=db user=%s password=%s dbname=%s sslmode=disable", user, password, dbname)
var err error
a.DB, err = sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
a.Router = mux.NewRouter()
ping_router := PingRouter{}
ping_router.Route(a.Router)
users_router := UsersRouter{DB: a.DB}
users_router.Route(a.Router)
recipes_router := RecipesRouter{DB: a.DB}
recipes_router.Route(a.Router)
registration_router := RegistrationRouter{DB: a.DB}
registration_router.Route(a.Router)
// protected endpoint requiring jwt auth
a.Router.HandleFunc("/private", a.PrivateResource).Methods("GET", "POST", "PUT", "PATCH", "DELETE")
a.Router.Use(loggingMiddleware)
http.Handle("/", a.Router)
}
// Start the app
func (a *App) Run(addr string) {
log.Println("Starting api... Listening on port", addr)
log.Fatal(http.ListenAndServe(addr, a.Router))
}
func (a *App) pingHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "pong!")
}
// test auth in this route
func (a *App) PrivateResource(w http.ResponseWriter, r *http.Request) {
return
}