Skip to content

Commit

Permalink
Merge pull request #13 from VanTekken/main
Browse files Browse the repository at this point in the history
Made HTML output dynamic to include request variables
  • Loading branch information
pareshpawar authored Oct 29, 2023
2 parents 4d51c33 + a881f7d commit e107b71
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
25 changes: 15 additions & 10 deletions html/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple HTTP Server</title>
</head>
<body>
<h1>Welcome to Simple HTTP Server</h1>
<h3>by Paresh Pawar</h3>
</body>
</html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Simple HTTP Server</title>
</head>
<body>
<h1>Welcome to Simple HTTP Server</h1>
<h3>by Paresh Pawar</h3>
<ul>
<li>Origin IP: {{.Origin}}</li>
<li>Request Method: {{.Type}}</li>
<li>Destination: {{.Hostname}}</li>
</ul>
</body>
</html>
32 changes: 30 additions & 2 deletions simple_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ 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.Handle("/html/", http.StripPrefix("/html/", http.FileServer(http.Dir("./html"))))
http.HandleFunc("/html/", htmlhandler)
serverBrand := figure.NewColorFigure("Simple HTTP Server", "straight", "green", true)
serverBrand.Print()
myBrand := figure.NewColorFigure("by PareshPawar.com", "term", "green", true)
Expand All @@ -23,6 +23,34 @@ func main() {
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){

file, err := os.ReadFile("html/index.html")
check(err)

template, err := template.New("webpage").Parse(string(file))
check(err)

data := struct {
Origin string
Type string
Hostname string
}{
Origin: r.RemoteAddr,
Type: r.Method,
Hostname: r.Host,
}

err = template.Execute(w, data)
check(err)
}

func handler(w http.ResponseWriter, r *http.Request) {
timestamp := time.Now()
if r.Method == "GET" {
Expand Down

0 comments on commit e107b71

Please sign in to comment.