-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (126 loc) · 4.07 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"database/sql"
"html/template"
"log"
"net/http"
"sync"
"time"
"os"
"io"
_ "github.com/go-sql-driver/mysql"
)
type Item struct {
Id int `db:"id"`
Judul string `db:"judul"`
Isi string `db:"isi"`
Ktg int `db:"ktg"`
Tgl time.Time `db:"tgl"`
}
func main() {
db, err := sql.Open("mysql", "root:1234@/adsense")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Mengatur parameter koneksi database
db.SetConnMaxLifetime(time.Minute * 3)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
http.Handle("/gambar/", http.StripPrefix("/gambar/",
http.FileServer(http.Dir("gambar"))))
http.HandleFunc("/tes", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
var results []Item
var wg sync.WaitGroup
// Menggunakan goroutine untuk mengambil data dari database
wg.Add(1)
go func() {
defer wg.Done()
stmt, err := db.Prepare("SELECT * FROM post")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer stmt.Close()
rows, err := stmt.Query()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
for rows.Next() {
var data Item
var times string
err := rows.Scan(&data.Id, &data.Judul, &data.Isi,
&data.Ktg, ×)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Mengkonversi string timestampValue menjadi time.Time
data.Tgl, err = time.Parse("2006-01-02 15:04:05", times)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
results = append(results, data)
}
}()
// Menunggu goroutine selesai
wg.Wait()
tmpl, err := template.ParseFiles("index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, results)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else if r.Method == http.MethodPost {
no := r.PostFormValue("no")
// Menyimpan data ke database
stmt, err := db.Prepare("INSERT INTO tes (no) VALUES (?)")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer stmt.Close()
_, err = stmt.Exec(no) // Menggunakan "no" karena "id" tidak didefinisikan
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Menerima file dari frontend
file, header, err := r.FormFile("file") // "gambar" adalah nama field pada form HTML
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
// Mendapatkan nama file dan ekstensinya
fileName := header.Filename
// Membuka file untuk penyimpanan
newFile, err := os.Create("gambar/" + fileName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer newFile.Close()
// Menyalin isi file yang diterima ke file baru
_, err = io.Copy(newFile, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// route closer
}
// end route
})
err = http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal(err)
}
}