-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
226 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
root = "." | ||
testdata_dir = "testdata" | ||
tmp_dir = "tmp" | ||
|
||
[build] | ||
args_bin = [] | ||
bin = "tmp\\main.exe" | ||
cmd = "go build -o ./tmp/main.exe ./cmd" | ||
delay = 1000 | ||
exclude_dir = ["assets", "tmp", "vendor", "testdata"] | ||
exclude_file = [] | ||
exclude_regex = ["_test.go"] | ||
exclude_unchanged = false | ||
follow_symlink = false | ||
full_bin = "" | ||
include_dir = [] | ||
include_ext = ["go", "tpl", "tmpl", "html"] | ||
include_file = [] | ||
kill_delay = "0s" | ||
log = "build-errors.log" | ||
poll = false | ||
poll_interval = 0 | ||
post_cmd = [] | ||
pre_cmd = [] | ||
rerun = false | ||
rerun_delay = 500 | ||
send_interrupt = false | ||
stop_on_error = false | ||
|
||
[color] | ||
app = "" | ||
build = "yellow" | ||
main = "magenta" | ||
runner = "green" | ||
watcher = "cyan" | ||
|
||
[log] | ||
main_only = false | ||
time = false | ||
|
||
[misc] | ||
clean_on_exit = false | ||
|
||
[screen] | ||
clear_on_rebuild = false | ||
keep_scroll = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,12 @@ | |
*.so | ||
*.dylib | ||
|
||
./vendor | ||
./vendor/* | ||
vendor | ||
vendor/* | ||
.env | ||
/tmp | ||
|
||
.vscode | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package sharedRouter | ||
|
||
import ( | ||
"context" | ||
|
||
v1Router "github.com/quinntas/go-rest-template/api/shared/infra/router/v1" | ||
) | ||
|
||
func InitRouters(ctx context.Context) { | ||
v1Router.NewV1Router("/api/v1", ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package v1Router | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
healthcheckUseCase "github.com/quinntas/go-rest-template/api/shared/useCases/healthCheck" | ||
"github.com/quinntas/go-rest-template/internal/api/web" | ||
) | ||
|
||
func NewV1Router(path string, ctx context.Context) { | ||
router := web.NewHttpRouter(path, ctx) | ||
|
||
web.Route(router, http.MethodGet, "/health", ctx, healthcheckUseCase.Execute) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package healthcheckUseCase | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/quinntas/go-rest-template/internal/api/utils" | ||
"github.com/quinntas/go-rest-template/internal/api/web" | ||
) | ||
|
||
func Execute(request *http.Request, response http.ResponseWriter, ctx context.Context) error { | ||
web.JsonResponse(response, http.StatusOK, &utils.Map[string]{ | ||
"message": "ok", | ||
}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package utils | ||
|
||
type Key string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package env | ||
|
||
import "github.com/quinntas/go-rest-template/internal/api/utils" | ||
|
||
const ( | ||
ENV_CTX_KEY utils.Key = "ENV" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package utils | ||
|
||
type Map[T interface{}] map[string]T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
package web | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/quinntas/go-rest-template/internal/api/utils" | ||
) | ||
|
||
const ( | ||
JSON_CTX_KEY = "JSON" | ||
QUERY_CTX_KEY = "QUERY" | ||
PATH_CTX_KEY = "PATH" | ||
JSON_CTX_KEY utils.Key = "JSON" | ||
QUERY_CTX_KEY utils.Key = "QUERY" | ||
PATH_CTX_KEY utils.Key = "PATH" | ||
) | ||
|
||
func applyDefaultHeaders(w http.ResponseWriter) { | ||
w.Header().Set("Access-Control-Allow-Origin", "*") | ||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS") | ||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") | ||
w.Header().Set("Server", "Encom-Backend/1.0.0 (Golang)") | ||
w.Header().Set("X-Powered-By", "Encom") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,60 @@ | ||
package web | ||
|
||
import "net/http" | ||
import ( | ||
"net/http" | ||
|
||
"github.com/quinntas/go-rest-template/internal/api/utils" | ||
) | ||
|
||
type HttpError struct { | ||
status int | ||
message string | ||
body interface{} | ||
body utils.Map[interface{}] | ||
} | ||
|
||
func (e *HttpError) Error() string { | ||
return e.message | ||
} | ||
|
||
func (e *HttpError) ToJsonResponse(response http.ResponseWriter) { | ||
e.body["message"] = e.message | ||
JsonResponse( | ||
response, | ||
e.status, | ||
&map[string]string{ | ||
"message": e.message, | ||
}, | ||
&e.body, | ||
) | ||
} | ||
|
||
func NewHttpError[T interface{}](status int, message string, body T) *HttpError { | ||
func NewHttpError(status int, message string, body utils.Map[interface{}]) *HttpError { | ||
return &HttpError{ | ||
status: status, | ||
body: body, | ||
message: message, | ||
} | ||
} | ||
|
||
func UnprocessableEntity() *HttpError { | ||
return NewHttpError( | ||
http.StatusUnprocessableEntity, | ||
"unprocessable entity", | ||
utils.Map[interface{}]{ | ||
"hint": "please check the request's body", | ||
}, | ||
) | ||
} | ||
|
||
func BadRequest() *HttpError { | ||
return NewHttpError( | ||
http.StatusBadRequest, | ||
"bad request", | ||
map[string]string{}, | ||
utils.Map[interface{}]{}, | ||
) | ||
} | ||
|
||
func NotFound() *HttpError { | ||
return NewHttpError( | ||
http.StatusNotFound, | ||
"not found", | ||
map[string]string{}, | ||
utils.Map[interface{}]{}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.