summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2017-06-30 23:15:36 +0100
committerKatolaZ <katolaz@freaknet.org>2017-06-30 23:15:36 +0100
commitee3dc59322eb3a366f3594261491381db5084110 (patch)
treefa3a98639cf8c38f80c42fe00f1f8abb0562d200
parentcfc67a824600be8d522c23c108d6333fca6b0359 (diff)
Added max_size in the configuration file
-rw-r--r--README.md13
-rw-r--r--binit.cfg10
-rw-r--r--config.go13
-rw-r--r--main.go14
4 files changed, 46 insertions, 4 deletions
diff --git a/README.md b/README.md
index b3868b7..dceb7e4 100644
--- a/README.md
+++ b/README.md
@@ -13,8 +13,17 @@ The unique ID of a paste is obtained from the SHA256 of the
concatenation of title, time, and content. Rendering is minimal, but
can be enhanced.
+`binit` is currently configured through a simple key=value
+configuration file. The available options are:
+
+* host (the hostname to listen on)
+* port (the port to bind)
+* paste\_dir (the folder where pastes are kept)
+* templ\_dir (the folder where HTML files and templates are kept)
+* max_size (the maximum allowed length of a paste, in bytes. Larger
+ pastes will be trimmed to that length)
+
+
### TODO
-* Check maximum paste length
-* Add a config file (hostname, port, pastedir)
* Add a simple template system
diff --git a/binit.cfg b/binit.cfg
index df17e2d..2758441 100644
--- a/binit.cfg
+++ b/binit.cfg
@@ -2,7 +2,17 @@
## These are comments
##
+## host/IP
host=localhost
+
+## Port number
port=8080
+
+## Directory where all pastes are kept
paste_dir=./pastes
+
+## Directory where HTML files and templates are kept
templ_dir=./html
+
+## max size of a paste, in bytes (cannot exceed 65535)
+max_size=16384
diff --git a/config.go b/config.go
index 729815f..afbaaf0 100644
--- a/config.go
+++ b/config.go
@@ -7,7 +7,7 @@ import (
"bufio"
"regexp"
"strings"
- "log"
+ "strconv"
)
@@ -17,7 +17,7 @@ type Config struct {
paste_dir string
templ_dir string
log_fname string
- logger *log.Logger
+ max_size uint16
}
@@ -29,6 +29,8 @@ func (c Config) String() string {
s+= "Port: " + c.port + "\n"
s+= "paste_dir: " + c.paste_dir + "\n"
s+= "templ_dir: " + c.templ_dir + "\n"
+ s+= "log_fname: " + c.log_fname + "\n"
+ s+= "max_size: " + string(c.max_size) + "\n"
return s
@@ -66,6 +68,13 @@ func parse_config (fname string, c *Config) error {
c.templ_dir = fields[1]
case "log_fname":
c.log_fname = fields[1]
+ case "max_size":
+ if m_size, err := strconv.ParseUint(fields[1], 10, 16); err == nil {
+ c.max_size = uint16(m_size)
+ } else {
+ fmt.Fprintf(os.Stderr, "Invalid max_size value %s at line %d (max: 65535)\n",
+ fields[1], line)
+ }
default:
fmt.Fprintf(os.Stderr, "Error reading config file %s at line %d: unknown variable '%s'\n",
fname, line, fields[0])
diff --git a/main.go b/main.go
index cd50525..aa34a5f 100644
--- a/main.go
+++ b/main.go
@@ -19,10 +19,20 @@ var p_conf = Config{
paste_dir: "./pastes",
templ_dir: "./tmpl",
log_fname: "./binit.log",
+ max_size: 4096,
}
+func min (a, b int) int {
+
+ if a > b {
+ return b
+ } else {
+ return a
+ }
+
+}
func handle_get_paste(w http.ResponseWriter, r *http.Request) {
@@ -70,6 +80,9 @@ func handle_put_paste(w http.ResponseWriter, r *http.Request) {
paste := req_body.Get("paste")
now := time.Now().String()
// format content
+
+ paste = paste[0:min(len(paste), int(p_conf.max_size))]
+
content := fmt.Sprintf("# Title: %s\n# Pasted: %s\n------------\n%s", title, now, paste)
// ccompute the sha256 hash using title, body, and time
@@ -138,6 +151,7 @@ func main() {
log.Printf(" + listening on: %s:%s\n", p_conf.host, p_conf.port )
log.Printf(" + paste_dir: %s\n", p_conf.paste_dir)
log.Printf(" + templ_dir: %s\n", p_conf.templ_dir)
+ log.Printf(" + max_size: %d\n", p_conf.max_size)
http.HandleFunc("/", req_handler)