6 Commits

7 changed files with 93 additions and 21 deletions

View File

@@ -23,14 +23,19 @@ builds:
- "6"
- "7"
universal_binaries:
- replace: false
archives:
- format: tar.gz
name_template: |-
{{ .ProjectName }}-{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}
{{ .ProjectName }}-{{ .Version }}_{{ .Os }}_{{ if eq .Arch "all" }}universal{{ else }}{{ .Arch }}{{ end }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}
wrap_in_directory: true
format_overrides:
- goos: windows
format: zip
replacements:
darwin: macos
checksum:
name_template: "checksums.txt"

View File

@@ -1,3 +1,3 @@
{
".": "2.0.0"
".": "2.1.0"
}

View File

@@ -1,5 +1,13 @@
# Changelog
## [2.1.0](https://github.com/jimeh/casecmp/compare/v2.0.0...v2.1.0) (2022-11-14)
### Features
* **helm-chart:** add casecmp.httpsExamples value ([406f21f](https://github.com/jimeh/casecmp/commit/406f21f72115884503bfe3e928d993a536eb45b2))
* **json:** add support for JSON request and response types ([0658bad](https://github.com/jimeh/casecmp/commit/0658bad90257aa55bfadea6b6167337b21df1a13))
## [2.0.0](https://github.com/jimeh/casecmp/compare/v1.5.0...v2.0.0) (2022-11-14)

View File

@@ -15,10 +15,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 2.0.0 # x-release-please-version
version: 2.1.0 # x-release-please-version
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: 2.0.0 # x-release-please-version
appVersion: 2.1.0 # x-release-please-version

View File

@@ -33,6 +33,9 @@ spec:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
- name: "FORCE_HTTPS"
value: "{{ if .Values.casecmp.httpsExamples }}true{{ end }}"
ports:
- name: http
containerPort: 8080

View File

@@ -10,6 +10,10 @@ image:
# Overrides the image tag whose default is the chart appVersion.
tag: ""
casecmp:
# Use "https://" scheme in examples.
httpsExamples: false
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

86
main.go
View File

@@ -2,13 +2,16 @@ package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"text/template"
"time"
)
@@ -33,21 +36,36 @@ var (
versionFlag = flag.Bool("v", false, "Print version info")
)
type IndexData struct {
Name string
Version string
Scheme string
Host string
}
var indexTpl = template.Must(template.New("index").Parse(`{{.Name}} {{.Version}}
Case-insensitive string comparison, as an API. Because ¯\_(ツ)_/¯
Example usage:
curl -X POST -F "a=Foo Bar" -F "b=FOO BAR" {{.Scheme}}://{{.Host}}/
curl -X GET "{{.Scheme}}://{{.Host}}/?a=Foo+Bar&b=FOO+BAR"
curl -X GET -H "Accept: application/json" "{{.Scheme}}://{{.Host}}/?a=Foo+Bar&b=FOO+BAR"
curl -X POST -H "Content-Type: application/json" -d '{"a":"Foo Bar","b":"FOO BAR"}' {{.Scheme}}://{{.Host}}/
`))
func indexHandler(w http.ResponseWriter, r *http.Request) {
scheme := "http"
if r.TLS != nil || *forceHTTPSFlag {
scheme = "https"
}
_, err := fmt.Fprintf(w, `%s %s
Case-insensitive string comparison, as an API. Because ¯\_(ツ)_/¯
Example usage:
curl -X POST -F "a=Foo Bar" -F "b=FOO BAR" %s://%s/
curl -X GET "%s://%s/?a=Foo+Bar&b=FOO+BAR"
`,
name, version, scheme, r.Host, scheme, r.Host)
err := indexTpl.Execute(w, &IndexData{
Name: name,
Version: version,
Scheme: scheme,
Host: r.Host,
})
if err != nil {
log.Fatal(err)
}
@@ -65,25 +83,59 @@ https://github.com/jimeh/casecmp
}
}
func casecmpHandler(w http.ResponseWriter, r *http.Request) {
a := r.FormValue("a")
b := r.FormValue("b")
resp := "0"
type JSONData struct {
A string `json:"a"`
B string `json:"b"`
}
func casecmpHandler(w http.ResponseWriter, r *http.Request) error {
var a, b string
contentType := r.Header.Get("Content-Type")
if strings.Contains(contentType, "application/json") {
body, err := io.ReadAll(r.Body)
if err != nil {
return err
}
d := JSONData{}
err = json.Unmarshal(body, &d)
if err != nil {
return err
}
a = d.A
b = d.B
} else {
a = r.FormValue("a")
b = r.FormValue("b")
}
resp := "0"
if strings.EqualFold(string(a), string(b)) {
resp = "1"
}
_, err := fmt.Fprint(w, resp)
if err != nil {
log.Fatal(err)
accept := r.Header.Get("Accept")
if strings.Contains(accept, "application/json") {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, err := fmt.Fprintf(w, `{"result":%s}`, resp)
return err
}
_, err := fmt.Fprint(w, resp)
return err
}
func handler(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
if r.Method != "GET" || r.URL.RawQuery != "" {
casecmpHandler(w, r)
err := casecmpHandler(w, r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprint(w, err.Error())
}
return
}
indexHandler(w, r)