Skip to content

Commit

Permalink
GoApp Added with Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinav-26 committed Jun 28, 2021
1 parent 551ae82 commit 15fb00e
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 0 deletions.
12 changes: 12 additions & 0 deletions go-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.8.5-jessie

RUN mkdir /go-app

# create a working directory
WORKDIR /go-app

# add source code
ADD . /go-app

# run main.go
CMD ["go", "run", "app.go"]
60 changes: 60 additions & 0 deletions go-app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"html/template"
"log"
"net/http"
"os"
"path/filepath"
)

// func handler(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
// }

func main() {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))

http.HandleFunc("/", serveTemplate)

log.Println("Listening on :8080...")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}

func serveTemplate(w http.ResponseWriter, r *http.Request) {
lp := filepath.Join("templates", "layout.html")
fp := filepath.Join("templates", filepath.Clean(r.URL.Path))

info, err := os.Stat(fp)
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, r)
return
}
}

// Return a 404 if the request is for a directory
if info.IsDir() {
http.NotFound(w, r)
return
}

tmpl, err := template.ParseFiles(lp, fp)
if err != nil {
// Log the detailed error
log.Println(err.Error())
// Return a generic "Internal Server Error" message
http.Error(w, http.StatusText(500), 500)
return
}

err = tmpl.ExecuteTemplate(w, "layout", nil)
if err != nil {
log.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
}
}
41 changes: 41 additions & 0 deletions go-app/static/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
body{
background-color:aliceblue;
padding: 2rem;
}

/* DEMO-SPECIFIC STYLES */
.typewriter{
margin-bottom: 20px;
}
.typewriter h1 {
color: #333;
text-align: center;
font-family: monospace;
overflow: hidden; /* Ensures the content is not revealed until the animation */
white-space: nowrap; /* Keeps the content on a single line */
margin: 0 auto; /* Gives that scrolling effect as the typing happens */
letter-spacing: .15em; /* Adjust as needed */
animation:
typing 3.5s steps(30, end),
blink-caret .5s step-end infinite;
}

/* The typing effect */
@keyframes typing {
from { width: 0 }
to { width: 100% }
}

/* The typewriter cursor effect */
@keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: rgb(0, 217, 255) }
}

img{
display: block;
margin-left: auto;
margin-right: auto;
width: 80%;
}

Binary file added go-app/static/images/hello_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions go-app/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{define "title"}}Go Web Template{{end}}

{{define "body"}}
<div class="typewriter">
<h1>Hello World! Welcome to the Go Application!</h1>
</div>

<img src="/static/images/hello_world.png" alt="Hello World">
{{end}}
17 changes: 17 additions & 0 deletions go-app/templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{define "layout"}}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>GoWebTemplate</title>
<link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<div class="typewriter">
<h1>Hello World! Welcome to the Go Application!</h1>
</div>

<img src="/static/images/hello_world.png" alt="Hello World">
</body>
</html>
{{end}}

0 comments on commit 15fb00e

Please sign in to comment.