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 603f30f0
authored
Feb 25, 2025
by
mhmdhaekal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(initialize): move config to domain
1 parent
5e441d8e
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
42 deletions
cmd/app-1/main.go
internal/domain/config.go
internal/handler/error.go
cmd/app-1/main.go
View file @
603f30f
...
...
@@ -2,12 +2,11 @@ package main
import
(
"log"
"
os
"
"
ui.ac.id/dtd/go-webserver-boilerplate/internal/domain
"
"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"
...
...
@@ -15,49 +14,12 @@ 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
()
{
config
:=
i
nitConfig
()
config
:=
domain
.
I
nitConfig
()
db
,
err
:=
gorm
.
Open
(
postgres
.
Open
(
config
.
DatabaseUrl
),
&
gorm
.
Config
{})
if
err
!=
nil
{
log
.
Fatal
(
err
)
os
.
Exit
(
1
)
}
userRepository
:=
postgresql
.
NewPostgresUserRepository
(
db
)
...
...
@@ -82,5 +44,9 @@ func main() {
user
.
Get
(
"/:id"
,
userHandler
.
GetUserDetail
)
user
.
Get
(
"/"
,
userHandler
.
GetUserList
)
app
.
Listen
(
":"
+
config
.
Port
)
err
=
app
.
Listen
(
":"
+
config
.
Port
)
if
err
!=
nil
{
return
}
}
internal/domain/config.go
0 → 100644
View file @
603f30f
package
domain
import
(
"github.com/joho/godotenv"
"log"
"os"
)
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
,
}
}
internal/handler/error.go
View file @
603f30f
package
handler
import
(
"errors"
"github.com/gofiber/fiber/v3"
"ui.ac.id/dtd/go-webserver-boilerplate/internal/domain"
// Adjust import path to your project
)
...
...
@@ -34,7 +35,8 @@ func ErrorHandler(ctx fiber.Ctx, err error) error {
}
func
formatError
(
err
error
)
ErrorResponse
{
if
customErr
,
ok
:=
err
.
(
*
domain
.
Error
);
ok
{
var
customErr
*
domain
.
Error
if
errors
.
As
(
err
,
&
customErr
)
{
errResp
:=
ErrorResponse
{
Message
:
customErr
.
Message
,
}
...
...
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