윤준석

Merge branch 'feature/220510_db_setting' into 'main'

Feature/220510 db setting

# DB 세팅 및 초기화

- mysql:5.7
- docker-compose로 deploy

See merge request !4
...@@ -276,4 +276,7 @@ dist ...@@ -276,4 +276,7 @@ dist
276 # Ignore code-workspaces 276 # Ignore code-workspaces
277 *.code-workspace 277 *.code-workspace
278 278
279 +# Ignore .env file
280 +.env
281 +
279 # End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,node,jetbrains 282 # End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,node,jetbrains
......
1 +FROM --platform=linux/x86_64 mysql:5.7
2 +
3 +ADD ./mysql_init /docker-entrypoint-initdb.d
4 +
5 +EXPOSE 3306
6 +
7 +CMD ["mysqld"]
...\ No newline at end of file ...\ No newline at end of file
1 +#!/usr/bin/env bash
2 +
3 +docker build -t mamuri-db .
4 +docker-compose up -d
...\ No newline at end of file ...\ No newline at end of file
1 +version: '3'
2 +
3 +services:
4 + db:
5 + image: mamuri-db
6 + restart: always
7 + container_name: mamuri-db-container
8 + ports:
9 + - '33060:3306'
10 + env_file:
11 + - "./mysql_init/.env"
1 +TZ=Asia/Seoul
2 +MYSQL_HOST={YOUR_MYSQL_HOST}
3 +MYSQL_PORT={YOUR_MYSQL_PORT}
4 +MYSQL_ROOT_PASSWORD={YOUR_MYSQL_ROOT_PASSWORD}
...\ No newline at end of file ...\ No newline at end of file
1 +CREATE DATABASE mamuri_db;
2 +
3 +USE mamuri_db;
4 +
5 +CREATE TABLE user
6 +(
7 + id INT AUTO_INCREMENT PRIMARY KEY,
8 + user_id INT NOT NULL
9 +);
10 +
11 +CREATE TABLE keyword
12 +(
13 + id INT AUTO_INCREMENT PRIMARY KEY,
14 + keyword VARCHAR(150) NULL
15 +);
16 +
17 +CREATE TABLE user_keyword
18 +(
19 + id INT AUTO_INCREMENT PRIMARY KEY,
20 + user_id INT NOT NULL,
21 + keyword_id INT NOT NULL,
22 + FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE,
23 + FOREIGN KEY (keyword_id) REFERENCES keyword (id) ON DELETE CASCADE
24 +);
25 +
26 +CREATE TABLE item
27 +(
28 + id INT AUTO_INCREMENT PRIMARY KEY,
29 + keyword_id INT NOT NULL,
30 + platform VARCHAR(50) NOT NULL,
31 + name VARCHAR(100) NOT NULL,
32 + price INT NOT NULL,
33 + thumbnail_url VARCHAR(255) NULL,
34 + item_url VARCHAR(255) NOT NULL,
35 + extra_info TEXT NULL,
36 + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
37 +);
38 +
39 +CREATE TABLE item_check
40 +(
41 + id INT AUTO_INCREMENT PRIMARY KEY,
42 + item_id INT NULL,
43 + user_id INT NULL,
44 + FOREIGN KEY (item_id) REFERENCES item (id) ON DELETE CASCADE,
45 + FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
46 +);
...\ No newline at end of file ...\ No newline at end of file
1 +#!/usr/bin/env bash
2 +
3 +docker-compose down
4 +docker image rm mamuri-db
...\ No newline at end of file ...\ No newline at end of file
1 +# Secret Configuration
2 +SECRET.CLIENTID=
3 +SECRET.CLIENTSECRET=
...\ No newline at end of file ...\ No newline at end of file
1 +package config
2 +
3 +import (
4 + "log"
5 +
6 + "github.com/caarlos0/env/v6"
7 + "github.com/joho/godotenv"
8 +)
9 +
10 +type Config struct {
11 + Secret struct {
12 + CLIENTID string `env:"SECRET.CLIENTID"`
13 + CLIENTSECRET string `env:"SECRET.CLIENTSECRET"`
14 + }
15 +}
16 +
17 +var Cfg *Config
18 +
19 +func init() {
20 + err := godotenv.Load("./config/.env")
21 + if err != nil {
22 + log.Fatal("Load .env file failed.")
23 + }
24 +
25 + config := Config{}
26 + if err := env.Parse(&config); err != nil {
27 + log.Fatalf("%+v\n", err)
28 + }
29 + Cfg = &config
30 +}
1 +module joongna
2 +
3 +go 1.17
4 +
5 +require (
6 + github.com/caarlos0/env/v6 v6.9.1 // indirect
7 + github.com/joho/godotenv v1.4.0 // indirect
8 +)
1 +github.com/caarlos0/env/v6 v6.9.1 h1:zOkkjM0F6ltnQ5eBX6IPI41UP/KDGEK7rRPwGCNos8k=
2 +github.com/caarlos0/env/v6 v6.9.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
3 +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
4 +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
1 +package main
2 +
3 +import (
4 + "fmt"
5 + "io/ioutil"
6 + "joongna/config"
7 + "log"
8 + "net/http"
9 + url2 "net/url"
10 +)
11 +
12 +func main() {
13 + keyword := "m1 pro 맥북 프로 16인치"
14 + encText := url2.QueryEscape("중고나라" + keyword)
15 + url := "https://openapi.naver.com/v1/search/cafearticle.json?query=" + encText + "&sort=sim"
16 +
17 + req, err := http.NewRequest("GET", url, nil)
18 + if err != nil {
19 + log.Fatal(err)
20 + }
21 + req.Header.Add("X-Naver-Client-Id", config.Cfg.Secret.CLIENTID)
22 + req.Header.Add("X-Naver-Client-Secret", config.Cfg.Secret.CLIENTSECRET)
23 +
24 + client := &http.Client{}
25 + resp, err := client.Do(req)
26 + if err != nil {
27 + log.Fatal(err)
28 + }
29 + defer resp.Body.Close()
30 +
31 + bytes, _ := ioutil.ReadAll(resp.Body)
32 + str := string(bytes)
33 + fmt.Println(str)
34 +}