-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
131 lines (112 loc) · 3.76 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
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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"net/http/fcgi"
"net/http/httputil"
"os"
"runtime"
"github.com/BjoernSchilberg/najukoffer/kindergruppen"
"github.com/BjoernSchilberg/najukoffer/landesverbaende"
"github.com/BjoernSchilberg/najukoffer/storchenkoffer"
"github.com/BjoernSchilberg/najukoffer/termine"
jwt "github.com/dgrijalva/jwt-go"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
"github.com/rs/cors"
)
var appAddr string
var dbHost string
var dbUser string
var dbName string
var dbPasswd string
var signingKey []byte
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
runtime.GOMAXPROCS(runtime.NumCPU())
signingKey = []byte(os.Getenv("SIGNINGKEY"))
appAddr = os.Getenv("APPADDR")
dbHost = os.Getenv("DBHOST")
dbName = os.Getenv("DBNAME")
dbUser = os.Getenv("DBUSER")
dbPasswd = os.Getenv("DBPASSWD")
}
func requestLogger(targetMux http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println(err)
}
//log.Println(string(requestDump))
log.Println(fmt.Sprintf("%q", requestDump))
})
}
func isAuthorized(endpoint func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header["Token"] != nil {
token, err := jwt.Parse(r.Header["Token"][0], func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("There was an error")
}
return signingKey, nil
})
if err != nil {
fmt.Fprintf(w, err.Error())
}
if token.Valid {
endpoint(w, r)
}
} else {
fmt.Fprintf(w, "Not Authorized!")
}
})
}
func main() {
var err error
connectionString :=
fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true&collation=utf8mb4_general_ci&charset=utf8", dbUser, dbPasswd, dbHost, dbName)
db, err := sql.Open("mysql", connectionString)
if err != nil {
log.Fatal(err)
}
defer db.Close()
mux := http.NewServeMux()
if appAddr != "" {
// Run as a local web server
//mux.HandleFunc("/termine", isAuthorized termine.GetTermine(db)))
mux.HandleFunc("/today", termine.Get(db, "today"))
mux.HandleFunc("/this_week", termine.Get(db, "this_week"))
mux.HandleFunc("/this_month", termine.Get(db, "this_month"))
mux.HandleFunc("/this_year", termine.Get(db, "this_year"))
mux.HandleFunc("/next_6month", termine.GetNext6Month(db))
mux.HandleFunc("/kindergruppen", kindergruppen.Get())
mux.HandleFunc("/storchenkoffer", storchenkoffer.Get())
mux.HandleFunc("/landesverbaende", landesverbaende.Get())
log.Println("Listening on " + appAddr + "...")
//err = http.ListenAndServe(appAddr, requestLogger(mux))
// cors.Default() setup the middleware with default options being
// all origins accepted with simple methods (GET, POST). See
// documentation below for more options.
handler := cors.Default().Handler(mux)
err = http.ListenAndServe(appAddr, handler)
} else {
// Run as FCGI via standard I/O
mux.HandleFunc("/fcgi-bin/najukoffer/today", termine.Get(db, "today"))
mux.HandleFunc("/fcgi-bin/najukoffer/this_week", termine.Get(db, "this_week"))
mux.HandleFunc("/fcgi-bin/najukoffer/this_month", termine.Get(db, "this_month"))
mux.HandleFunc("/fcgi-bin/najukoffer/this_year", termine.Get(db, "this_year"))
mux.HandleFunc("/fcgi-bin/najukoffer/next_6month", termine.GetNext6Month(db))
mux.HandleFunc("/fcgi-bin/najukoffer/kindergruppen", kindergruppen.Get())
mux.HandleFunc("/fcgi-bin/najukoffer/storchenkoffer", storchenkoffer.Get())
mux.HandleFunc("/fcgi-bin/najukoffer/landesverbaende", landesverbaende.Get())
err = fcgi.Serve(nil, mux)
}
if err != nil {
log.Fatal(err)
}
}