-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.txt
66 lines (57 loc) · 1.56 KB
/
example.txt
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
/*
package main
import (
"flag"
"github.com/ajays20078/go-ini"
"github.com/gorilla/mux"
"github.com/ajays20078/go-http-async-logger"
"log"
"net"
"net/http"
"os"
"time"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func check_boolean(e bool) {
if !e {
panic("Unexpected Error!!!")
}
}
func main() {
wordPtr := flag.String("c", "/etc/goserver/config.ini", "config file")
file, err := ini.LoadFile(*wordPtr)
check(err)
port, ok := file.Get("Server", "Port")
check_boolean(ok)
virtual_server, ok := file.Get("Server", "VirtualHost")
check_boolean(ok)
access_log, ok := file.Get("Server", "AccessLog")
check_boolean(ok)
access_file_handler, err := os.OpenFile(access_log, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer access_file_handler.Close()
rtr := mux.NewRouter()
rtr.HandleFunc("/user/{name:[a-z]+}/profile", func(w http.ResponseWriter, r *http.Request) {
profile(w, r, access_file_handler)
}).Host(virtual_server).Methods("GET")
http.Handle("/", rtr)
log.Println("Listening...")
http.ListenAndServe(":"+port, nil)
}
func profile(w http.ResponseWriter, r *http.Request, access_file_handler *os.File) {
start_time := time.Now().UnixNano()
params := mux.Vars(r)
name := params["name"]
return_string := "Hello " + name
ip, port, _ := net.SplitHostPort(r.RemoteAddr)
return_string = return_string + "\t IP is" + ip + ":" + port
http_return_status := 200
w.Write([]byte(return_string))
defer asyncHttpLogger.LogLine(r, http_return_status, return_string, access_file_handler, start_time, time.Now().UnixNano())
}