-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
112 lines (89 loc) · 1.92 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"runtime"
"github.com/BurntSushi/toml"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
const (
VERSION = "0.1"
EVENTS_LOG = "events.log"
ERROR_LOG = "error.log"
)
var (
errLog *log.Logger
logger *log.Logger
conf Config
)
var (
flag_v, flag_debug, flag_help bool
)
type Config struct {
Debug bool
Authen bool
}
func init() {
flag.BoolVar(&flag_v, "v", false, "Version")
flag.BoolVar(&flag_debug, "debug", false, "Debug")
flag.BoolVar(&flag_help, "h", false, "Help")
flag.Parse()
if flag_v {
fmt.Println("Version:", VERSION)
os.Exit(0)
}
if flag_help {
flag.Usage()
os.Exit(0)
}
//create error logger
f0, err := os.OpenFile(ERROR_LOG, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
errLog.Fatal("[error]opening error file: %v", err)
}
errLog = log.New(f0, "", log.Lshortfile|log.LstdFlags)
//create events logger
f1, err := os.OpenFile(EVENTS_LOG, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
errLog.Fatal("[error]opening error file: %v", err)
}
logger = log.New(f1, "", log.LstdFlags)
//Decode config.toml
if _, err := toml.DecodeFile("config.toml", &conf); err != nil {
errLog.Fatal(err)
}
}
func PingHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.RemoteAddr + "\nAlita(小艾):I'm here!\n"))
}
func main() {
runtime.GOMAXPROCS(2)
logger.Println("Start......")
//Single instance
Singleton()
//connect to DB
//db, err := DBConnect()
//if err != nil {
// os.Exit(0)
//}
//db.Close()
//Create Router
r := mux.NewRouter()
//use logging Middleware
r.Use(loggingMiddleware)
///use auth Middleware
amw := authenticationMiddleware{}
amw.Populate()
if conf.Authen {
r.Use(amw.Middleware)
}
//handler
r.HandleFunc("/", PingHandler)
r.HandleFunc("/log", LogHandler)
r.HandleFunc("/simple", SimpleHandler)
logger.Fatal(http.ListenAndServe(":9900", r))
}