윤준석

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
# Ignore code-workspaces
*.code-workspace
# Ignore .env file
.env
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode,node,jetbrains
......
FROM --platform=linux/x86_64 mysql:5.7
ADD ./mysql_init /docker-entrypoint-initdb.d
EXPOSE 3306
CMD ["mysqld"]
\ No newline at end of file
#!/usr/bin/env bash
docker build -t mamuri-db .
docker-compose up -d
\ No newline at end of file
version: '3'
services:
db:
image: mamuri-db
restart: always
container_name: mamuri-db-container
ports:
- '33060:3306'
env_file:
- "./mysql_init/.env"
TZ=Asia/Seoul
MYSQL_HOST={YOUR_MYSQL_HOST}
MYSQL_PORT={YOUR_MYSQL_PORT}
MYSQL_ROOT_PASSWORD={YOUR_MYSQL_ROOT_PASSWORD}
\ No newline at end of file
CREATE DATABASE mamuri_db;
USE mamuri_db;
CREATE TABLE user
(
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL
);
CREATE TABLE keyword
(
id INT AUTO_INCREMENT PRIMARY KEY,
keyword VARCHAR(150) NULL
);
CREATE TABLE user_keyword
(
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
keyword_id INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE,
FOREIGN KEY (keyword_id) REFERENCES keyword (id) ON DELETE CASCADE
);
CREATE TABLE item
(
id INT AUTO_INCREMENT PRIMARY KEY,
keyword_id INT NOT NULL,
platform VARCHAR(50) NOT NULL,
name VARCHAR(100) NOT NULL,
price INT NOT NULL,
thumbnail_url VARCHAR(255) NULL,
item_url VARCHAR(255) NOT NULL,
extra_info TEXT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE item_check
(
id INT AUTO_INCREMENT PRIMARY KEY,
item_id INT NULL,
user_id INT NULL,
FOREIGN KEY (item_id) REFERENCES item (id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
);
\ No newline at end of file
#!/usr/bin/env bash
docker-compose down
docker image rm mamuri-db
\ No newline at end of file
# Secret Configuration
SECRET.CLIENTID=
SECRET.CLIENTSECRET=
\ No newline at end of file
package config
import (
"log"
"github.com/caarlos0/env/v6"
"github.com/joho/godotenv"
)
type Config struct {
Secret struct {
CLIENTID string `env:"SECRET.CLIENTID"`
CLIENTSECRET string `env:"SECRET.CLIENTSECRET"`
}
}
var Cfg *Config
func init() {
err := godotenv.Load("./config/.env")
if err != nil {
log.Fatal("Load .env file failed.")
}
config := Config{}
if err := env.Parse(&config); err != nil {
log.Fatalf("%+v\n", err)
}
Cfg = &config
}
module joongna
go 1.17
require (
github.com/caarlos0/env/v6 v6.9.1 // indirect
github.com/joho/godotenv v1.4.0 // indirect
)
github.com/caarlos0/env/v6 v6.9.1 h1:zOkkjM0F6ltnQ5eBX6IPI41UP/KDGEK7rRPwGCNos8k=
github.com/caarlos0/env/v6 v6.9.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
package main
import (
"fmt"
"io/ioutil"
"joongna/config"
"log"
"net/http"
url2 "net/url"
)
func main() {
keyword := "m1 pro 맥북 프로 16인치"
encText := url2.QueryEscape("중고나라" + keyword)
url := "https://openapi.naver.com/v1/search/cafearticle.json?query=" + encText + "&sort=sim"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("X-Naver-Client-Id", config.Cfg.Secret.CLIENTID)
req.Header.Add("X-Naver-Client-Secret", config.Cfg.Secret.CLIENTSECRET)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
str := string(bytes)
fmt.Println(str)
}