Last active
January 1, 2016 01:09
-
-
Save Hamcha/8070637 to your computer and use it in GitHub Desktop.
Logsrv ported from Coffeescript (Node.JS) to Go trying to reduce Memory usage - Node.js version: 26.5MiB - Go version: 3.7MiB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"net/http" | |
"html/template" | |
) | |
type ChatLog struct { | |
Content string | |
} | |
type LogList struct { | |
Files []ChatLog | |
} | |
func home(w http.ResponseWriter, r *http.Request) { | |
t, _ := template.ParseFiles("list.html") | |
files, _ := ioutil.ReadDir("logs") | |
filelist := make([]ChatLog, len(files)) | |
for i, f := range files { | |
filelist[i].Content = f.Name() | |
} | |
p := LogList{Files:filelist} | |
t.Execute(w, p) | |
} | |
func viewlog(w http.ResponseWriter, r *http.Request) { | |
t, _ := template.ParseFiles("chat.html") | |
buf, _ := ioutil.ReadFile("logs/"+r.URL.Path[len("/log/"):]) | |
s := string(buf) | |
p := ChatLog{Content:s} | |
t.Execute(w, p) | |
} | |
func main() { | |
http.HandleFunc("/", home) | |
http.HandleFunc("/log/", viewlog) | |
http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static")))) | |
http.ListenAndServe(":8785", nil) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
express = require 'express' | |
fs = require 'fs' | |
app = express() | |
app.use express.static 'static' | |
app.engine 'ejs', require('ejs').__express | |
app.set 'view engine', 'ejs' | |
app.set 'views', __dirname + '/templates' | |
app.set 'view options', { layout:false, root: __dirname + '/templates' } | |
logfolder = "logs" | |
app.get "/log/:logname", (req, res) -> | |
fname = req.params.logname.replace "..","" | |
fs.readFile logfolder+"/"+fname, (err, data) -> | |
return res.end "INVALID FILE" if not data | |
res.render "chat", {messages:data} | |
app.get "/", (req, res) -> | |
fs.readdir logfolder, (err,dirs) -> | |
throw err if err | |
return res.end "Dir \""+logfolder+"\" is null" unless dirs | |
res.render "list", { logs: dirs } | |
app.listen 8785 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment