Luis Figueiredo 8 yıl önce
işleme
9498f4606a

+ 99 - 0
assets/strapdown.css

@@ -0,0 +1,99 @@
+
+/*******************************************************************
+
+This chunk is to fix Bootstrap so that the Markdown output looks good
+
+*******************************************************************/
+
+body {
+  padding-top: 60px;
+  padding-bottom: 40px;
+  font-size: 15px;
+  line-height: 150%;
+}
+
+xmp, textarea {
+  display: none;
+}
+
+h1,h2,h3,h4 {
+  margin: 15px 0;
+}
+
+pre {
+  margin: 20px 0;
+}
+
+img {
+  margin: 10px 0;
+}
+
+.navbar {
+  z-index: 1;
+}
+
+.table {
+  width: auto;
+}
+
+/*******************************************************************
+
+This chunk is for Google's Code Prettify:
+http://google-code-prettify.googlecode.com
+
+*******************************************************************/
+
+
+/* Pretty printing styles. Used with prettify.js. */
+
+/* SPAN elements with the classes below are added by prettyprint. */
+.pln { color: #000 }  /* plain text */
+
+@media screen {
+  .str { color: #080 }  /* string content */
+  .kwd { color: #008 }  /* a keyword */
+  .com { color: #800 }  /* a comment */
+  .typ { color: #606 }  /* a type name */
+  .lit { color: #066 }  /* a literal value */
+  /* punctuation, lisp open bracket, lisp close bracket */
+  .pun, .opn, .clo { color: #660 }
+  .tag { color: #008 }  /* a markup tag name */
+  .atn { color: #606 }  /* a markup attribute name */
+  .atv { color: #080 }  /* a markup attribute value */
+  .dec, .var { color: #606 }  /* a declaration; a variable name */
+  .fun { color: red }  /* a function name */
+}
+
+/* Use higher contrast and text-weight for printable form. */
+@media print, projection {
+  .str { color: #060 }
+  .kwd { color: #006; font-weight: bold }
+  .com { color: #600; font-style: italic }
+  .typ { color: #404; font-weight: bold }
+  .lit { color: #044 }
+  .pun, .opn, .clo { color: #440 }
+  .tag { color: #006; font-weight: bold }
+  .atn { color: #404 }
+  .atv { color: #060 }
+}
+
+/* Put a border around prettyprinted code snippets. */
+pre.prettyprint { padding: 2px; border: 1px solid #888 }
+
+/* Specify class=linenums on a pre to get line numbering */
+ol.linenums { margin-top: 0; margin-bottom: 0 } /* IE indents via margin-left */
+li.L0,
+li.L1,
+li.L2,
+li.L3,
+li.L5,
+li.L6,
+li.L7,
+li.L8 { list-style-type: none }
+/* Alternate shading for lines */
+li.L1,
+li.L3,
+li.L5,
+li.L7,
+li.L9 { background: #eee }
+

Dosya farkı çok büyük olduğundan ihmal edildi
+ 442 - 0
assets/strapdown.js


Dosya farkı çok büyük olduğundan ihmal edildi
+ 9 - 0
assets/themes/bootstrap-responsive.min.css


Dosya farkı çok büyük olduğundan ihmal edildi
+ 681 - 0
assets/themes/github.min.css


Dosya farkı çok büyük olduğundan ihmal edildi
+ 11 - 0
assets/themes/paper.min.css


Dosya farkı çok büyük olduğundan ihmal edildi
+ 845 - 0
assets/themes/united.min.css


Dosya farkı çok büyük olduğundan ihmal edildi
+ 31 - 0
binAssets/binAssets.go


+ 25 - 0
example/README.md

@@ -0,0 +1,25 @@
+MARKDOWN TEST
+==========================
+
+Testing markdown
+```go
+package main
+
+import "fmt"
+
+func main() {
+	fmt.Println("Hello world")
+}
+```
+
+### Test table
+
+id | name
+---|-----
+0  | admin
+1  | user
+
+Enumeration  
+
+* one thing
+* two things

BIN
example/httpServe


+ 9 - 0
example/other.html

@@ -0,0 +1,9 @@
+<html>
+	<head>
+		<title>httpServe test page</title>
+		<link rel="stylesheet" href="style.css">
+	</head>
+	<body>
+		Out of the box folder server
+	</body>
+</html>

+ 3 - 0
example/style.css

@@ -0,0 +1,3 @@
+body {
+	font-family: sans-serif;
+}

+ 9 - 0
example/test/subfolder/other.html

@@ -0,0 +1,9 @@
+<html>
+	<head>
+		<title>httpServe test page</title>
+		<link rel="stylesheet" href="style.css">
+	</head>
+	<body>
+		Out of the box folder server
+	</body>
+</html>

+ 1 - 0
example/test/test.txt

@@ -0,0 +1 @@
+test

+ 156 - 0
main.go

@@ -0,0 +1,156 @@
+// Simpliest server
+package main
+
+//go:generate folder2go assets binAssets
+
+import (
+	"fmt"
+	"hexasoftware/cmd/httpServe/binAssets"
+	_ "hexasoftware/lib/prettylog/global"
+	"io"
+	"io/ioutil"
+	"log"
+	"mime"
+	"net"
+	"net/http"
+	"os"
+	"path/filepath"
+	"strings"
+)
+
+func CreateHandleFunc(prefix string) func(http.ResponseWriter, *http.Request) {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var solvedPath = r.URL.Path
+		if solvedPath == prefix {
+			solvedPath = prefix + "/index.html"
+		}
+		log.Printf("%s - (embed)%s", r.Method, r.URL.Path)
+		if strings.HasPrefix(solvedPath, prefix) {
+			solvedPath = solvedPath[len(prefix):]
+		}
+		data, ok := binAssets.Data[solvedPath]
+		if !ok {
+			w.WriteHeader(404)
+		}
+		w.Header().Set("Content-type", mime.TypeByExtension(filepath.Ext(solvedPath)))
+		w.Write(data)
+	}
+}
+
+func HandleMarkDown(w http.ResponseWriter, r *http.Request, path string) error {
+	log.Println("Handling markdown")
+	f, err := os.Open(path)
+	if err != nil {
+		return err
+	}
+	defer f.Close()
+	/* // Server side markdown2html
+	data, err := ioutil.ReadAll(f)
+	if err != nil {
+		return err
+	}
+	w.Write([]byte("<html><head><link rel='stylesheet' href='/.httpServe/strapdown.css'><link rel='stylesheet' href='/.httpServe/themes/united.min.css'></head><body>"))
+	mdData := blackfriday.MarkdownCommon(data)
+	w.Write(mdData)
+	w.Write([]byte("</body></html>"))
+	return nil
+	*/
+	w.Write([]byte(
+		`
+<!DOCTYPE html>
+<html>
+<xmp theme="paper" style="display:none;">
+`))
+
+	io.Copy(w, f)
+	w.Write([]byte(
+		`
+			</xmp>
+	<script src=".httpServe/strapdown.js"></script>
+</html>`))
+	return nil
+}
+
+func HandleFolder(w http.ResponseWriter, r *http.Request, path string) error {
+
+	res, err := ioutil.ReadDir(path)
+	if err != nil {
+		return err
+	}
+	w.Write([]byte(
+		`<html>
+ <body>
+ <ul>`))
+	for _, f := range res {
+		w.Write([]byte(fmt.Sprintf(`<li><a href="/%s">%s</a>`, path+"/"+f.Name(), f.Name())))
+	}
+	w.Write([]byte(
+		`</ul>
+ </body>
+ </html>
+ `))
+
+	return nil
+}
+
+type FileServer struct {
+}
+
+func (FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	path := r.URL.Path[1:]
+	if path == "" {
+		path = "index.html"
+		if _, err := os.Stat(path); os.IsNotExist(err) {
+			path = "."
+		}
+	}
+
+	if strings.Contains(path, "..") {
+		http.ServeFile(w, r, path)
+	}
+	log.Printf("%s - %s", r.Method, r.URL.Path)
+
+	fstat, err := os.Stat(path)
+	if err != nil {
+		log.Println("ERR:", err)
+		http.ServeFile(w, r, path)
+		return
+	}
+	if fstat.IsDir() {
+		HandleFolder(w, r, path)
+		return
+	}
+
+	if filepath.Ext(path) == ".md" {
+		err := HandleMarkDown(w, r, path)
+		if err != nil {
+			http.ServeFile(w, r, path)
+		}
+		return
+	}
+
+	// Default file server
+	http.ServeFile(w, r, path)
+}
+
+func main() {
+
+	mux := http.NewServeMux()
+	mux.HandleFunc("/.httpServe/", CreateHandleFunc("/.httpServe"))
+	mux.Handle("/", FileServer{})
+	var port = 8080
+	for {
+		addr := fmt.Sprintf(":%d", port)
+		listener, err := net.Listen("tcp", addr)
+		if err != nil {
+			log.Println("Err opening", port, err)
+			port++
+			log.Println("Trying port", port)
+			continue
+		}
+		log.Println("Listening with port:", port)
+
+		http.Serve(listener, mux)
+	}
+	//http.ListenAndServe(":8080", http.FileServer(http.Dir('.')))
+}