-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (61 loc) · 2.02 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
72
73
74
75
76
package main
import (
"fmt"
"github.com/GymWorkoutApp/gwa_auth/cache"
"github.com/GymWorkoutApp/gwa_auth/database"
"github.com/GymWorkoutApp/gwa_auth/models"
"github.com/GymWorkoutApp/gwa_auth/store"
"github.com/dgrijalva/jwt-go"
"log"
"net/http"
"os"
"github.com/GymWorkoutApp/gwa_auth/errors"
"github.com/GymWorkoutApp/gwa_auth/generates"
"github.com/GymWorkoutApp/gwa_auth/manager"
"github.com/GymWorkoutApp/gwa_auth/server"
)
func main() {
managerServer := manager.NewDefaultManager()
managerServer.MapAccessGenerate(generates.NewJWTAccessGenerate([]byte("00000000"), jwt.SigningMethodHS512))
// token memory store
managerServer.MapTokenStorage(redis.NewRedisStore(&redis.Options{
Addr: "127.0.0.1:6379",
DB: 15,
}))
srv := server.NewDefaultServer(managerServer)
db := database.NewManageDB().Get()
defer db.Close()
db.AutoMigrate(&models.Client{})
// client memory store
clientStore := store.NewClientStoreDB()
clientStore.Set(&models.Client{
Secret: "999999",
Domain: "http://localhost",
})
managerServer.MapClientStorage(clientStore)
srv.SetAllowGetAccessRequest(true)
srv.SetClientInfoHandler(server.ClientFormHandler)
srv.SetInternalErrorHandler(func(err error) (re *errors.Response) {
log.Println("Internal Error:", err.Error())
re = &errors.Response{Error: err, StatusCode: 500, Description: err.Error()}
return
})
srv.SetResponseErrorHandler(func(re *errors.Response) {
log.Println("Response Error:", re.Error.Error())
})
http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
err := srv.HandleAuthorizeRequest(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
})
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
srv.HandleTokenRequest(w, r)
})
http.HandleFunc("/introspect", func (w http.ResponseWriter, r *http.Request) {
srv.HandleIntrospectRequest(w, r)
})
port := os.Getenv("PORT")
log.Println(fmt.Sprintf("Running on :%v", port))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v",port), nil))
}