Skip to content

Commit

Permalink
Merge pull request #1 from ShravanMeena/master
Browse files Browse the repository at this point in the history
Update from original
  • Loading branch information
nicm42 authored Oct 13, 2019
2 parents 47f9a20 + 11a0101 commit 109ad62
Show file tree
Hide file tree
Showing 171 changed files with 15,962 additions and 160 deletions.
Binary file added Basic Website Designs/GoPage/.DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions Basic Website Designs/GoPage/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:alpine
RUN apk add git
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN go get -d github.com/gorilla/mux
RUN go build -o main .
RUN adduser -S -D -H -h /app appuser
USER appuser
CMD ["./main"]

6 changes: 6 additions & 0 deletions Basic Website Designs/GoPage/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'
services:
go_page:
build: .
ports:
- "8080:8080"
108 changes: 108 additions & 0 deletions Basic Website Designs/GoPage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"encoding/json"
"io"
"log"
"net/http"
"strconv"

"github.com/gorilla/mux"
)

// Cat structure type (object)
type Cat struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Pic string `json:"pic"`
}

// Dog structure type (object)
type Dog struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Pic string `json:"pic"`
}

var cats = []Cat{
Cat{ID: 1, Name: "Lemon", Age: 3, Pic: "https://images2.minutemediacdn.com/image/upload/c_crop,h_1193,w_2121,x_0,y_175/f_auto,q_auto,w_1100/v1554921998/shape/mentalfloss/549585-istock-909106260.jpg"},
Cat{ID: 2, Name: "Lyra", Age: 8, Pic: "https://www.petmd.com/sites/default/files/Senior-Cat-Care-2070625.jpg"},
Cat{ID: 3, Name: "Howl", Age: 5, Pic: "https://img.washingtonpost.com/wp-apps/imrs.php?src=https://img.washingtonpost.com/rf/image_960w/2010-2019/WashingtonPost/2016/05/05/Interactivity/Images/iStock_000001440835_Large1462483441.jpg&w=1484"},
Cat{ID: 4, Name: "Faraday", Age: 3, Pic: "http://r.ddmcdn.com/s_f/o_1/cx_462/cy_245/cw_1349/ch_1349/w_720/APL/uploads/2015/06/caturday-shutterstock_149320799.jpg"},
Cat{ID: 5, Name: "Monet", Age: 1, Pic: "https://s.marketwatch.com/public/resources/images/MW-HP036_CatOnC_ZH_20190808094806.jpg"},
Cat{ID: 6, Name: "Olive", Age: 10, Pic: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQdMIU_4V4XtUAiV2uOBmeixkhQuy6N3eaHH1XuUzOYFyQZBZefEg"},
Cat{ID: 7, Name: "Ramen", Age: 4, Pic: "https://static.scientificamerican.com/sciam/cache/file/32665E6F-8D90-4567-9769D59E11DB7F26_source.jpg?w=590&h=800&7E4B4CAD-CAE1-4726-93D6A160C2B068B2"},
}

var dogs = []Dog{
Dog{ID: 1, Name: "Adna", Age: 7, Pic: "http://www.dictionary.com/e/wp-content/uploads/2018/05/doggo-300x300.jpg"},
Dog{ID: 2, Name: "Kata", Age: 10, Pic: "https://i.ytimg.com/vi/7NYaGOyJiCY/maxresdefault.jpg"},
Dog{ID: 3, Name: "Newton", Age: 2, Pic: "http://www.notinthedoghouse.com/wp-content/uploads/2014/02/dog-with-closed-eyes.jpg"},
Dog{ID: 4, Name: "Faraday", Age: 9, Pic: "https://i.pinimg.com/originals/a2/07/78/a207785ece9d08a17326f9709207f0a8.jpg"},
Dog{ID: 5, Name: "Hoss", Age: 15, Pic: "https://cdn2-www.dogtime.com/assets/uploads/gallery/finnish-spitz-dogs-and-puppies/finnish-spitz-dogs-puppies-1.jpg"},
Dog{ID: 6, Name: "Franklin", Age: 1, Pic: "https://static.independent.co.uk/s3fs-public/thumbnails/image/2019/09/04/13/istock-1031307988.jpg?w968h681"},
}

func catsHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(cats)
}

func catHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(req)
id, _ := strconv.Atoi(params["id"])
for _, cat := range cats {
if cat.ID == id {
json.NewEncoder(w).Encode(cat)
return
}
}
w.WriteHeader(http.StatusNotFound)
}

func dogsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(dogs)
}

func dogHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(req)
id, _ := strconv.Atoi(params["id"])
for _, dog := range dogs {
if dog.ID == id {
json.NewEncoder(w).Encode(dog)
return
}
}
w.WriteHeader(http.StatusNotFound)
}

func testHandler(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "Hello, Mux!\n")
}

func indexHandler(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "static/index.html")
}

func funPageHandler(w http.ResponseWriter, req *http.Request) {
http.ServeFile(w, req, "static/contact.html")
}

func main() {
r := mux.NewRouter()
r.HandleFunc("/", indexHandler).Methods("GET")
r.HandleFunc("/test", testHandler).Methods("GET")
r.HandleFunc("/index", indexHandler).Methods("GET")
r.HandleFunc("/cats", catsHandler).Methods("GET")
r.HandleFunc("/cats/{id:[0-9]+}", catHandler).Methods("GET")
r.HandleFunc("/dogs", dogsHandler).Methods("GET")
r.HandleFunc("/dogs/{id:[0-9]+}", dogHandler).Methods("GET")
r.HandleFunc("/func", funPageHandler)
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":8080", nil))
}
16 changes: 16 additions & 0 deletions Basic Website Designs/GoPage/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
***SIMPLE! Website/Server using Go language***

Created this simple website using Golang and HTML while showing how to work with JSON in Go.
The main objective was to show simple, well-rounded examples of a website/app while using common tools.
In this case, Golang, HTML, and Docker were used to show how fun and easy Golang and Docker can be!

**It's very easy to spin this web service up:**

- Just simply run the bash script included in the folder (```serve.sh```).
- The bash script will run two commands: `docker-compose down` && `docker-compose up`
- Alternatively, you can run these two commands manually in the terminal/command prompt/powershell.
- Then, visit ```localhost:8080``` in your favorite browser.

*When using the Docker command(s), it's unnecessary to have Go installed and setup on your computer (THIS is one of the main beauties behind the bacon, which is GO!).*

- Hunter Hartline -- [[email protected]]
4 changes: 4 additions & 0 deletions Basic Website Designs/GoPage/serve.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

docker-compose down
docker-compose up -d --build --force-recreate
19 changes: 19 additions & 0 deletions Basic Website Designs/GoPage/static/contact.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="/styles/contact.css">
<title>Document</title>
</head>

<body>
<a href="/">Home</a>
<img id="contact-img"
src="https://cdn.onebauer.media/one/empire-tmdb/films/686/images/vlulJKZ843k2fCRgIaUlm8X2EWz.jpg?quality=50&width=1800&ratio=16-9&resizeStyle=aspectfill&format=jpg"
alt="Jodie Foster/Contact">
</body>

</html>
48 changes: 48 additions & 0 deletions Basic Website Designs/GoPage/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>

