Skip to content

Commit

Permalink
init gohf
Browse files Browse the repository at this point in the history
  • Loading branch information
a179346 committed Jan 10, 2025
1 parent 58f40bc commit 4f7fdf1
Show file tree
Hide file tree
Showing 15 changed files with 552 additions and 1 deletion.
32 changes: 32 additions & 0 deletions .github/release-drafter.yml
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
20 changes: 20 additions & 0 deletions .github/workflows/release-drafter.yml
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 }}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# gohf
# GoHF

A new Go HTTP framework
23 changes: 23 additions & 0 deletions context.go
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 },
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/gohf-http/gohf

go 1.23.4
12 changes: 12 additions & 0 deletions gohf_responses/dummy_response.go
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) {}
39 changes: 39 additions & 0 deletions gohf_responses/error_response.go
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)
}
31 changes: 31 additions & 0 deletions gohf_responses/io_response.go
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)
}
32 changes: 32 additions & 0 deletions gohf_responses/json_response.go
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)
}
31 changes: 31 additions & 0 deletions gohf_responses/text_response.go
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))
}
33 changes: 33 additions & 0 deletions handler_repository.go
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,
}
}
45 changes: 45 additions & 0 deletions http_handler.go
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)
}
Loading

0 comments on commit 4f7fdf1

Please sign in to comment.