Host API under /api. Use filepath.Join rather than string concatenation

pull/3149/head
Kegan Dougal 2017-02-06 15:31:53 +00:00
parent d4247e0179
commit db31f0a087
1 changed files with 7 additions and 5 deletions

View File

@ -13,6 +13,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strconv" "strconv"
"time" "time"
) )
@ -36,9 +37,10 @@ func respond(code int, w http.ResponseWriter) {
w.Write([]byte("{}")) w.Write([]byte("{}"))
} }
func gzipAndSave(data []byte, filepath string) error { func gzipAndSave(data []byte, fpath string) error {
filepath = "bugs/" + filepath fpath = filepath.Join("bugs", fpath)
if _, err := os.Stat(filepath); err == nil {
if _, err := os.Stat(fpath); err == nil {
return fmt.Errorf("file already exists") // the user can just retry return fmt.Errorf("file already exists") // the user can just retry
} }
var b bytes.Buffer var b bytes.Buffer
@ -52,14 +54,14 @@ func gzipAndSave(data []byte, filepath string) error {
if err := gz.Close(); err != nil { if err := gz.Close(); err != nil {
return err return err
} }
if err := ioutil.WriteFile(filepath, b.Bytes(), 0644); err != nil { if err := ioutil.WriteFile(fpath, b.Bytes(), 0644); err != nil {
return err return err
} }
return nil return nil
} }
func main() { func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/api/submit", func(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" && req.Method != "OPTIONS" { if req.Method != "POST" && req.Method != "OPTIONS" {
respond(405, w) respond(405, w)
return return