From cd95aa3dc71bec3d15541424f328b6f3735432f1 Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Mon, 3 Jul 2017 10:40:48 +0100 Subject: config options reorganised. More robust config parser. --- README.md | 22 +++++++++++----------- binit.cfg | 9 ++++++--- config.go | 29 ++++++++++++++++------------- main.go | 17 ++++++++++------- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index dceb7e4..8a61d89 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,23 @@ -## binit -- minimal pastebin-like in 100 lines of golang +## binit -- minimal pastebin-like in golang That's just it. Preliminary version of a minimal, no-fuss pastebin-like service in golang. -Needs a folder "pastes/" to exist on the same dir where the program is -run from. At the moment, it binds on `localhost:8000` and serves -pastes in the format: +It serves pastes in the format: - localhost:8000/abcdef1234567890 + mypasteserver.org/abcdef1234567890 -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. +and stores them in a folder, one file per paste, whose filename is +equal to the paste ID. 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) +* server\_name (the FQDN where the service is reachable from outside) +* bind\_addr (the address to listen on) +* bind\_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 @@ -26,4 +26,4 @@ configuration file. The available options are: ### TODO -* Add a simple template system +* reorganise the code for paste storage/retrieve diff --git a/binit.cfg b/binit.cfg index 2758441..0f66b25 100644 --- a/binit.cfg +++ b/binit.cfg @@ -2,11 +2,14 @@ ## These are comments ## -## host/IP -host=localhost +## Server name +server_name=localhost:8080 + +## Bind address +bind_addr = 127.0.0.1 ## Port number -port=8080 +bind_port=8080 ## Directory where all pastes are kept paste_dir=./pastes diff --git a/config.go b/config.go index afbaaf0..ad5b4cb 100644 --- a/config.go +++ b/config.go @@ -12,8 +12,9 @@ import ( type Config struct { - host string - port string + server_name string + bind_addr string + bind_port string paste_dir string templ_dir string log_fname string @@ -25,8 +26,8 @@ func (c Config) String() string { var s string - s+= "Host: " + c.host + "\n" - s+= "Port: " + c.port + "\n" + s+= "Server name: " + c.server_name + "\n" + s+= "Listening on: " + c.bind_addr + ":" + c.bind_port +"\n" s+= "paste_dir: " + c.paste_dir + "\n" s+= "templ_dir: " + c.templ_dir + "\n" s+= "log_fname: " + c.log_fname + "\n" @@ -54,20 +55,22 @@ func parse_config (fname string, c *Config) error { // it's not a blank line if matched, _ := regexp.MatchString("^#", s); matched != true { // This is not a comment... - if matched, _ := regexp.MatchString("^([a-z_]+)=.*", s); matched == true { + if matched, _ := regexp.MatchString("^([a-z_ ]+)=.*", s); matched == true { // and contains an assignment fields := strings.Split(s, "=") - switch fields[0]{ - case "host": - c.host = fields[1] - case "port": - c.port = fields[1] + switch strings.Trim(fields[0], " \t\""){ + case "server_name": + c.server_name = strings.Trim(fields[1], " \t\"") + case "bind_addr": + c.bind_addr = strings.Trim(fields[1], " \t\"") + case "bind_port": + c.bind_port = strings.Trim(fields[1], " \t\"") case "paste_dir": - c.paste_dir = fields[1] + c.paste_dir = strings.Trim(fields[1], " \t\"") case "templ_dir": - c.templ_dir = fields[1] + c.templ_dir = strings.Trim(fields[1], " \t\"") case "log_fname": - c.log_fname = fields[1] + c.log_fname = strings.Trim(fields[1], " \t\"") case "max_size": if m_size, err := strconv.ParseUint(fields[1], 10, 16); err == nil { c.max_size = uint16(m_size) diff --git a/main.go b/main.go index 76bc4a0..6c3c151 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,9 @@ import ( var p_conf = Config{ - host: "localhost", - port: "8000", + server_name: "localhost", + bind_addr: "0.0.0.0", + bind_port: "8000", paste_dir: "./pastes", templ_dir: "./tmpl", log_fname: "./binit.log", @@ -108,13 +109,14 @@ func handle_put_paste(w http.ResponseWriter, r *http.Request) { if err := ioutil.WriteFile(paste_dir+ paste_name, []byte(content), 0644); err == nil { // and then we return the URL: log.Printf(" `-- saving paste to : %s", paste_dir + paste_name) - hostname := r.Host + //hostname := r.Host + hostname := p_conf.server_name if show := req_body.Get("show"); show != "1" { fmt.Fprintf(w, "%s/%s", hostname, paste_name) return } else{ - fmt.Fprintf(w, "Link: %s", - paste_hash[i:i+16], paste_hash[i:i+16]) + fmt.Fprintf(w, "Link: http://%s/%s", + hostname, paste_hash[i:i+16], hostname, paste_hash[i:i+16]) return } } else { @@ -158,12 +160,13 @@ func main() { log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds) log.Println("Binit version 0.1 -- Starting ") - log.Printf(" + listening on: %s:%s\n", p_conf.host, p_conf.port ) + log.Printf(" + Serving pastes on: %s\n", p_conf.server_name) + log.Printf(" + listening on: %s:%s\n", p_conf.bind_addr, p_conf.bind_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) - log.Fatal(http.ListenAndServe(p_conf.host + ":" + p_conf.port, nil)) + log.Fatal(http.ListenAndServe(p_conf.bind_addr + ":" + p_conf.bind_port, nil)) } -- cgit v1.2.3