-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (87 loc) · 2.24 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
package main
import (
"context"
"fmt"
"html/template"
"io"
"log"
"net/http"
"cloud.google.com/go/storage"
"github.com/go-chi/chi"
)
const bucketName = "haki_melodylane" //REPLACE BUCKET NAME
var bkt *storage.BucketHandle
type Album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Content string `json:"content"`
}
const markdownTemplate = `
----
title: {{ .Title }}
artist: {{ .Artist }}
----
{{ .Content }}
`
func init() {
client, err := storage.NewClient(context.TODO())
if err != nil {
log.Fatal(err)
}
//Making a bucket handle
bkt = client.Bucket(bucketName)
}
func main() {
r := chi.NewRouter()
r.Post("/albums", createAlbumHandler)
r.Delete("/albums/{id}", deleteAlbumHandler)
if err := http.ListenAndServe(":8080", r); err != nil {
log.Fatal(err)
}
}
func createAlbumHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
if err := r.ParseMultipartForm(32 << 20); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
album := Album{
ID: r.PostFormValue("ID"),
Title: r.PostFormValue("Title"),
Artist: r.PostFormValue("Artist"),
Content: r.PostFormValue("Content"),
}
// UPLOAD ALBUM METADATA
objAlbumWriter := bkt.Object(album.ID + "/index.md").NewWriter(ctx)
tmpl, err := template.New("album").Parse(markdownTemplate)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(objAlbumWriter, album)
if err != nil {
log.Fatal(err)
}
if err := objAlbumWriter.Close(); err != nil {
log.Fatal(err)
}
objCoverWriter := bkt.Object(album.ID + "/cover.jpg").NewWriter(ctx)
photo, _, _ := r.FormFile("cover")
if _, err := io.Copy(objCoverWriter, photo); err != nil {
log.Fatalf("Failed to write cover: %v", err)
}
if err := objCoverWriter.Close(); err != nil {
log.Fatalf("Failed to close object: %v", err)
}
fmt.Fprintf(w, "Album created successfully")
}
func deleteAlbumHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
albumID := chi.URLParam(r, "id")
obj := bkt.Object(albumID + "/index.md")
if err := obj.Delete(ctx); err != nil {
http.Error(w, "Error deleting user", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Album deleted successfully")
}