Skip to content

Commit

Permalink
feat(hackathon): add route and struct for upload
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenphong1301 committed Feb 25, 2023
1 parent 74d611a commit 4bbb144
Show file tree
Hide file tree
Showing 31 changed files with 1,289 additions and 101 deletions.
6 changes: 4 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# application configuration
# ... this part is optional because config will get default value from env variable

DB_CONN_STR = "host=127.0.0.1 port=5432 user=dbUser password=dbPassword dbname=application sslmode=disable"
REDIS_HOST = "127.0.0.1:6379"

# config for expose port from docker container to host
DOCKER_APP_PORT = 80
DOCKER_DB_PORT = 5432
DOCKER_REDIS_PORT = 6379

# test configuration
TEST_DB_CONN_STR = "host=127.0.0.1 port=5432 user=db.user password=db.password sslmode=disable"
TEST_DB_CONN_STR = "host=127.0.0.1 port=5432 user=dbUser password=dbPassword sslmode=disable"
12 changes: 12 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/watcherTasks.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion cmd/v1/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"elotus/database/migration"
redis2 "elotus/package/redis"
"fmt"

Expand All @@ -10,10 +11,26 @@ import (

"elotus/config"
"elotus/global"
"elotus/internal/v1/interface/http"
"elotus/internal/v1/delivery/http"
"elotus/package/db"
)

// @title Fiber Swagger Example API
// @version 2.0
// @description This is a sample server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization

func main() {
global.Init()
defer global.DeInit()
Expand All @@ -28,9 +45,17 @@ func main() {
panic(err)
}

// migrate database
migration.CreateTable(database)

app := fiber.New()
app.Use(cors.New())
app.Use(requestid.New())
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowMethods: "GET, POST, PUT, DELETE",
AllowHeaders: "Origin, Content-Type, Accept, Accept-Language, Content-Length,Authorization",
}))

http.NewHandler(
http.WithEngine(app),
Expand Down
21 changes: 11 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package config

// all global environment variable read from ENV
var (
Environment = GetString("ENVIRONMENT", "development") // environment
Port = GetInt64("APP_PORT", 80) // app port
DbConnStr = GetString("DB_CONN_STR", "host=postgres port=5432 user=db.user password=db.password dbname=application sslmode=disable") // postgres connection string
DbMaxConn = GetInt64("DB_MAX_CONN", 10) // max connection to db
DbMaxIdleConn = GetInt64("DB_MAX_IDLE_CONN", 2) // max idle connection to db
DBLogLevel = GetInt64("DB_LOG_LEVEL", 4) // db log level
Environment = GetString("ENVIRONMENT", "development") // environment
Domain = GetString("DOMAIN", "127.0.0.1") // domain
Port = GetInt64("APP_PORT", 80) // app port
DbConnStr = GetString("DB_CONN_STR", "host=postgres port=5432 user=dbUser password=dbPassword dbname=application sslmode=disable") // postgres connection string
DbMaxConn = GetInt64("DB_MAX_CONN", 10) // max connection to db
DbMaxIdleConn = GetInt64("DB_MAX_IDLE_CONN", 2) // max idle connection to db
DBLogLevel = GetInt64("DB_LOG_LEVEL", 4) // db log level
JWTKey = GetString("JWT_KEY", "development-key")
TokenLifeTime = GetInt64("TOKEN_LIFE_TIME", 2) // jwt token life time
StoragePath = GetString("STORAGE_PATH", "/upload") // upload path
RedisHost = GetString("REDIS_HOST", "redis:6379") // redis host, include port
RedisPassword = GetString("REDIS_PASSWORD", "redis.password") // redis pw
TokenLifeTime = GetInt64("TOKEN_LIFE_TIME", 2) // jwt token life time
StoragePath = GetString("STORAGE_PATH", "/upload") // upload path
RedisHost = GetString("REDIS_HOST", "redis:6379") // redis host, include port
RedisPassword = GetString("REDIS_PASSWORD", "redisPassword") // redis pw
LogLevel = GetInt64("LOG_LEVEL", -1)
)
1 change: 0 additions & 1 deletion database/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ func CreateTable(db *gorm.DB) {

func DropTable(db *gorm.DB) {
db.Migrator().DropTable(&model.User{})

}
13 changes: 8 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ services:
ports:
- "${DOCKER_DB_PORT}:5432"
environment:
POSTGRES_PASSWORD: "db.user"
POSTGRES_USER: "db.password"
POSTGRES_PASSWORD: "dbPassword"
POSTGRES_USER: "dbUser"
volumes:
- ./database/migration/database_init.sql:/docker-entrypoint-initdb.d/init.sql
- ../postgres:/var/lib/postgresql/data
- ./database/initsql/database_init.sql:/docker-entrypoint-initdb.d/init.sql
restart: always
redis:
container_name: application-redis
image: redis
command: >
--requirepass redisPassword
environment:
REDIS_PASSWORD: "redis.password"
REDIS_PASSWORD: redisPassword
ports:
- "${DOCKER_REDIS_PORT}:6379"
app:
container_name: application-server
image: golang:1.20-alpine
Expand Down
Loading

0 comments on commit 4bbb144

Please sign in to comment.