2015-09-24 01:44:49 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2015-09-25 22:03:14 -04:00
|
|
|
"os"
|
|
|
|
"fmt"
|
2015-09-24 01:44:49 -04:00
|
|
|
|
2015-09-24 23:21:37 -04:00
|
|
|
"github.com/flosch/pongo2"
|
2015-09-24 01:44:49 -04:00
|
|
|
"github.com/zenazn/goji"
|
2015-09-25 12:00:14 -04:00
|
|
|
"github.com/zenazn/goji/web/middleware"
|
2015-09-24 01:44:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var Config struct {
|
|
|
|
bind string
|
|
|
|
filesDir string
|
2015-09-25 12:00:14 -04:00
|
|
|
noLogs bool
|
2015-09-24 01:44:49 -04:00
|
|
|
siteName string
|
2015-09-25 00:58:38 -04:00
|
|
|
siteURL string
|
2015-09-24 01:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
|
|
|
|
"host to bind to (default: 127.0.0.1:8080)")
|
2015-09-25 00:58:38 -04:00
|
|
|
flag.StringVar(&Config.filesDir, "filespath", "files/",
|
2015-09-25 12:47:55 -04:00
|
|
|
"path to files directory (including trailing slash)")
|
2015-09-25 12:00:14 -04:00
|
|
|
flag.BoolVar(&Config.noLogs, "nologs", false,
|
|
|
|
"remove stdout output for each request")
|
2015-09-25 00:58:38 -04:00
|
|
|
flag.StringVar(&Config.siteName, "sitename", "linx",
|
2015-09-24 01:44:49 -04:00
|
|
|
"name of the site")
|
2015-09-25 00:58:38 -04:00
|
|
|
flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
|
|
|
|
"site base url (including trailing slash)")
|
2015-09-24 01:44:49 -04:00
|
|
|
flag.Parse()
|
|
|
|
|
2015-09-25 12:00:14 -04:00
|
|
|
if Config.noLogs {
|
|
|
|
goji.Abandon(middleware.Logger)
|
|
|
|
}
|
2015-09-24 23:21:37 -04:00
|
|
|
|
2015-09-25 22:03:14 -04:00
|
|
|
// make directory if needed
|
|
|
|
err := os.MkdirAll(Config.filesDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: could not create files directory")
|
2015-09-26 07:50:33 -04:00
|
|
|
os.Exit(1)
|
2015-09-25 22:03:14 -04:00
|
|
|
}
|
2015-09-25 12:47:55 -04:00
|
|
|
|
2015-09-24 23:21:37 -04:00
|
|
|
// Template Globals
|
|
|
|
pongo2.DefaultSet.Globals["sitename"] = Config.siteName
|
|
|
|
|
|
|
|
// Routing setup
|
2015-09-24 01:44:49 -04:00
|
|
|
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
|
|
|
|
selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
|
|
|
|
|
|
|
|
goji.Get("/", indexHandler)
|
2015-09-25 12:00:14 -04:00
|
|
|
|
2015-09-24 01:44:49 -04:00
|
|
|
goji.Post("/upload", uploadPostHandler)
|
2015-09-25 12:00:14 -04:00
|
|
|
goji.Post("/upload/", http.RedirectHandler("/upload", 301))
|
2015-09-24 01:44:49 -04:00
|
|
|
goji.Put("/upload", uploadPutHandler)
|
2015-09-25 12:00:14 -04:00
|
|
|
goji.Put("/upload/:name", uploadPutHandler)
|
|
|
|
|
2015-09-24 16:04:51 -04:00
|
|
|
goji.Get("/static/*", http.StripPrefix("/static/",
|
|
|
|
http.FileServer(http.Dir("static/"))))
|
2015-09-24 01:44:49 -04:00
|
|
|
goji.Get(nameRe, fileDisplayHandler)
|
2015-09-24 16:04:51 -04:00
|
|
|
goji.Get(selifRe, fileServeHandler)
|
2015-09-25 12:00:14 -04:00
|
|
|
goji.NotFound(notFoundHandler)
|
2015-09-24 01:44:49 -04:00
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", Config.bind)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not bind: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
goji.ServeListener(listener)
|
|
|
|
}
|