Compare commits

..

No commits in common. "aa6799fe9975684bf69ef9680da28b6a3da8fb0c" and "d39a65a6460efa8103c7d3cbe17a5e47adc7ff38" have entirely different histories.

4 changed files with 39 additions and 29 deletions

View file

@ -1,8 +1,6 @@
# http-go-status # http-go-status
Inspired by [httpstatu.us](https://github.com/aaronpowell/httpstatus), but written in Go, and is as minimal as possible. No third party dependencies, and less than 100 lines of code in a single file. Inspired by [httpstatu.us](https://github.com/aaronpowell/httpstatus), but written in Go, and is almost as minimal as possible. Only a signle dependency, and less than 100 lines of code in a single file.
Previous versions had a single third-party dependency, but that's no longer necessary.
## Installation ## Installation
@ -24,14 +22,8 @@ To make your request from within a web browser (CORS is supported), you could do
let resp = await fetch("http://localhost:8080/451"); let resp = await fetch("http://localhost:8080/451");
console.log(resp.status); // 451 console.log(resp.status); // 451
console.log(resp.statusText); // Unavailable for Legal Reasons console.log(resp.statusText); // Unavailable for Legal Reasons
resp = await fetch("http://localhost:8080/418/path/to/nothing");
console.log(resp.status); // 418
console.log(resp.statusText); // I'm a teapot
``` ```
If there is no path, if the first element in the path is not an integer, or if it's an integer outside of the acceptable range, a 200 will be returned.
## Deployment ## Deployment
I'll leave this as an exercise for the reader. I'll leave this as an exercise for the reader.

2
go.mod
View file

@ -1,3 +1,5 @@
module codeberg.org/danjones000/http-go-status module codeberg.org/danjones000/http-go-status
go 1.21.4 go 1.21.4
require github.com/julienschmidt/httprouter v1.3.0

2
go.sum
View file

@ -0,0 +1,2 @@
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=

54
main.go
View file

@ -23,14 +23,15 @@ import (
"os" "os"
"strconv" "strconv"
"strings" "strings"
"github.com/julienschmidt/httprouter"
) )
func Status(w http.ResponseWriter, r *http.Request) { func Status(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
header := w.Header() header := w.Header()
header.Set("Access-Control-Allow-Origin", "*") header.Set("Access-Control-Allow-Origin", "*")
status := strings.TrimLeft(r.RequestURI, "/") status := ps.ByName("status")
status, _, _ = strings.Cut(status, "/")
fmt.Printf("Got %s status\n", status) fmt.Printf("Got %s status\n", status)
if status == "" { if status == "" {
status = "200" status = "200"
@ -51,18 +52,37 @@ func Status(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(ret) w.WriteHeader(ret)
} }
func HandleCors(w http.ResponseWriter, r *http.Request) { func GetRouter() *httprouter.Router {
fmt.Println("handling CORS") router := httprouter.New()
if r.Header.Get("Access-Control-Request-Method") != "" {
// Set CORS headers // We don't handle OPTIONS, so that we can support CORS
header := w.Header() methods := [8]string{
header.Set("Access-Control-Allow-Methods", "*") http.MethodGet,
header.Set("Access-Control-Allow-Origin", "*") http.MethodHead,
header.Set("Access-Control-Allow-Headers", "*") http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodTrace}
for _, method := range methods {
router.Handle(method, "/:status", Status)
} }
// Adjust status code to 204 // Handle CORS preflight requests
w.WriteHeader(http.StatusNoContent) router.GlobalOPTIONS = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Access-Control-Request-Method") != "" {
// Set CORS headers
header := w.Header()
header.Set("Access-Control-Allow-Methods", header.Get("Allow"))
header.Set("Access-Control-Allow-Origin", "*")
}
// Adjust status code to 204
w.WriteHeader(http.StatusNoContent)
})
return router
} }
func GetPort() string { func GetPort() string {
@ -80,11 +100,5 @@ func GetPort() string {
func main() { func main() {
port := GetPort() port := GetPort()
fmt.Printf("Listening on %s\n", port) fmt.Printf("Listening on %s\n", port)
log.Fatal(http.ListenAndServe(port, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Fatal(http.ListenAndServe(port, GetRouter()))
if r.Method == http.MethodOptions {
HandleCors(w, r)
} else {
Status(w, r)
}
})))
} }