Commit 8d215c20 authored by Somnath Ghorpade's avatar Somnath Ghorpade
Browse files

Merge branch 'gogintemplate-dockerized' into 'main'

#1  Dockerize the projects and fixed the logger

See merge request !1
parents 13d37c25 5513fe64
1 merge request!1#1 Dockerize the projects and fixed the logger
Showing with 143 additions and 80 deletions
# Use a smaller base image for the build stage
FROM golang:1.20.0-alpine AS builder
# Set the working directory
WORKDIR /workspace
# Copy only the necessary files for dependency resolution
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy the entire application source
COPY . .
# Build the Go application with optimizations enabled
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -tags prod -o /bin/app ./servers/
# Use a smaller base image for the final stage
FROM alpine:3.20
# Set the working directory
WORKDIR /application
# Copy the built binary from the builder stage
COPY --from=builder /bin/app /application/
# Copy the configs folder from the local machine to the image
COPY ./configs/ /application/configs
# Set the entry point for the container
ENTRYPOINT ["/application/app"]
......@@ -50,4 +50,75 @@ Golang Gin web framework template project
- To get the connection object GetImmuDAOWithHost(coreimmudb.DefaultHostName) method is used.
`GetImmuDAOWithHost(coreimmudb.DefaultHostName)` returns the immudb server connection object of specified HostName. This method is used when we need to get connection object, other than default database.
Note: All Immudb operations' result is in Base64 format.
\ No newline at end of file
Note: All Immudb operations' result is in Base64 format.
## database json format
```json
{
"MongoConfig": [
{
"hostName": "CoreStudio",
"server": "localhost",
"port": 27017,
"username": "",
"password": "",
"database": "CoreStudio",
"isDefault": true
}
],
"ImmudbConfig":[{
"hostName": "ImmuDbHost",
"server": "localhost",
"port": 3322,
"database": "",
"username": "",
"password": "",
"isDefault": true
}],
"MysqlConfig": [
{
"hostName": "MySQLHost",
"server": "localhost",
"port": 3306,
"username": "",
"password": "",
"database": "",
"isDefault":true
}
],
"SqlServerConfig": [
{
"connMaxLifetime": 0,
"database": "",
"dbType": "SQLSERVER",
"hostName": "SQLServerHost",
"isDefault": true,
"maxIdleConns": 0,
"maxOpenConns": 2,
"parameters": [],
"password": "",
"port": 65142,
"protocol": "",
"server": "",
"sqlParameters": [
{
"key": "testKey",
"value": "testValue"
},
{
"key": "boolFlag",
"value": "false"
},
{
"key": "intFlag",
"value": "100"
}
],
"username": ""
}
]
}
```
version: "3"
tasks:
run:
cmds:
- sudo docker-compose up --build --remove-orphans
{}
\ No newline at end of file
{
"MongoConfig": [
{
"hostName": "CoreStudio",
"server": "localhost",
"port": 27017,
"username": "",
"password": "",
"database": "CoreStudio",
"isDefault": true
}
],
"ImmudbConfig":[{
"hostName": "ImmuDbHost",
"server": "localhost",
"port": 3322,
"database": "",
"username": "",
"password": "",
"isDefault": true
}],
"MysqlConfig": [
{
"hostName": "MySQLHost",
"server": "localhost",
"port": 3306,
"username": "",
"password": "",
"database": "",
"isDefault":true
}
],
"SqlServerConfig": [
{
"connMaxLifetime": 0,
"database": "",
"dbType": "SQLSERVER",
"hostName": "SQLServerHost",
"isDefault": true,
"maxIdleConns": 0,
"maxOpenConns": 2,
"parameters": [],
"password": "",
"port": 65142,
"protocol": "",
"server": "",
"sqlParameters": [
{
"key": "testKey",
"value": "testValue"
},
{
"key": "boolFlag",
"value": "false"
},
{
"key": "intFlag",
"value": "100"
}
],
"username": ""
}
]
}
version: '3'
services:
gogintemplate:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
volumes:
- ./logs:/application/logs
- ./configs:/application/configs
\ No newline at end of file
......@@ -13,7 +13,7 @@ import (
var (
// JWTKey - JWTKey for r and c
JWTKey = "gUkXp2s5v8y/B?E(H+MbQeThVmYq3t6w"
ConfigPath = "../database.json"
ConfigPath = "configs/database.json"
Host = "CoreStudio"
Database = "CoreStudio"
Collection = "gotemplate"
......
......@@ -16,6 +16,8 @@ import (
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/filemdl"
"go.uber.org/zap/zapcore"
"log"
"github.com/gin-contrib/cors"
"github.com/tidwall/gjson"
......@@ -39,13 +41,13 @@ func main() {
//Access log
accessLog := filepath.Join("logs")
if err := filemdl.CreateDirectoryRecursive(accessLog); err != nil {
loggermdl.LogError(err)
log.Println(err)
return
}
logFile, err := os.Create(filepath.Join(accessLog, "access.log"))
if err != nil {
loggermdl.LogError(err)
log.Println(err)
return
}
g := gin.New()
......@@ -53,17 +55,18 @@ func main() {
gin.DefaultWriter = logFile
g.Use(gin.LoggerWithWriter(logFile))
g.Use(middleware.Recovery())
if !IS_PRODUCTION {
mainLogFile, err := os.Create(filepath.Join(accessLog, "main.log"))
if err != nil {
loggermdl.LogError(err)
return
}
gin.DefaultErrorWriter = mainLogFile
gin.DefaultWriter = mainLogFile
g.Use(gin.LoggerWithWriter(mainLogFile))
g.Use(middleware.CustomLogger())
}
initLogger()
// if !IS_PRODUCTION {
// mainLogFile, err := os.Create(filepath.Join(accessLog, "main.log"))
// if err != nil {
// log.Println(err)
// return
// }
// gin.DefaultErrorWriter = mainLogFile
// gin.DefaultWriter = mainLogFile
// g.Use(gin.LoggerWithWriter(mainLogFile))
// g.Use(middleware.CustomLogger())
// }
md := cors.DefaultConfig()
md.AllowAllOrigins = true
md.AllowHeaders = []string{"*"}
......@@ -92,7 +95,7 @@ func main() {
return
}
}
initLogger()
loggermdl.LogError("Server started on ", netListen.Addr().String())
err = http.Serve(netListen, g)
if errormdl.CheckErr(err) != nil {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment