Beispiel #1
0
func Example() {
	src := []byte(`
/* hello, world! */
var a = 3;

// b is a cool function
function b() {
  return 7;
}`)

	highlighted, err := syntaxhighlight.AsHTML(src)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println(string(highlighted))

	// Output:
	// <span class="com">/* hello, world! */</span>
	// <span class="kwd">var</span> <span class="pln">a</span> <span class="pun">=</span> <span class="dec">3</span><span class="pun">;</span>
	//
	// <span class="com">// b is a cool function</span>
	// <span class="kwd">function</span> <span class="pln">b</span><span class="pun">(</span><span class="pun">)</span> <span class="pun">{</span>
	//   <span class="kwd">return</span> <span class="dec">7</span><span class="pun">;</span>
	// <span class="pun">}</span>
}
Beispiel #2
0
func main() {
	flag.Parse()

	log.SetFlags(0)

	if flag.NArg() != 1 {
		log.Fatal("Must specify exactly 1 filename argument.")
	}

	input, err := ioutil.ReadFile(flag.Arg(0))
	if err != nil {
		log.Fatal(err)
	}

	html, err := syntaxhighlight.AsHTML(input)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", html)
}
Beispiel #3
0
func main() {
	// create the storage directory
	if err := os.MkdirAll(storage, 0755); err != nil {
		log.Fatalf("creating storage directory %q failed: %v", storage, err)
	}

	// create mux server
	mux := http.NewServeMux()

	// static files
	staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("/src/static")))
	mux.Handle("/static/", staticHandler)

	// upload function
	mux.HandleFunc("/paste", func(w http.ResponseWriter, r *http.Request) {
		// make them auth
		allowed := auth(r)
		if !allowed {
			w.Header().Set("WWW-Authenticate", `Basic realm="`+baseuri+`"`)
			w.WriteHeader(401)
			w.Write([]byte("401 Unauthorized\n"))
			return
		}

		w.Header().Set("Content-Type", "application/json")
		if r.Method != "POST" {
			writeError(w, "not a valid endpoint")
			return
		}

		content, err := ioutil.ReadAll(r.Body)
		if err != nil {
			writeError(w, fmt.Sprintf("reading from body failed: %v", err))
			return
		}

		// create a unique id for the paste
		id, err := uuid()
		if err != nil {
			writeError(w, fmt.Sprintf("uuid generation failed: %v", err))
			return
		}

		// write to file
		filepath := path.Join(storage, id)
		if err = ioutil.WriteFile(filepath, content, 0755); err != nil {
			writeError(w, fmt.Sprintf("writing file to %q failed: %v", filepath, err))
			return
		}

		fmt.Fprint(w, JSONResponse{
			"uri": baseuri + id,
		})
		log.Printf("paste posted successfully")
		return
	})

	// index function
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		if r.URL.Path == "/" {
			// they want the root
			// if probably me the admin
			// but make them auth
			allowed := auth(r)
			if !allowed {
				w.Header().Set("WWW-Authenticate", `Basic realm="`+baseuri+`"`)
				w.WriteHeader(401)
				w.Write([]byte("401 Unauthorized\n"))
				return
			}

			var files string
			err := filepath.Walk(storage, func(path string, f os.FileInfo, err error) error {
				if filepath.Base(path) != storage {
					files += fmt.Sprintf(`<tr>
<td><a href="%s%s">%s</a></td>
<td>%s</td>
<td>%d</td>
</tr>`, baseuri, filepath.Base(path), filepath.Base(path), f.ModTime().Format("2006-01-02T15:04:05Z07:00"), f.Size())
				}
				return nil
			})
			if err != nil {
				writeError(w, fmt.Sprintf("walking %s failed: %v", storage, err))
			}

			fmt.Fprintf(w, "%s<table><thead><tr><th>name</th><th>modified</th><th>size</th></tr></thead><tbody>%s</tbody></table>%s", htmlBegin, files, htmlEnd)
			log.Printf("index file rendered")
			return
		}

		// check if a file exists for what they are asking
		serveRaw := false
		filename := strings.Trim(r.URL.Path, "/")
		filename = path.Join(storage, filename)

		// check if they want the raw file
		if strings.HasSuffix(filename, "/raw") {
			w.Header().Set("Content-Type", "text/plain")
			filename = strings.TrimSuffix(filename, "/raw")
			serveRaw = true
		}

		// check if file exists
		if _, err := os.Stat(filename); os.IsNotExist(err) {
			writeError(w, fmt.Sprintf("No such file or directory: %q", filename))
			return
		}

		// read the file
		src, err := ioutil.ReadFile(filename)
		if err != nil {
			writeError(w, fmt.Sprintf("Reading file %q failed: %v", filename, err))
			return
		}

		if serveRaw {
			// serve the file
			w.Write(src)
			log.Printf("raw paste served: %s", src)
			return
		}

		// try to syntax highlight the file
		highlighted, err := syntaxhighlight.AsHTML(src)
		if err != nil {
			writeError(w, fmt.Sprintf("Highlighting file %q failed: %v", filename, err))
			return
		}

		fmt.Fprintf(w, "%s<pre><code>%s</code></pre>%s", htmlBegin, string(highlighted), htmlEnd)
		log.Printf("highlighted paste served: %s", filename)
		return
	})

	// set up the server
	server := &http.Server{
		Addr:    ":" + port,
		Handler: mux,
	}
	log.Printf("Starting server on port %q with baseuri %q", port, baseuri)
	if certFile != "" && keyFile != "" {
		log.Fatal(server.ListenAndServeTLS(certFile, keyFile))
	} else {
		log.Fatal(server.ListenAndServe())
	}
}