<body>
<h1>Index Page</h1>
<a href="/func">Contact</a>
<br>
<br>
<a href="/cats">Cats</a>
<br>
<a href="/cats/1">Lemon</a>
<br>
<a href="/cats/2">Lyra</a>
<br>
<a href="/cats/3">Howl</a>
<br>
<a href="/cats/4">Faraday</a>
<br>
<a href="/cats/5">Monet</a>
<br>
<a href="/cats/6">Olive</a>
<br>
<a href="/cats/7">Ramen</a>
<br>
<br>
<a href="/dogs">Dogs</a>
<br>
<a href="/dogs/1">Ada</a>
<br>
<a href="/dogs/2">Kata</a>
<br>
<a href="/dogs/3">Newton</a>
<br>
<a href="/dogs/4">Faraday</a>
<br>
<a href="/dogs/5">Hoss</a>
<br>
<a href="/dogs/6">Franklin</a>
</body>

</html>
6 changes: 6 additions & 0 deletions Basic Website Designs/GoPage/static/styles/contact.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#contact-img{
/* display: block;
position: relative; */
height: 350px;
border-radius: 7px;
}
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
autohr @soumyadip
*/
var Calculator = {

results_id: 'calculator-result',
results_value: '0',
memory_id: 'calculator-screen',
memory_value: '',
history_id: 'calc-history-list',
history_value: [],

SUM: ' + ',
MIN: ' - ',
DIV: ' / ',
MULT: ' * ',
PROC: '%',
SIN: 'sin(',
COS: 'cos(',
MOD: ' mod ',
BRO: '(',
BRC: ')',

calculate: function() {
this.history_value.push(this.memory_value);
this.results_value = this.engine.exec(this.memory_value);
this.add_to_history();
this.refresh();
},

put: function(value) {
this.memory_value += value;
this.update_memory();
},

reset: function() {
this.memory_value = '';
this.results_value = '0';
this.clear_history();
this.refresh();
},

refresh: function() {
this.update_result();
this.update_memory();
},

update_result: function() {
document.getElementById(this.results_id).innerHTML = this.results_value;
},

update_memory: function() {
document.getElementById(this.memory_id).innerHTML = this.memory_value;
},

add_to_history: function() {
if (isNaN(this.results_value) == false) {
var div = document.createElement('li');
div.innerHTML = this.memory_value + ' = ' + this.results_value;

var tag = document.getElementById(this.history_id);
tag.insertBefore(div, tag.firstChild);
}
},

clear_history: function(){
$('#'+this.history_id+ '> li').remove();
},

engine: {
exec: function(value) {
try {return eval(this.parse(value))}
catch (e) {return e}
},

parse: function(value) {
if (value != null && value != '') {
value = this.replaceFun(value, Calculator.PROC, '/100');
value = this.replaceFun(value, Calculator.MOD, '%');
value = this.addSequence(value, Calculator.PROC);

value = this.replaceFun(value, 'sin', 'Math.sin');
value = this.replaceFun(value, 'cos', 'Math.cos');
return value;
}
else return '0';
},

replaceFun: function(txt, reg, fun) {
return txt.replace(new RegExp(reg, 'g'), fun);
},

addSequence: function(txt, fun) {
var list = txt.split(fun);
var line = '';

for(var nr in list) {
if (line != '') {
line = '(' + line + ')' + fun + '(' + list[nr] + ')';
} else {
line = list[nr];
}
}
return line;
}
}
}


$(document).keypress(function(e) {
var element = $('*[data-key="'+e.which+'"]');

var fun = function(element){
// skip if this is no a functional button
if (element.length == 0){ return true }

if (element.data('constant') != undefined){
return Calculator.put(Calculator[element.data('constant')]);
}

if (element.data('method') != undefined){
return Calculator[element.data('method')]();
}

return Calculator.put(element.html());
}

if (fun(element) != false){
return false
} else {
return true
}
});

$(document).ready(function() {

$(".btn").click(function(e) {
e.preventDefault();

if ($(this).data('constant') != undefined){
return Calculator.put(Calculator[$(this).data('constant')]);
}

if ($(this).data('method') != undefined){
return Calculator[$(this).data('method')]();
}

return Calculator.put($(this).html());
});
});

Large diffs are not rendered by default.

Loading

0 comments on commit 109ad62

Please sign in to comment.