-
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
15 changed files
with
552 additions
and
1 deletion.
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,32 @@ | ||
name-template: 'v$RESOLVED_VERSION 🌈' | ||
tag-template: 'v$RESOLVED_VERSION' | ||
categories: | ||
- title: '🚀 Features' | ||
label: 'feature' | ||
- title: '🐛 Bug Fixes' | ||
label: 'fix' | ||
- title: '📃 Documents' | ||
label: 'docs' | ||
- title: '🧰 Maintenance' | ||
label: 'chore' | ||
- title: '🔬 Others' | ||
labels: | ||
- 'test' | ||
- 'ci' | ||
change-template: '- $TITLE @$AUTHOR (#$NUMBER)' | ||
change-title-escapes: '\<*_&' | ||
version-resolver: | ||
major: | ||
labels: | ||
- 'major' | ||
minor: | ||
labels: | ||
- 'minor' | ||
patch: | ||
labels: | ||
- 'patch' | ||
default: patch | ||
template: | | ||
## Changes | ||
$CHANGES |
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,20 @@ | ||
name: Release Drafter | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
update_release_draft: | ||
permissions: | ||
contents: write | ||
pull-requests: read | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: release-drafter/release-drafter@v6 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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,2 +1,3 @@ | ||
# gohf | ||
# GoHF | ||
|
||
A new Go HTTP framework |
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,23 @@ | ||
package gohf | ||
|
||
type Response interface { | ||
Send(ResponseWriter, *Request) | ||
} | ||
|
||
type HandlerFunc func(c *Context) Response | ||
|
||
type NextFunc func() Response | ||
|
||
type Context struct { | ||
Res ResponseWriter | ||
Req *Request | ||
Next NextFunc | ||
} | ||
|
||
func newContext(res ResponseWriter, req *Request) *Context { | ||
return &Context{ | ||
Res: res, | ||
Req: req, | ||
Next: func() Response { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/gohf-http/gohf | ||
|
||
go 1.23.4 |
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,12 @@ | ||
package gohf_responses | ||
|
||
import "github.com/gohf-http/gohf" | ||
|
||
type DummyResponse struct { | ||
} | ||
|
||
func NewDummyResponse() DummyResponse { | ||
return DummyResponse{} | ||
} | ||
|
||
func (response DummyResponse) Send(_ gohf.ResponseWriter, _ *gohf.Request) {} |
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,39 @@ | ||
package gohf_responses | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/gohf-http/gohf" | ||
) | ||
|
||
type ErrorResponse[T interface{}] struct { | ||
Status int `json:"status"` | ||
Message string `json:"message"` | ||
Err T `json:"err"` | ||
} | ||
|
||
func NewErrorResponse[TError error](statusCode int, err TError) ErrorResponse[TError] { | ||
return ErrorResponse[TError]{ | ||
Status: statusCode, | ||
Message: err.Error(), | ||
Err: err, | ||
} | ||
} | ||
|
||
func (response ErrorResponse[T]) Error() string { | ||
return fmt.Sprintf("default http error [%d]: %s", response.Status, response.Message) | ||
} | ||
|
||
func (response ErrorResponse[T]) Send(res gohf.ResponseWriter, req *gohf.Request) { | ||
if errors.Is(req.RootContext().Err(), context.Canceled) { | ||
return | ||
} | ||
|
||
res.SetHeader("Content-Type", "application/json") | ||
res.SetStatus(response.Status) | ||
//nolint:errcheck | ||
json.NewEncoder(res).Encode(response) | ||
} |
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,31 @@ | ||
package gohf_responses | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
|
||
"github.com/gohf-http/gohf" | ||
) | ||
|
||
type IoResponse struct { | ||
Status int | ||
Reader io.Reader | ||
} | ||
|
||
func NewIoResponse(statusCode int, reader io.Reader) IoResponse { | ||
return IoResponse{ | ||
Status: statusCode, | ||
Reader: reader, | ||
} | ||
} | ||
|
||
func (response IoResponse) Send(res gohf.ResponseWriter, req *gohf.Request) { | ||
if errors.Is(req.RootContext().Err(), context.Canceled) { | ||
return | ||
} | ||
|
||
res.SetStatus(response.Status) | ||
//nolint:errcheck | ||
io.Copy(res, response.Reader) | ||
} |
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,32 @@ | ||
package gohf_responses | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
|
||
"github.com/gohf-http/gohf" | ||
) | ||
|
||
type JsonResponse[T interface{}] struct { | ||
Status int | ||
Data T | ||
} | ||
|
||
func NewJsonResponse[T interface{}](statusCode int, data T) JsonResponse[T] { | ||
return JsonResponse[T]{ | ||
Status: statusCode, | ||
Data: data, | ||
} | ||
} | ||
|
||
func (response JsonResponse[T]) Send(res gohf.ResponseWriter, req *gohf.Request) { | ||
if errors.Is(req.RootContext().Err(), context.Canceled) { | ||
return | ||
} | ||
|
||
res.SetHeader("Content-Type", "application/json") | ||
res.SetStatus(response.Status) | ||
//nolint:errcheck | ||
json.NewEncoder(res).Encode(response.Data) | ||
} |
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,31 @@ | ||
package gohf_responses | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/gohf-http/gohf" | ||
) | ||
|
||
type TextResponse struct { | ||
Status int | ||
Text string | ||
} | ||
|
||
func NewTextResponse(statusCode int, text string) TextResponse { | ||
return TextResponse{ | ||
Status: statusCode, | ||
Text: text, | ||
} | ||
} | ||
|
||
func (response TextResponse) Send(res gohf.ResponseWriter, req *gohf.Request) { | ||
if errors.Is(req.RootContext().Err(), context.Canceled) { | ||
return | ||
} | ||
|
||
res.SetHeader("Content-Type", "text/plain") | ||
res.SetStatus(response.Status) | ||
//nolint:errcheck | ||
res.Write([]byte(response.Text)) | ||
} |
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,33 @@ | ||
package gohf | ||
|
||
type handlerRepository struct { | ||
handlers []*handler | ||
} | ||
|
||
func newHandlerRepository() *handlerRepository { | ||
return &handlerRepository{} | ||
} | ||
|
||
func (hr *handlerRepository) addHandler(f HandlerFunc, owner *Router, pattern string, all bool) { | ||
hr.handlers = append(hr.handlers, newHandler(f, owner, pattern, all)) | ||
} | ||
|
||
func (hr *handlerRepository) getHandlers() []*handler { | ||
return hr.handlers | ||
} | ||
|
||
type handler struct { | ||
f HandlerFunc | ||
owner *Router | ||
pattern string | ||
all bool | ||
} | ||
|
||
func newHandler(f HandlerFunc, owner *Router, pattern string, all bool) *handler { | ||
return &handler{ | ||
f: f, | ||
owner: owner, | ||
pattern: pattern, | ||
all: all, | ||
} | ||
} |
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,45 @@ | ||
package gohf | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type httpHandler struct { | ||
handlerFuncs []HandlerFunc | ||
} | ||
|
||
func newHttpHandler() *httpHandler { | ||
return &httpHandler{} | ||
} | ||
|
||
func (httpHandler *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
res := newResponseWriter(w) | ||
req := newRequest(r) | ||
c := newContext(res, req) | ||
|
||
var handle func(idx int) Response | ||
handle = func(idx int) Response { | ||
if idx == len(httpHandler.handlerFuncs) { | ||
return nil | ||
} | ||
|
||
c.Next = func() Response { return handle(idx + 1) } | ||
|
||
return httpHandler.handlerFuncs[idx](c) | ||
} | ||
|
||
if response := handle(0); response != nil { | ||
response.Send(c.Res, c.Req) | ||
} | ||
} | ||
|
||
func (httpHandler *httpHandler) addHandlerFunc(handlerFunc HandlerFunc) { | ||
httpHandler.handlerFuncs = append( | ||
httpHandler.handlerFuncs, | ||
handlerFunc, | ||
) | ||
} | ||
|
||
func (httpHandler *httpHandler) len() int { | ||
return len(httpHandler.handlerFuncs) | ||
} |
Oops, something went wrong.