linx-server/server.go

135 lines
3.3 KiB
Go
Raw Normal View History

2015-09-24 05:44:49 +00:00
package main
import (
"flag"
2015-09-28 02:17:12 +00:00
"fmt"
2015-09-24 05:44:49 +00:00
"log"
"net"
"net/http"
"net/http/fcgi"
2015-09-26 02:03:14 +00:00
"os"
2015-09-28 02:17:12 +00:00
"regexp"
"strconv"
2015-09-30 19:54:30 +00:00
"time"
2015-09-24 05:44:49 +00:00
"github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2"
2015-09-24 05:44:49 +00:00
"github.com/zenazn/goji"
"github.com/zenazn/goji/web/middleware"
2015-09-24 05:44:49 +00:00
)
var Config struct {
2015-09-29 23:28:10 +00:00
bind string
filesDir string
metaDir string
noLogs bool
allowHotlink bool
siteName string
siteURL string
fastcgi bool
2015-09-24 05:44:49 +00:00
}
var Templates = make(map[string]*pongo2.Template)
var TemplateSet *pongo2.TemplateSet
2015-09-30 19:54:30 +00:00
var staticBox *rice.Box
var timeStarted time.Time
var timeStartedStr string
func setup() {
if Config.noLogs {
goji.Abandon(middleware.Logger)
}
2015-09-28 02:17:12 +00:00
// make directories if needed
err := os.MkdirAll(Config.filesDir, 0755)
2015-09-26 02:03:14 +00:00
if err != nil {
fmt.Println("Error: could not create files directory")
2015-09-26 11:50:33 +00:00
os.Exit(1)
2015-09-26 02:03:14 +00:00
}
2015-09-28 02:17:12 +00:00
err = os.MkdirAll(Config.metaDir, 0700)
if err != nil {
fmt.Println("Error: could not create metadata directory")
2015-09-28 02:17:12 +00:00
os.Exit(1)
}
// ensure siteURL ends wth '/'
if lastChar := Config.siteURL[len(Config.siteURL)-1:]; lastChar != "/" {
Config.siteURL = Config.siteURL + "/"
}
// Template setup
2015-09-29 01:58:50 +00:00
p2l, err := NewPongo2TemplatesLoader()
if err != nil {
fmt.Println("Error: could not load templates")
os.Exit(1)
}
TemplateSet := pongo2.NewSet("templates", p2l)
TemplateSet.Globals["sitename"] = Config.siteName
err = populateTemplatesMap(TemplateSet, Templates)
if err != nil {
fmt.Println("Error: could not load templates")
os.Exit(1)
}
2015-09-30 19:54:30 +00:00
staticBox = rice.MustFindBox("static")
timeStarted = time.Now()
timeStartedStr = strconv.FormatInt(timeStarted.Unix(), 10)
2015-09-30 19:54:30 +00:00
// Routing setup
2015-09-24 05:44:49 +00:00
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
selifRe := regexp.MustCompile(`^/selif/(?P<name>[a-z0-9-\.]+)$`)
2015-09-29 03:46:43 +00:00
selifIndexRe := regexp.MustCompile(`^/selif/$`)
torrentRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)/torrent$`)
2015-09-24 05:44:49 +00:00
goji.Get("/", indexHandler)
2015-09-30 04:56:51 +00:00
goji.Get("/paste/", pasteHandler)
goji.Get("/paste", http.RedirectHandler("/paste/", 301))
2015-09-24 05:44:49 +00:00
goji.Post("/upload", uploadPostHandler)
2015-09-28 20:02:03 +00:00
goji.Post("/upload/", uploadPostHandler)
2015-09-24 05:44:49 +00:00
goji.Put("/upload", uploadPutHandler)
goji.Put("/upload/:name", uploadPutHandler)
2015-09-29 03:46:43 +00:00
goji.Delete("/:name", deleteHandler)
2015-09-30 19:54:30 +00:00
goji.Get("/static/*", staticHandler)
2015-09-24 05:44:49 +00:00
goji.Get(nameRe, fileDisplayHandler)
goji.Get(selifRe, fileServeHandler)
2015-09-29 03:46:43 +00:00
goji.Get(selifIndexRe, unauthorizedHandler)
goji.Get(torrentRe, fileTorrentHandler)
goji.NotFound(notFoundHandler)
}
func main() {
flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
"host to bind to (default: 127.0.0.1:8080)")
flag.StringVar(&Config.filesDir, "filespath", "files/",
"path to files directory")
flag.StringVar(&Config.metaDir, "metapath", "meta/",
"path to metadata directory")
flag.BoolVar(&Config.noLogs, "nologs", false,
"remove stdout output for each request")
2015-09-29 23:28:10 +00:00
flag.BoolVar(&Config.allowHotlink, "allowhotlink", false,
"Allow hotlinking of files")
flag.StringVar(&Config.siteName, "sitename", "linx",
"name of the site")
flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/",
"site base url (including trailing slash)")
flag.BoolVar(&Config.fastcgi, "fastcgi", false,
"serve through fastcgi")
flag.Parse()
setup()
2015-09-24 05:44:49 +00:00
listener, err := net.Listen("tcp", Config.bind)
if err != nil {
log.Fatal("Could not bind: ", err)
}
if Config.fastcgi {
fcgi.Serve(listener, goji.DefaultMux)
} else {
goji.ServeListener(listener)
}
2015-09-24 05:44:49 +00:00
}