-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple_http_server.go
124 lines (107 loc) · 3.45 KB
/
simple_http_server.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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"time"
"github.com/common-nighthawk/go-figure"
"pareshpawar.com/simple-http-server/utils"
)
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/html/", htmlhandler)
http.HandleFunc("/healthcheck", healthhandler)
serverBrand := figure.NewColorFigure("Simple HTTP Server", "straight", "green", true)
serverBrand.Print()
myBrand := figure.NewColorFigure("by PareshPawar.com", "term", "green", true)
myBrand.Print()
log.Print("pareshpawar/simple-http-server: Simple HTTP Server Running on port 8081")
log.Fatal(http.ListenAndServe("0.0.0.0:8081", nil))
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func htmlhandler(w http.ResponseWriter, r *http.Request) {
timestamp := time.Now()
if r.Method == "GET" {
fmt.Print(string("\033[34m"))
} else if r.Method == "POST" {
fmt.Print(string("\033[33m"))
} else if r.Method == "PUT" {
fmt.Print(string("\033[35m"))
} else if r.Method == "DELETE" {
fmt.Print(string("\033[31m"))
} else {
fmt.Print(string("\033[36m"))
}
fmt.Printf("%s %s %s %s ===> from %s\n", timestamp.Local(), r.Method, r.URL, r.Proto, r.RemoteAddr)
file, err := os.ReadFile("html/index.html")
check(err)
template, err := template.New("webpage").Parse(string(file))
check(err)
type reqDataStruct struct{ ReqTime, ReqType, Host, Remote, RemoteAddr string }
reqData := reqDataStruct{
ReqTime: timestamp.String(),
ReqType: r.Proto + " " + r.Method + " " + r.URL.Path,
Host: r.Host,
Remote: r.RemoteAddr,
RemoteAddr: utils.GetMyOutboundIP().String(),
}
data := struct {
Request reqDataStruct
Headers http.Header
}{
Request: reqData,
Headers: r.Header,
}
err = template.Execute(w, data)
check(err)
}
func handler(w http.ResponseWriter, r *http.Request) {
timestamp := time.Now()
if r.Method == "GET" {
fmt.Print(string("\033[34m"))
} else if r.Method == "POST" {
fmt.Print(string("\033[33m"))
} else if r.Method == "PUT" {
fmt.Print(string("\033[35m"))
} else if r.Method == "DELETE" {
fmt.Print(string("\033[31m"))
} else {
fmt.Print(string("\033[36m"))
}
fmt.Printf("%s %s %s %s ===> from %s\n", timestamp.Local(), r.Method, r.URL, r.Proto, r.RemoteAddr)
fmt.Fprintf(w, "Request Time ==> %s\n", timestamp)
fmt.Fprintf(w, "Request Type ==> %s %s %s\n", r.Method, r.URL, r.Proto)
fmt.Fprintf(w, "Hostname/Host ==> %s\n", r.Host)
fmt.Fprintf(w, "Remote Address ==> %s\n", r.RemoteAddr)
fmt.Fprintf(w, "Local Address ==> %s\n\n", utils.GetMyOutboundIP())
// print request headers
for key, value := range r.Header {
fmt.Fprintf(w, "Header[%q] = %s\n", key, value)
}
// log form errors
if err := r.ParseForm(); err != nil {
log.Print(err)
}
// print form data key-value pairs
for key, value := range r.Form {
fmt.Fprintf(w, "FormData[%q] = %q\n", key, value)
}
// print the environment variable
fmt.Fprintf(w, "\nYOUR_ENV = %s\n", os.Getenv("YOUR_ENV"))
// print brand
serverBrand := figure.NewColorFigure("Simple HTTP Server", "straight", "green", true)
fmt.Fprintf(w, "__________________________________________________________\n")
figure.Write(w, serverBrand)
fmt.Fprintf(w, "----------------------------------------------------------\n")
fmt.Fprintf(w, " by PareshPawar.com \n")
fmt.Fprintf(w, "----------------------------------------------------------\n")
}
func healthhandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK\n")
}