Repository navigation
Expand file tree
/
Copy path_main.go
More file actions
115 lines (94 loc) · 2.92 KB
/
Copy path_main.go
File metadata and controls
115 lines (94 loc) · 2.92 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"database/sql"
"fmt"
"github.com/CelesteComet/celeste-auth-server/app"
"github.com/CelesteComet/celeste-auth-server/app/mhttp"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/google"
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
)
var (
host = "celestecomet.c7bjz8zer8ha.us-east-1.rds.amazonaws.com"
port = 5432
user = os.Getenv("AWS_DB_USERNAME")
password = os.Getenv("AWS_DB_PASSWORD")
dbname = "CelesteComet"
)
var (
connStr = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
)
type corsHandler struct {
next http.Handler
}
func (handler *corsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Expose-Headers", "JWT")
w.Header().Set("Access-Control-Expose-Headers", "Jwt")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Origin, Accept, token")
if (r.Method == "OPTIONS") {
w.WriteHeader(http.StatusOK)
return
} else {
handler.next.ServeHTTP(w, r)
}
}
func withCors(h http.Handler) http.Handler {
return &corsHandler{next: h}
}
// Oauth Middleware
type oauthHandler struct {
next http.Handler
}
var us mhttp.UserHandler
func (handler *oauthHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
log.Println("DOING ITS THING")
user, err := gothic.CompleteUserAuth(res, req)
if err != nil {
fmt.Fprintln(res, err)
return
}
log.Println(user)
log.Println(res)
us.CreateOrFindUser(user)
}
func main() {
log.Println("Starting Authentication Service")
log.Println("Connecting to AWS RDS Postgresql server")
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
log.Println("Server connection successful")
defer db.Close()
router := &mux.Router{}
s := &app.Server{Port: ":1337", DB: db, Router: router}
us = mhttp.UserHandler{DB: db}
// Routes
s.Router.Handle("/users", us.CreateUser()).Methods("POST")
s.Router.Handle("/login", us.FindUserByCredentials()).Methods("POST")
// OAuth
goth.UseProviders(
google.New(os.Getenv("GOOGLE_KEY"), os.Getenv("GOOGLE_SECRET"), "http://localhost:1337/auth/google/callback"),
)
s.Router.HandleFunc("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) {
if _ , err := gothic.CompleteUserAuth(res, req); err == nil {
// t, _ := template.New("foo").Parse(userTemplate)
// t.Execute(res, gothUser)
} else {
gothic.BeginAuthHandler(res, req)
}
})
// Middleware
withCorsRouter := withCors(s.Router)
loggedRouter := handlers.LoggingHandler(os.Stdout, withCorsRouter)
http.ListenAndServe(s.Port, loggedRouter)
log.Println("Service is now running on port 1337")
}