config options reorganised. More robust config parser.

master
KatolaZ 8 years ago
parent e29d79e5f9
commit cd95aa3dc7
  1. 22
      README.md
  2. 9
      binit.cfg
  3. 27
      config.go
  4. 17
      main.go

@ -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 That's just it. Preliminary version of a minimal, no-fuss
pastebin-like service in golang. pastebin-like service in golang.
Needs a folder "pastes/" to exist on the same dir where the program is It serves pastes in the format:
run from. At the moment, it binds on `localhost:8000` and serves
pastes in the format:
localhost:8000/abcdef1234567890 mypasteserver.org/abcdef1234567890
The unique ID of a paste is obtained from the SHA256 of the and stores them in a folder, one file per paste, whose filename is
concatenation of title, time, and content. Rendering is minimal, but equal to the paste ID. The unique ID of a paste is obtained from the
can be enhanced. 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 `binit` is currently configured through a simple key=value
configuration file. The available options are: configuration file. The available options are:
* host (the hostname to listen on) * server\_name (the FQDN where the service is reachable from outside)
* port (the port to bind) * bind\_addr (the address to listen on)
* bind\_port (the port to bind)
* paste\_dir (the folder where pastes are kept) * paste\_dir (the folder where pastes are kept)
* templ\_dir (the folder where HTML files and templates 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 * max_size (the maximum allowed length of a paste, in bytes. Larger
@ -26,4 +26,4 @@ configuration file. The available options are:
### TODO ### TODO
* Add a simple template system * reorganise the code for paste storage/retrieve

@ -2,11 +2,14 @@
## These are comments ## These are comments
## ##
## host/IP ## Server name
host=localhost server_name=localhost:8080
## Bind address
bind_addr = 127.0.0.1
## Port number ## Port number
port=8080 bind_port=8080
## Directory where all pastes are kept ## Directory where all pastes are kept
paste_dir=./pastes paste_dir=./pastes

@ -12,8 +12,9 @@ import (
type Config struct { type Config struct {
host string server_name string
port string bind_addr string
bind_port string
paste_dir string paste_dir string
templ_dir string templ_dir string
log_fname string log_fname string
@ -25,8 +26,8 @@ func (c Config) String() string {
var s string var s string
s+= "Host: " + c.host + "\n" s+= "Server name: " + c.server_name + "\n"
s+= "Port: " + c.port + "\n" s+= "Listening on: " + c.bind_addr + ":" + c.bind_port +"\n"
s+= "paste_dir: " + c.paste_dir + "\n" s+= "paste_dir: " + c.paste_dir + "\n"
s+= "templ_dir: " + c.templ_dir + "\n" s+= "templ_dir: " + c.templ_dir + "\n"
s+= "log_fname: " + c.log_fname + "\n" s+= "log_fname: " + c.log_fname + "\n"
@ -57,17 +58,19 @@ func parse_config (fname string, c *Config) error {
if matched, _ := regexp.MatchString("^([a-z_ ]+)=.*", s); matched == true { if matched, _ := regexp.MatchString("^([a-z_ ]+)=.*", s); matched == true {
// and contains an assignment // and contains an assignment
fields := strings.Split(s, "=") fields := strings.Split(s, "=")
switch fields[0]{ switch strings.Trim(fields[0], " \t\""){
case "host": case "server_name":
c.host = fields[1] c.server_name = strings.Trim(fields[1], " \t\"")
case "port": case "bind_addr":
c.port = fields[1] c.bind_addr = strings.Trim(fields[1], " \t\"")
case "bind_port":
c.bind_port = strings.Trim(fields[1], " \t\"")
case "paste_dir": case "paste_dir":
c.paste_dir = fields[1] c.paste_dir = strings.Trim(fields[1], " \t\"")
case "templ_dir": case "templ_dir":
c.templ_dir = fields[1] c.templ_dir = strings.Trim(fields[1], " \t\"")
case "log_fname": case "log_fname":
c.log_fname = fields[1] c.log_fname = strings.Trim(fields[1], " \t\"")
case "max_size": case "max_size":
if m_size, err := strconv.ParseUint(fields[1], 10, 16); err == nil { if m_size, err := strconv.ParseUint(fields[1], 10, 16); err == nil {
c.max_size = uint16(m_size) c.max_size = uint16(m_size)

@ -14,8 +14,9 @@ import (
var p_conf = Config{ var p_conf = Config{
host: "localhost", server_name: "localhost",
port: "8000", bind_addr: "0.0.0.0",
bind_port: "8000",
paste_dir: "./pastes", paste_dir: "./pastes",
templ_dir: "./tmpl", templ_dir: "./tmpl",
log_fname: "./binit.log", 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 { if err := ioutil.WriteFile(paste_dir+ paste_name, []byte(content), 0644); err == nil {
// and then we return the URL: // and then we return the URL:
log.Printf(" `-- saving paste to : %s", paste_dir + paste_name) 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" { if show := req_body.Get("show"); show != "1" {
fmt.Fprintf(w, "%s/%s", hostname, paste_name) fmt.Fprintf(w, "%s/%s", hostname, paste_name)
return return
} else{ } else{
fmt.Fprintf(w, "<html><body>Link: <a href='%s'>%s</a></body></html>", fmt.Fprintf(w, "<html><body>Link: <a href='http://%s/%s'>http://%s/%s</a></body></html>",
paste_hash[i:i+16], paste_hash[i:i+16]) hostname, paste_hash[i:i+16], hostname, paste_hash[i:i+16])
return return
} }
} else { } else {
@ -158,12 +160,13 @@ func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds) log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
log.Println("Binit version 0.1 -- Starting ") 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(" + paste_dir: %s\n", p_conf.paste_dir)
log.Printf(" + templ_dir: %s\n", p_conf.templ_dir) log.Printf(" + templ_dir: %s\n", p_conf.templ_dir)
log.Printf(" + max_size: %d\n", p_conf.max_size) log.Printf(" + max_size: %d\n", p_conf.max_size)
http.HandleFunc("/", req_handler) 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))
} }

Loading…
Cancel
Save