Skip to content

Commit c2796d5

Browse files
committed
post a path will create a document in mongodb
1 parent 58b15df commit c2796d5

File tree

4 files changed

+90
-8
lines changed

4 files changed

+90
-8
lines changed

config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"address": "localhost:9090",
33
"read_timeout": 10,
4-
"write_timeout": 10
4+
"write_timeout": 10,
5+
"db_host": "127.0.0.1:27017",
6+
"db_name": "howtodo_test",
7+
"collection": "images"
58
}

document.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"labix.org/v2/mgo"
7+
"labix.org/v2/mgo/bson"
8+
)
9+
10+
type Document struct {
11+
Id bson.ObjectId `bson:"_id,omitempty"`
12+
Name string `bson:"name"`
13+
Path string `bson:"path"`
14+
ContentType string `bson:"content_type"`
15+
Binary bson.Binary `bson:"binary"`
16+
}
17+
18+
func (d *Document) Collcetion(s *mgo.Session) *mgo.Collection {
19+
return s.DB(Configuration.DBName).C(Configuration.Collection)
20+
}
21+
22+
func (d *Document) Save(s *mgo.Session) error {
23+
coll := d.Collcetion(s)
24+
25+
if !bson.IsObjectIdHex(d.Id.Hex()) {
26+
d.Id = bson.NewObjectId()
27+
}
28+
29+
_, err := coll.Upsert(bson.M{"_id": d.Id}, d)
30+
if err != nil {
31+
log.Fatalln(err)
32+
return err
33+
}
34+
return nil
35+
}

handler.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io"
55
"log"
66
"net/http"
7+
"strings"
78
)
89

910
type ImgHandler struct {
@@ -24,5 +25,31 @@ func (h *ImgHandler) handleGET(w http.ResponseWriter, req *http.Request) {
2425
}
2526

2627
func (h *ImgHandler) handlePOST(w http.ResponseWriter, req *http.Request) {
28+
h.createDocument(req)
2729
io.WriteString(w, "Hello POST\n")
2830
}
31+
32+
func (h *ImgHandler) createDocument(req *http.Request) {
33+
s := MgoSession.Copy()
34+
defer s.Close()
35+
36+
name, path := h.convertPath(req.URL.Path)
37+
document := &Document{Name: name, Path: path}
38+
err := document.Save(s)
39+
if err != nil {
40+
log.Fatalln(err)
41+
}
42+
}
43+
44+
func (h *ImgHandler) convertPath(urlPath string) (string, string) {
45+
var path []string
46+
folders := strings.Split(urlPath, "/")
47+
for ind, folder := range folders {
48+
trimFolder := strings.Trim(folder, " ")
49+
if trimFolder != "" && ind != len(folders)-1 {
50+
path = append(path, strings.ToLower(folder))
51+
}
52+
}
53+
54+
return folders[len(folders)-1], strings.Join(path, ",")
55+
}

main.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,48 @@ import (
77
"os"
88
"path/filepath"
99
"time"
10+
11+
"labix.org/v2/mgo"
12+
)
13+
14+
var (
15+
Configuration Config
16+
MgoSession *mgo.Session
1017
)
1118

1219
type Config struct {
1320
Address string `json:"address"`
1421
ReadTimeout time.Duration `json:"read_timeout"`
1522
WriteTimeout time.Duration `json:"write_timeout"`
23+
DBHost string `json:"db_host"`
24+
DBName string `json:"db_name"`
25+
Collection string `json:"collection"`
1626
}
1727

1828
func main() {
1929
// read config file
2030
configFile, err := os.Open(filepath.Join(
2131
os.Getenv("GOPATH"), "src", "github.com", "arkxu", "imgongo", "config.json"))
2232
if err != nil {
23-
log.Fatal(err)
33+
log.Panicln(err)
2434
}
2535

26-
var configuration Config
27-
json.NewDecoder(configFile).Decode(&configuration)
36+
json.NewDecoder(configFile).Decode(&Configuration)
37+
38+
// Initialize mongo connection
39+
log.Println(Configuration.DBHost)
40+
MgoSession, err = mgo.Dial(Configuration.DBHost)
41+
if err != nil {
42+
log.Panicln(err)
43+
}
2844

2945
// start the server
3046
s := &http.Server{
31-
Addr: configuration.Address,
47+
Addr: Configuration.Address,
3248
Handler: new(ImgHandler),
33-
ReadTimeout: configuration.ReadTimeout * time.Second,
34-
WriteTimeout: configuration.WriteTimeout * time.Second,
49+
ReadTimeout: Configuration.ReadTimeout * time.Second,
50+
WriteTimeout: Configuration.WriteTimeout * time.Second,
3551
}
36-
log.Fatal(s.ListenAndServe())
52+
log.Panicln(s.ListenAndServe())
53+
3754
}

0 commit comments

Comments
 (0)