6 Commits

7 changed files with 93 additions and 21 deletions

View File

@@ -23,14 +23,19 @@ builds:
- "6" - "6"
- "7" - "7"
universal_binaries:
- replace: false
archives: archives:
- format: tar.gz - format: tar.gz
name_template: |- 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 wrap_in_directory: true
format_overrides: format_overrides:
- goos: windows - goos: windows
format: zip format: zip
replacements:
darwin: macos
checksum: checksum:
name_template: "checksums.txt" name_template: "checksums.txt"

View File

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

View File

@@ -1,5 +1,13 @@
# Changelog # 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) ## [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 # 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. # to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/) # 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 # 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 # 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. # follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes. # 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 }} {{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }} imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
- name: "FORCE_HTTPS"
value: "{{ if .Values.casecmp.httpsExamples }}true{{ end }}"
ports: ports:
- name: http - name: http
containerPort: 8080 containerPort: 8080

View File

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

86
main.go
View File

@@ -2,13 +2,16 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"text/template"
"time" "time"
) )
@@ -33,21 +36,36 @@ var (
versionFlag = flag.Bool("v", false, "Print version info") 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) { func indexHandler(w http.ResponseWriter, r *http.Request) {
scheme := "http" scheme := "http"
if r.TLS != nil || *forceHTTPSFlag { if r.TLS != nil || *forceHTTPSFlag {
scheme = "https" scheme = "https"
} }
_, err := fmt.Fprintf(w, `%s %s err := indexTpl.Execute(w, &IndexData{
Name: name,
Case-insensitive string comparison, as an API. Because ¯\_(ツ)_/¯ Version: version,
Scheme: scheme,
Example usage: Host: r.Host,
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)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -65,25 +83,59 @@ https://github.com/jimeh/casecmp
} }
} }
func casecmpHandler(w http.ResponseWriter, r *http.Request) { type JSONData struct {
a := r.FormValue("a") A string `json:"a"`
b := r.FormValue("b") B string `json:"b"`
resp := "0" }
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)) { if strings.EqualFold(string(a), string(b)) {
resp = "1" resp = "1"
} }
_, err := fmt.Fprint(w, resp)
if err != nil { accept := r.Header.Get("Accept")
log.Fatal(err) 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) { func handler(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path { switch r.URL.Path {
case "/": case "/":
if r.Method != "GET" || r.URL.RawQuery != "" { 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 return
} }
indexHandler(w, r) indexHandler(w, r)