Added max_size in the configuration file

master
KatolaZ 8 years ago
parent cfc67a8246
commit ee3dc59322
  1. 13
      README.md
  2. 10
      binit.cfg
  3. 13
      config.go
  4. 14
      main.go

@ -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

@ -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

@ -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])

@ -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)

Loading…
Cancel
Save