-
-
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
1 parent
4c631e9
commit 2212e88
Showing
12 changed files
with
575 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,13 @@ | ||
# These are supported funding model platforms | ||
|
||
# github: [dmitrymomot] | ||
patreon: DmytroMomot | ||
open_collective: # Replace with a single Open Collective username | ||
ko_fi: dmomot | ||
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel | ||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry | ||
liberapay: # Replace with a single Liberapay username | ||
issuehunt: # Replace with a single IssueHunt username | ||
otechie: # Replace with a single Otechie username | ||
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry | ||
custom: ["https://www.buymeacoffee.com/dmomot"] |
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 @@ | ||
--- | ||
name: Bug report | ||
about: Create a report to help us improve | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
|
||
--- | ||
|
||
**Package version** | ||
Version of the package where you found bug or a related commit hash. | ||
|
||
**Describe the bug** | ||
A clear and concise description of what the bug is. | ||
|
||
**Steps to reproduce** | ||
Steps to reproduce the behavior: | ||
|
||
**Expected behavior** | ||
A clear and concise description of what you expected to happen. | ||
|
||
**Additional context** | ||
Add any other context about the problem here. If applicable, add code snippet to help explain your problem, or a link to a repository with the code that reproduces the issue. |
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: Feature request | ||
about: Suggest an idea for this project | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
|
||
--- | ||
|
||
**Is your feature request related to a problem? Please describe.** | ||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] | ||
|
||
**Describe the solution you'd like** | ||
A clear and concise description of what you want to happen. | ||
|
||
**Describe alternatives you've considered** | ||
A clear and concise description of any alternative solutions or features you've considered. | ||
|
||
**Additional context** | ||
Add any other context or screenshots about the feature request here. |
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,71 @@ | ||
# For most projects, this workflow file will not need changing; you simply need | ||
# to commit it to your repository. | ||
# | ||
# You may wish to alter this file to override the set of languages analyzed, | ||
# or to provide custom queries or build logic. | ||
# | ||
# ******** NOTE ******** | ||
# We have attempted to detect the languages in your repository. Please check | ||
# the `language` matrix defined below to confirm you have the correct set of | ||
# supported CodeQL languages. | ||
# | ||
name: "CodeQL" | ||
|
||
on: | ||
push: | ||
branches: ["main"] | ||
pull_request: | ||
# The branches below must be a subset of the branches above | ||
branches: ["main"] | ||
schedule: | ||
- cron: "21 19 * * 5" | ||
|
||
jobs: | ||
analyze: | ||
name: Analyze | ||
runs-on: ubuntu-latest | ||
permissions: | ||
actions: read | ||
contents: read | ||
security-events: write | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
language: ["go"] | ||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] | ||
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
# Initializes the CodeQL tools for scanning. | ||
- name: Initialize CodeQL | ||
uses: github/codeql-action/init@v2 | ||
with: | ||
languages: ${{ matrix.language }} | ||
# If you wish to specify custom queries, you can do so here or in a config file. | ||
# By default, queries listed here will override any specified in a config file. | ||
# Prefix the list here with "+" to use these queries and those in the config file. | ||
|
||
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs | ||
# queries: security-extended,security-and-quality | ||
|
||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java). | ||
# If this step fails, then you should remove it and run the build manually (see below) | ||
- name: Autobuild | ||
uses: github/codeql-action/autobuild@v2 | ||
|
||
# ℹ️ Command-line programs to run using the OS shell. | ||
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun | ||
|
||
# If the Autobuild fails above, remove it and uncomment the following three lines. | ||
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. | ||
|
||
# - run: | | ||
# echo "Run, Build Application using script" | ||
# ./location_of_script_within_repo/buildscript.sh | ||
|
||
- name: Perform CodeQL Analysis | ||
uses: github/codeql-action/analyze@v2 |
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,27 @@ | ||
name: "GolangCI Lint" | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
golangci: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: "1.20" | ||
|
||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: latest |
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 @@ | ||
# Github workflow syntax: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions | ||
name: Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
# branches-ignore: | ||
# - main | ||
# - "releases/**" | ||
pull_request: | ||
# The branches below must be a subset of the branches above | ||
branches: | ||
- main | ||
- "releases/**" | ||
|
||
jobs: | ||
tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: "1.20" | ||
|
||
- name: Install dependencies | ||
run: go mod download -x | ||
|
||
- name: Run tests | ||
run: go test -v -p 1 -count=1 -race -cover ./... |
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,119 @@ | ||
# httpserver | ||
The httpserver package in Go offers a simple and efficient solution for creating, running, and gracefully shutting down HTTP servers, with support for context cancellation and concurrent execution. | ||
|
||
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/dmitrymomot/httpserver)](https://github.com/dmitrymomot/httpserver) | ||
[![Go Reference](https://pkg.go.dev/badge/github.com/dmitrymomot/httpserver.svg)](https://pkg.go.dev/github.com/dmitrymomot/httpserver) | ||
[![License](https://img.shields.io/github/license/dmitrymomot/httpserver)](https://github.com/dmitrymomot/httpserver/blob/main/LICENSE) | ||
|
||
[![Tests](https://github.com/dmitrymomot/httpserver/actions/workflows/tests.yml/badge.svg)](https://github.com/dmitrymomot/httpserver/actions/workflows/tests.yml) | ||
[![CodeQL Analysis](https://github.com/dmitrymomot/httpserver/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/dmitrymomot/httpserver/actions/workflows/codeql-analysis.yml) | ||
[![GolangCI Lint](https://github.com/dmitrymomot/httpserver/actions/workflows/golangci-lint.yml/badge.svg)](https://github.com/dmitrymomot/httpserver/actions/workflows/golangci-lint.yml) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/dmitrymomot/httpserver)](https://goreportcard.com/report/github.com/dmitrymomot/httpserver) | ||
|
||
The `httpserver` package in Go offers a simple and efficient solution for creating, running, and gracefully shutting down HTTP servers. It supports context cancellation and concurrent execution, making it suitable for a wide range of web applications. | ||
|
||
## Features | ||
|
||
- Easy to set up and start HTTP servers | ||
- Graceful shutdown handling | ||
- Context cancellation support | ||
- Concurrency management with `errgroup` | ||
- Lightweight and flexible design | ||
|
||
## Installation | ||
|
||
To install the `httpserver` package, use the following command: | ||
|
||
```bash | ||
go get github.com/dmitrymomot/httpserver | ||
``` | ||
|
||
## Usage | ||
|
||
Here's a basic example of how to use the `httpserver` package: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"github.com/dmitrymomot/httpserver" | ||
"time" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) | ||
defer cancel() | ||
|
||
r := http.NewServeMux() | ||
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("Hello, World!")) | ||
}) | ||
|
||
if err := httpserver.Run(ctx, ":8080", r); err != nil { | ||
panic(err) | ||
} | ||
} | ||
``` | ||
|
||
This will start an HTTP server on port 8080 and respond with "Hello, World!" to `GET /` request. The server will be gracefully shut down after 10 minutes. | ||
|
||
The `httpserver.Run` function is a shortcut for creating a new HTTP server and starting it. It's equivalent to the following code: | ||
|
||
```go | ||
srv := httpserver.NewServer(addr, handler) | ||
if err := srv.Start(ctx); err != nil { | ||
panic(err) | ||
} | ||
``` | ||
|
||
Using with `errgroup`: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"github.com/dmitrymomot/httpserver" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
r := http.NewServeMux() | ||
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("Hello, World!")) | ||
}) | ||
|
||
g, ctx := errgroup.WithContext(ctx) | ||
g.Go(func() error { | ||
return httpserver.Run(ctx, ":8080", r) | ||
}) | ||
g.Go(func() error { | ||
return httpserver.Run(ctx, ":8081", r) | ||
}) | ||
|
||
if err := g.Wait(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
``` | ||
|
||
The code above will start two HTTP servers on ports 8080 and 8081. Both servers will be gracefully shut down when the context is canceled. | ||
|
||
## Contributing | ||
|
||
Contributions to the `httpserver` package are welcome! Here are some ways you can contribute: | ||
|
||
- Reporting bugs | ||
- Additional tests cases | ||
- Suggesting enhancements | ||
- Submitting pull requests | ||
- Sharing the love by telling others about this project | ||
|
||
## License | ||
|
||
This project is licensed under the [Apache 2.0](LICENSE) - see the `LICENSE` file for details. |
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,14 @@ | ||
module github.com/dmitrymomot/httpserver | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.8.4 | ||
golang.org/x/sync v0.6.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= | ||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Oops, something went wrong.