-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
39 lines (29 loc) · 741 Bytes
/
api.go
File metadata and controls
39 lines (29 loc) · 741 Bytes
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
package main
import (
"database/sql"
"log"
"net/http"
"github.com/gorilla/mux"
_ "github.com/joho/godotenv/autoload"
)
type APIServer struct {
addr string
db *sql.DB
}
func NewAPIServer(addr string, db *sql.DB) *APIServer {
return &APIServer{addr: addr, db: db}
}
func (s *APIServer) Run() {
router := mux.NewRouter()
subrouter := router.PathPrefix("/api/v1").Subrouter()
store := NewStore(s.db)
fromEmail := EnvString("FROM_EMAIL", "")
if fromEmail == "" {
log.Fatal("FROM_EMAIL must be set")
}
mailer := NewSmtp2goMailer(fromEmail)
service := NewService(store, mailer)
service.RegisterRoutes(subrouter)
log.Println("starting the API server at ", s.addr)
log.Fatal(http.ListenAndServe(s.addr, subrouter))
}