This repository was archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
142 lines (115 loc) · 3.64 KB
/
Copy pathserver.go
File metadata and controls
142 lines (115 loc) · 3.64 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"os"
"log"
"time"
"fmt"
"net/http"
// "database/sql"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
)
var (
tokenInfoEndpoint string
tokenInfoUser string
tokenInfoPassword string
mysqlHost string
mysqlDatabase string
mysqlUsername string
mysqlPassword string
mysqlDSN string // Data Source Name
disableAuth = false // disable token introspection for testing purposes
mainRouter *mux.Router
)
func init() {
// token info
tokenInfoEndpoint = os.Getenv("tokenInfoEndpoint")
tokenInfoUser = os.Getenv("tokenInfoUser")
tokenInfoPassword = os.Getenv("tokenInfoPassword")
if os.Getenv("TESTING_NOAUTH") == "1" {
disableAuth = true
log.Printf("WARNING: token validation is disabled, use only for testing/development")
time.Sleep(time.Second * 2)
}
// mysqlHost = os.Getenv("MYSQL_HOST")
// mysqlDatabase = os.Getenv("MYSQL_DATABASE")
// mysqlUsername = os.Getenv("MYSQL_USER")
// mysqlPassword = os.Getenv("MYSQL_PASSWORD")
// // example: "root:password1@tcp(127.0.0.1:3306)/test"
// mysqlDSN = fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", mysqlUsername, mysqlPassword, mysqlHost, mysqlDatabase)
// log.Printf("mysqlHost: %s", mysqlHost)
// log.Printf("mysqlDatabase: %s", mysqlDatabase)
// log.Printf("mysqlUsername: %s", mysqlUsername)
// log.Printf("mysqlDSN: %s", mysqlDSN)
// count := 0
// for {
// count++
// db, err := sql.Open("mysql", mysqlDSN)
// if err != nil {
// if count > 1000 {
// log.Fatalf("(sql.Open) Unable to connect to database: %v", err)
// return
// }
// log.Printf("(sql.Open) Unable to connect to database: %v, retrying...", err)
// time.Sleep(time.Second * 3)
// continue
// }
// //err = db.Ping()
// for {
// _, err = db.Exec("DO 1")
// if err != nil {
// if count > 1000 {
// log.Fatalf("(db.Ping) Unable to connect to database: %v", err)
// return
// }
// log.Printf("(db.Ping) Unable to connect to database: %v, retrying...", err)
// time.Sleep(time.Second * 3)
// continue
// }
// break
// }
// break
// }
}
func createRouter() {
mainRouter = mux.NewRouter()
r := mainRouter
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{"id": "SAGE Edge Scheduler","available_resources":["api/v1/","metrics/"]}`)
})
log.Println("Sage Edge Scheduler API")
api := r.PathPrefix("/api/v1").Subrouter()
api.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{"id": "SAGE Edge Scheduler","available_resources":["goals"]}`)
})
api.Handle("/metrics", negroni.New(
negroni.HandlerFunc(authMW),
negroni.Wrap(http.HandlerFunc(getSESstats)),
)).Methods(http.MethodGet)
api.Handle("/goals", negroni.New(
negroni.HandlerFunc(authMW),
negroni.Wrap(http.HandlerFunc(listGoals)),
)).Methods(http.MethodGet)
api.Handle("/goals", negroni.New(
negroni.HandlerFunc(authMW),
negroni.Wrap(http.HandlerFunc(createGoal)),
)).Methods(http.MethodPost)
api.NewRoute().PathPrefix("/goals/{goal}/status").Handler(negroni.New(
negroni.HandlerFunc(authMW),
negroni.Wrap(http.HandlerFunc(getGoalStatus)),
)).Methods(http.MethodGet)
api.NewRoute().PathPrefix("/goals/{goal}/status").Handler(negroni.New(
negroni.HandlerFunc(authMW),
negroni.Wrap(http.HandlerFunc(addGoalStatus)),
)).Methods(http.MethodPost)
api.NewRoute().PathPrefix("/goals/metrics").Handler(negroni.New(
negroni.HandlerFunc(authMW),
negroni.Wrap(http.HandlerFunc(getGoalmetrics)),
)).Methods(http.MethodGet)
// match everything else...
api.NewRoute().PathPrefix("/").HandlerFunc(defaultHandler)
log.Fatalln(http.ListenAndServe(":8080", r))
}
func main() {
createRouter()
}