使用 Go 實作「短網址產生器」應用程式

前言

以下實作一個可以將檔案上傳至 Minio 伺服器的短網址服務。

建立專案

建立專案。

1
2
mkdir shortener-api
cd shortener-api

初始化 Go Modules。

1
go mod init github.com/memochou1993/shortener-api

實作

新增 .env 檔。

1
2
3
4
5
6
7
8
9
10
11
APP_KEY=
APP_OBJECT_ENDPOINT=
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
MINIO_ENDPOINT=
MINIO_ACCESS_KEY=
MINIO_SECRET_KEY=
MINIO_BUCKET=

新增 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
_ "github.com/joho/godotenv/autoload"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/speps/go-hashids/v2"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)

var (
hd *hashids.HashIDData
db *gorm.DB
minioClient *minio.Client
bucketName string
count int64
)

type Link struct {
ID uint `gorm:"primarykey" json:"-"`
Source string `json:"source"`
Code string `gorm:"index" json:"code"`
Key string `gorm:"key" json:"key"`
ContentType string `gorm:"content_type" json:"content_type"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
}

func init() {
hd = hashids.NewData()
hd.Salt = os.Getenv("APP_KEY")
hd.MinLength = 5

dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true",
os.Getenv("DB_USERNAME"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_DATABASE"),
)
var err error
if db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}); err != nil {
log.Fatal(err.Error())
}
if err = db.AutoMigrate(&Link{}); err != nil {
log.Fatal(err.Error())
}

endpoint := os.Getenv("MINIO_ENDPOINT")
accessKey := os.Getenv("MINIO_ACCESS_KEY")
secretKey := os.Getenv("MINIO_SECRET_KEY")
bucketName = os.Getenv("MINIO_BUCKET")
if minioClient, err = minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
}); err != nil {
log.Fatal(err)
}
exists, err := minioClient.BucketExists(context.Background(), bucketName)
if err != nil {
log.Fatal(err.Error())
}
if !exists {
opts := minio.MakeBucketOptions{}
if err = minioClient.MakeBucket(context.Background(), bucketName, opts); err != nil {
log.Fatal(err.Error())
}
}
}

func main() {
r := mux.NewRouter()
r.HandleFunc("/{code}", Redirect).Methods(http.MethodGet, http.MethodOptions)
r.HandleFunc("/api/links/{code}", ShowLink).Methods(http.MethodGet, http.MethodOptions)
r.HandleFunc("/api/links", StoreLink).Methods(http.MethodPost, http.MethodOptions)
r.HandleFunc("/api/links/{code}", DestroyLink).Methods(http.MethodDelete, http.MethodOptions)
r.HandleFunc("/api/objects/{object}", ShowObject).Methods(http.MethodGet, http.MethodOptions)
r.HandleFunc("/api/objects", StoreObject).Methods(http.MethodPost, http.MethodOptions)
log.Fatal(http.ListenAndServe(":80", r))
}

func Redirect(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
response(w, http.StatusOK, nil)
return
}
link := Link{}
if err := find(mux.Vars(r)["code"], &link); err != nil {
response(w, http.StatusNotFound, nil)
return
}
http.Redirect(w, r, link.Source, http.StatusMovedPermanently)
}

func StoreLink(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
response(w, http.StatusOK, nil)
return
}
link := Link{}
if err := json.NewDecoder(r.Body).Decode(&link); err != nil {
response(w, http.StatusInternalServerError, Payload{Error: err.Error()})
return
}
store(&link)
response(w, http.StatusCreated, Payload{
Data: link,
})
}

func ShowLink(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
response(w, http.StatusOK, nil)
return
}
link := Link{}
if err := find(mux.Vars(r)["code"], &link); err != nil {
response(w, http.StatusNotFound, nil)
return
}
response(w, http.StatusOK, Payload{
Data: link,
})
}

func DestroyLink(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
response(w, http.StatusOK, nil)
return
}
link := Link{}
if err := find(mux.Vars(r)["code"], &link); err != nil {
response(w, http.StatusNotFound, nil)
return
}
if link.Key != "" && link.Key != r.URL.Query().Get("key") {
response(w, http.StatusForbidden, nil)
return
}
if link.ContentType != "" {
objectName := filepath.Base(link.Source)
opts := minio.RemoveObjectOptions{}
err := minioClient.RemoveObject(context.Background(), bucketName, objectName, opts)
if err != nil {
response(w, http.StatusInternalServerError, err.Error())
return
}
}
db.Delete(&link)
response(w, http.StatusNoContent, nil)
}

func StoreObject(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
response(w, http.StatusOK, nil)
return
}
if err := r.ParseMultipartForm(32 << 20); err != nil {
response(w, http.StatusBadRequest, err.Error())
return
}
fileHeaders := r.MultipartForm.File["files[]"]
key := r.MultipartForm.Value["key"][0]
if len(fileHeaders) < 1 {
response(w, http.StatusBadRequest, nil)
return
}
header := fileHeaders[0]
file, err := header.Open()
if err != nil {
response(w, http.StatusInternalServerError, err.Error())
return
}
defer func() {
if err := file.Close(); err != nil {
log.Println(err.Error())
}
}()
objectName := strings.ReplaceAll(uuid.NewString(), "-", "") + filepath.Ext(header.Filename)
b, err := ioutil.ReadAll(r.Body)
if err != nil {
response(w, http.StatusInternalServerError, err.Error())
return
}
contentType := http.DetectContentType(b)
opts := minio.PutObjectOptions{ContentType: contentType}
_, err = minioClient.PutObject(context.Background(), bucketName, objectName, file, header.Size, opts)
if err != nil {
response(w, http.StatusInternalServerError, err.Error())
return
}
link := Link{
Source: fmt.Sprintf("%s/%s", os.Getenv("APP_OBJECT_ENDPOINT"), objectName),
Key: key,
ContentType: contentType,
}
store(&link)
response(w, http.StatusCreated, Payload{
Data: link,
})
}

func ShowObject(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
response(w, http.StatusOK, nil)
return
}
objectName := mux.Vars(r)["object"]
opts := minio.GetObjectOptions{}
object, err := minioClient.GetObject(context.Background(), bucketName, objectName, opts)
if err != nil {
response(w, http.StatusInternalServerError, err.Error())
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
if _, err = io.Copy(w, object); err != nil {
response(w, http.StatusNotFound, err.Error())
}
}

func find(code string, link *Link) error {
id, err := decode(code)
if err != nil {
return err
}
err = db.Where("id = ?", id).First(link).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
return nil
}

func store(link *Link) {
db.Model(link).Unscoped().Count(&count)
count++
link.Code = encode(count)
db.Create(link)
}

func encode(id int64) string {
h, _ := hashids.NewWithData(hd)
e, _ := h.Encode([]int{int(id)})
return e
}

func decode(code string) (int, error) {
h, _ := hashids.NewWithData(hd)
d, err := h.DecodeWithError(code)
if err != nil {
return 0, err
}
return d[0], nil
}

type Payload struct {
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}

func response(w http.ResponseWriter, code int, v interface{}) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.WriteHeader(code)
if v == nil {
return
}
if err := json.NewEncoder(w).Encode(v); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

啟動程式。

1
go run main.go

線上展示

程式碼