-
Notifications
You must be signed in to change notification settings - Fork 0
/
angacloud.go
74 lines (66 loc) · 1.64 KB
/
angacloud.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"sync"
"time"
"github.com/gorilla/mux"
)
var mu sync.Mutex
var totalRequests int
var startTime = time.Now()
var default404 http.Handler = http.HandlerFunc(errorPage)
func main() {
r := mux.NewRouter()
r.HandleFunc("/", home)
r.HandleFunc("/debug", debugInfo)
r.NotFoundHandler = default404
fmt.Print("anga.cloud is active!\n")
fmt.Print("http://localhost:8080")
log.Fatal(http.ListenAndServe("localhost:8080", r))
}
func errorPage(w http.ResponseWriter, r *http.Request) {
increaseCount()
content, err := os.ReadFile("error.html")
if err != nil {
fmt.Printf("Error %s", err)
}
fmt.Fprint(w, string(content))
}
func increaseCount() {
mu.Lock()
totalRequests++
mu.Unlock()
}
func home(w http.ResponseWriter, r *http.Request) {
increaseCount()
w.Header().Set("Content-Type", "text/html")
content, err := os.ReadFile("index.html")
if err != nil {
fmt.Printf("Error %s", err)
}
fmt.Fprintf(w, string(content))
}
func debugInfo(w http.ResponseWriter, r *http.Request) {
increaseCount()
upTime := time.Since(startTime)
fmt.Fprintf(w, "~ANGA.CLOUD~\n")
fmt.Fprintf(w, "*************\n")
fmt.Fprintf(w, "Requests: %d\n", totalRequests)
fmt.Fprintf(w, "Start Time: %q\n Uptime: %s\n", startTime, upTime)
fmt.Fprintf(w, "%s %s %s\n", r.Method, r.URL, r.Proto)
// fmt.Fprintf(w, "Host OS:%s\n", )
for k, v := range r.Header {
fmt.Fprintf(w, "Header[%q] = %q\n", k, v)
}
fmt.Fprintf(w, "Host = %q\n", r.Host)
fmt.Fprintf(w, "RemoteAddr = %q\n", r.RemoteAddr)
if err := r.ParseForm(); err != nil {
log.Print(err)
}
for k, v := range r.Form {
fmt.Fprintf(w, "Form[%q] = %q\n", k, v)
}
}