package main

import (
	//"fmt"
	"flag"
	"http"
	"io"
	"os"
)

var port *string = flag.String("p", "8080", "port number")

func put(r *http.Request) {
	f, _ := os.Open("." + r.URL.Path,
		os.O_CREAT|os.O_TRUNC|os.O_WRONLY, 0644)
	io.Copy(f, r.Body)
}

func get(c http.ResponseWriter, r *http.Request) {
	f, _ := os.Open("." + r.URL.Path, os.O_RDONLY, 0)
	io.Copy(c, f)
}

func handler(c http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		get(c, r)
	case "PUT":
		put(r)
	}
}

func main() {
	flag.Parse()
	http.HandleFunc("/", handler)
	println("listening on port :" + *port)
	http.ListenAndServe(":" + *port, nil)
}
