Skip to content

Commit

Permalink
Add initial web app
Browse files Browse the repository at this point in the history
  • Loading branch information
rsierra committed Jan 25, 2020
0 parents commit 8ff8c3f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
66 changes: 66 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Serve is a very simple static file server in go
Usage:
-p="8100": port to serve on
-d=".": the directory of static files to host
Navigating to http://localhost:8100 will display the index.html or directory
listing file.
*/
package main

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

var port = flag.String("p", "8100", "port to serve on")
var directory = flag.String("d", ".", "the directory of files to host")

func main() {
flag.Parse()

fs := http.FileServer(http.Dir(*directory))
http.Handle("/files/", http.StripPrefix("/files/", fs))

ss := http.FileServer(http.Dir("statics"))
http.Handle("/statics/", http.StripPrefix("/statics/", ss))

http.HandleFunc("/", serveTemplate)

log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}

func serveTemplate(w http.ResponseWriter, r *http.Request) {
var template_name string
if template_name = filepath.Clean(r.URL.Path); template_name == "/" {
template_name = "/index.html"
}

lp := filepath.Join("templates", "layout.html")
fp := filepath.Join("templates", template_name)

files, err := filePathWalkDir(*directory)
if err != nil {
log.Fatal(err)
}

log.Printf("Template %s\n", template_name)
tmpl, _ := template.ParseFiles(lp, fp)
tmpl.ExecuteTemplate(w, "layout", files)
}

func filePathWalkDir(root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
return files, err
}
12 changes: 12 additions & 0 deletions statics/css/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
body, html{
height: 100%;
}
#qr-code{
position: absolute;
background: white;
padding: 10px;
border: 1px solid #ddd;
}
#qr-code canvas{
display: block;
}
28 changes: 28 additions & 0 deletions statics/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$(document).ready(function () {
var $view = $("<div id='qr-code'></div>");
$("body").append($view);
$view.hide();
});

$("body").click(function (e) {
var QRVisibe = $(e.target).is(".qr-link")

// if there is click event outside IMG then close the qr-view box
if (!QRVisibe)
$('#qr-code').fadeOut();
});

$("a.qr-link").click(function (e) {
// prevent default click behaviour
e.preventDefault();

// get full link url
var href = $(this).prop('href');

// set its location and do show
$("#qr-code").css("top", (e.pageY) + "px").css("left", (e.pageX) + "px");
$("#qr-code").fadeIn();

// set qr-code content
$("#qr-code").empty().qrcode(href);
});
10 changes: 10 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{define "title"}}Files list{{end}}

{{define "body"}}
<h1>Files list</h1>
<ul>
{{range .}}
<li><a href="/{{.}}" class="qr-link">{{.}}</a></li>
{{end}}
</ul>
{{end}}
22 changes: 22 additions & 0 deletions templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{define "layout"}}
<!doctype html>
<html>

<head>
<link rel="stylesheet" type="text/css" href="statics/css/app.css">
<link
href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP8AAAD/AAAA/wAAAP8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/wAAAP/ExMT/xMTE/8TExP/ExMT/AAAA/wAAAP8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////////////////////////////xMTE/8TExP/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8">
<title>{{template "title"}}</title>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.qrcode@1.0.3/jquery.qrcode.min.js"></script>
</head>

<body>
{{template "body" .}}
<script type='text/javascript' src="statics/js/app.js"></script>
</body>

</html>
{{end}}

0 comments on commit 8ff8c3f

Please sign in to comment.