diff options
| author | KatolaZ <katolaz@freaknet.org> | 2017-07-03 10:40:48 +0100 | 
|---|---|---|
| committer | KatolaZ <katolaz@freaknet.org> | 2017-07-03 10:40:48 +0100 | 
| commit | cd95aa3dc71bec3d15541424f328b6f3735432f1 (patch) | |
| tree | d3235f3fc30809b2cb9a31f6e64c88fc817af184 | |
| parent | e29d79e5f9785d039175da43704d02907b54ab47 (diff) | |
config options reorganised. More robust config parser.
| -rw-r--r-- | README.md | 22 | ||||
| -rw-r--r-- | binit.cfg | 9 | ||||
| -rw-r--r-- | config.go | 29 | ||||
| -rw-r--r-- | main.go | 17 | 
4 files changed, 43 insertions, 34 deletions
| @@ -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 @@ -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 @@ -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) @@ -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, "<html><body>Link: <a href='%s'>%s</a></body></html>", -							paste_hash[i:i+16], paste_hash[i:i+16]) +						fmt.Fprintf(w, "<html><body>Link: <a href='http://%s/%s'>http://%s/%s</a></body></html>", +							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))  } | 
