From 484ecd1cc30aeb6f2f2e4a8e59d115f67b0ce873 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 10 Sep 2023 23:18:30 +0300 Subject: [PATCH] init: main and templates --- main.go | 96 +++++++++++++++++++++++++++++++++++++++ templates/index-pico.html | 92 +++++++++++++++++++++++++++++++++++++ templates/index-tail.html | 72 +++++++++++++++++++++++++++++ templates/index.html | 19 ++++++++ 4 files changed, 279 insertions(+) create mode 100644 main.go create mode 100644 templates/index-pico.html create mode 100644 templates/index-tail.html create mode 100644 templates/index.html diff --git a/main.go b/main.go new file mode 100644 index 0000000..9ad6450 --- /dev/null +++ b/main.go @@ -0,0 +1,96 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "time" +) + +const ( + DEFAULT_HTTP_PORT = "8080" + DEFAULT_HTTPS_PORT = "4433" +) + +type apiMessageResponse struct { + Message string +} + +type Logger struct { + Handler http.Handler +} + +func (l *Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) { + + tNow := time.Now().UTC() + l.Handler.ServeHTTP(w, r) + methodString := l.getMethodLogString(r.Method) + log.Printf(" %s %s %s %v", r.RemoteAddr, methodString, r.URL, time.Since(tNow)) +} + +func (l *Logger) getMethodLogString(method string) string { + colorBlue := "\033[34m" + colorReset := "\033[0m" + // colorRed := "\033[31m" + // colorGreen := "\033[32m" + // colorYellow := "\033[33m" + // colorPurple := "\033[35m" + // colorCyan := "\033[36m" + // colorWhite := "\033[37m" + if method == "GET" { return colorBlue + "GET" + colorReset } + return method +} + +func NewLogger(handler http.Handler) *Logger { + return &Logger{Handler: handler} +} + +func writeJSONResponse(w http.ResponseWriter, s string) http.ResponseWriter { + w.WriteHeader(http.StatusBadRequest) + jsonResp, err := json.Marshal(apiMessageResponse{Message: s}) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return w + } + _, err = w.Write(jsonResp) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return w + } + + return w + +} + +func main() { + + handler := http.NewServeMux() + + handler.HandleFunc( + "/download", + func(w http.ResponseWriter, r *http.Request) { + userURL := r.URL.Query().Get("url") + w.Header().Set("Content-Type", "application/json") + if userURL == "" { + w = writeJSONResponse(w, "Provide URL as query") + return + } + w = writeJSONResponse(w, fmt.Sprintf("You sent %s ", userURL))}, + ) + + wrappedHandler := NewLogger(handler) + srv := http.Server{ + ReadTimeout: 3 * time.Second, + WriteTimeout: 10 * time.Second, + Addr: ":" + DEFAULT_HTTP_PORT, + Handler: wrappedHandler, + } + + log.Printf("Starting HTTP on %s", DEFAULT_HTTP_PORT) + log.Fatalln(srv.ListenAndServe()) +} + + + + diff --git a/templates/index-pico.html b/templates/index-pico.html new file mode 100644 index 0000000..08f7e87 --- /dev/null +++ b/templates/index-pico.html @@ -0,0 +1,92 @@ + + + + + + Sign in example • Pico.css + + + + + + + + + + + + + + + + + +
+
+
+
+

Sign in

+

A minimalist layout for Login pages

+
+
+ + +
+ +
+ +
+
+
+
+
+ + + + + + + + + + + diff --git a/templates/index-tail.html b/templates/index-tail.html new file mode 100644 index 0000000..1b6e1fe --- /dev/null +++ b/templates/index-tail.html @@ -0,0 +1,72 @@ + + + + + + + + + + + + +
+
+ Your Company +

Sign in to your account

+
+ +
+
+
+ +
+ +
+
+ +
+
+ + +
+
+ +
+
+ +
+ +
+
+ +

+ Not a member? + Start a 14 day free trial +

+
+
+ + diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..1945466 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,19 @@ + + + + + + + Hello, world! + + +
+

Download Videos

+
+ + +
+ +
+ +