-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
143 lines (113 loc) · 3.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
firebase "firebase.google.com/go"
"github.com/nattawat.s/golang-firebase-cloud-storage/models"
"cloud.google.com/go/firestore"
cloud "cloud.google.com/go/storage"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"google.golang.org/api/option"
)
type App struct {
Router *mux.Router
ctx context.Context
client *firestore.Client
storage *cloud.Client
}
func main() {
godotenv.Load()
route := App{}
route.Init()
route.Run()
}
func (route *App) Init() {
route.ctx = context.Background()
sa := option.WithCredentialsFile("serviceAccountKey.json")
var err error
app, err := firebase.NewApp(route.ctx, nil, sa)
if err != nil {
log.Fatalln(err)
}
route.client, err = app.Firestore(route.ctx)
if err != nil {
log.Fatalln(err)
}
route.storage, err = cloud.NewClient(route.ctx, sa)
if err != nil {
log.Fatalln(err)
}
route.Router = mux.NewRouter()
route.initializeRoutes()
fmt.Println("Successfully connected at port : " + route.GetPort())
}
func (route *App) GetPort() string {
var port = os.Getenv("MyPort")
if port == "" {
port = "5000"
}
return ":" + port
}
func (route *App) Run() {
log.Fatal(http.ListenAndServe(route.GetPort(), route.Router))
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
func respondWithError(w http.ResponseWriter, code int, message string) {
respondWithJSON(w, code, map[string]string{"error": message})
}
func (route *App) initializeRoutes() {
route.Router.HandleFunc("/", route.Home).Methods("GET")
route.Router.HandleFunc("/upload/image", route.UploadImage).Methods("POST")
}
func (route *App) Home(w http.ResponseWriter, r *http.Request) {
respondWithJSON(w, http.StatusOK, "Hello World!")
}
func (route *App) UploadImage(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("image")
r.ParseMultipartForm(10 << 20)
if err != nil {
respondWithJSON(w, http.StatusBadRequest, err.Error())
return
}
defer file.Close()
imagePath := handler.Filename
bucket := "golang-cloud-firestore.appspot.com"
wc := route.storage.Bucket(bucket).Object(imagePath).NewWriter(route.ctx)
_, err = io.Copy(wc, file)
if err != nil {
respondWithJSON(w, http.StatusBadRequest, err.Error())
return
}
if err := wc.Close(); err != nil {
respondWithJSON(w, http.StatusBadRequest, err.Error())
return
}
err = CreateImageUrl(imagePath, bucket, route.ctx, route.client)
if err != nil {
respondWithJSON(w, http.StatusBadRequest, err.Error())
return
}
respondWithJSON(w, http.StatusCreated, "Create image success.")
}
func CreateImageUrl(imagePath string, bucket string, ctx context.Context, client *firestore.Client) error {
imageStructure := models.ImageStructure{
ImageName: imagePath,
URL: "https://storage.cloud.google.com/" + bucket + "/" + imagePath,
}
_, _, err := client.Collection("image").Add(ctx, imageStructure)
if err != nil {
return err
}
return nil
}