-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
59 lines (52 loc) · 1.24 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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
var (
LOG_LEVEL = os.Getenv("LOG_LEVEL")
AUTH_SECRET = os.Getenv("AUTH_SECRET")
APP_PORT = os.Getenv("APP_PORT")
REDIS_HOST = os.Getenv("REDIS_HOST")
REDIS_PORT = os.Getenv("REDIS_PORT")
APIKEYS = make(map[string]ApiKey)
APIKEY_HEADER = "apikey"
)
func init() {
if LOG_LEVEL == "" {
LOG_LEVEL = "warn"
}
if AUTH_SECRET == "" {
AUTH_SECRET = "AhmetVehbiOlgac"
}
if APP_PORT == "" {
APP_PORT = "8080"
}
if REDIS_HOST == "" {
REDIS_HOST = "localhost"
}
if REDIS_PORT == "" {
REDIS_PORT = "6379"
}
}
func main() {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
router.Methods(route.Method).Path(route.Pattern).Handler(route.HandlerFunc)
}
if err := http.ListenAndServe(":"+APP_PORT, router); err != nil {
log.Fatalln(err)
}
}
func response(w http.ResponseWriter, code int, text string) {
responseObject(w, code, Response{Code: code, Text: text})
}
func responseObject(w http.ResponseWriter, code int, v interface{}) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
json.NewEncoder(w).Encode(v)
}