Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
dtd
/
go-webserver-boilerplate
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 283bd3d8
authored
Feb 20, 2025
by
mhmdhaekal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(initialize): Implement config
1 parent
f85db4fe
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
1 deletions
.env.example
cmd/app-1/main.go
go.mod
go.sum
.env.example
View file @
283bd3d
DATABASE_URL=
APP_ENVIRONMENT=development
cmd/app-1/main.go
View file @
283bd3d
...
...
@@ -7,6 +7,7 @@ import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/healthcheck"
"github.com/gofiber/fiber/v3/middleware/logger"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"ui.ac.id/dtd/go-webserver-boilerplate/internal/handler"
...
...
@@ -14,8 +15,45 @@ import (
"ui.ac.id/dtd/go-webserver-boilerplate/internal/service"
)
type
Config
struct
{
DatabaseUrl
string
AppEnvironment
string
Port
string
}
func
initConfig
()
Config
{
environment
:=
os
.
Getenv
(
"APP_ENVIRONMENT"
)
if
environment
==
""
{
environment
=
"production"
}
if
environment
==
"development"
{
err
:=
godotenv
.
Load
()
if
err
!=
nil
{
panic
(
err
)
}
}
port
:=
os
.
Getenv
(
"PORT"
)
if
port
==
""
{
port
=
"8000"
}
log
.
Printf
(
"Environment: %s"
,
environment
)
return
Config
{
DatabaseUrl
:
os
.
Getenv
(
"DATABASE_URL"
),
AppEnvironment
:
environment
,
Port
:
port
,
}
}
func
main
()
{
db
,
err
:=
gorm
.
Open
(
postgres
.
Open
(
""
),
&
gorm
.
Config
{})
config
:=
initConfig
()
db
,
err
:=
gorm
.
Open
(
postgres
.
Open
(
config
.
DatabaseUrl
),
&
gorm
.
Config
{})
if
err
!=
nil
{
log
.
Fatal
(
err
)
...
...
@@ -43,4 +81,6 @@ func main() {
user
:=
v1
.
Group
(
"/user"
)
user
.
Get
(
"/:id"
,
userHandler
.
GetUserDetail
)
user
.
Get
(
"/"
,
userHandler
.
GetUserList
)
app
.
Listen
(
":"
+
config
.
Port
)
}
go.mod
View file @
283bd3d
...
...
@@ -15,6 +15,7 @@ require (
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
...
...
go.sum
View file @
283bd3d
...
...
@@ -23,6 +23,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment