Skip to content

Commit

Permalink
Remove trailing index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
tchssk committed May 31, 2021
1 parent 7010660 commit b87eed9
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions http/codegen/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ func serverFile(genpkg string, svc *expr.HTTPServiceExpr) *codegen.File {
path := filepath.Join(codegen.Gendir, "http", svcName, "server", "server.go")
title := fmt.Sprintf("%s HTTP server", svc.Name())
funcs := map[string]interface{}{
"join": func(ss []string, s string) string { return strings.Join(ss, s) },
"hasWebSocket": hasWebSocket,
"isWebSocketEndpoint": isWebSocketEndpoint,
"viewedServerBody": viewedServerBody,
"mustDecodeRequest": mustDecodeRequest,
"addLeadingSlash": addLeadingSlash,
"join": func(ss []string, s string) string { return strings.Join(ss, s) },
"hasWebSocket": hasWebSocket,
"isWebSocketEndpoint": isWebSocketEndpoint,
"viewedServerBody": viewedServerBody,
"mustDecodeRequest": mustDecodeRequest,
"addLeadingSlash": addLeadingSlash,
"removeTrailingIndexHTML": removeTrailingIndexHTML,
}
sections := []*codegen.SectionTemplate{
codegen.Header(title, "server", []*codegen.ImportSpec{
Expand Down Expand Up @@ -246,6 +247,13 @@ func addLeadingSlash(s string) string {
return "/" + s
}

func removeTrailingIndexHTML(s string) string {
if strings.HasSuffix(s, "/index.html") {
return strings.TrimSuffix(s, "index.html")
}
return s
}

func mapQueryDecodeData(dt expr.DataType, varName string, inc int) map[string]interface{} {
return map[string]interface{}{
"Type": dt,
Expand Down Expand Up @@ -369,10 +377,10 @@ func {{ .MountServer }}(mux goahttp.Muxer, h *{{ .ServerStruct }}) {
http.Redirect(w, r, "{{ .Redirect.URL }}", {{ .Redirect.StatusCode }})
}))
{{- else if .IsDir }}
{{- $filepath := addLeadingSlash .FilePath }}
{{- $filepath := addLeadingSlash (removeTrailingIndexHTML .FilePath) }}
{{ .MountHandler }}(mux, {{ range .RequestPaths }}{{if ne . $filepath }}goahttp.Replace("{{ . }}", "{{ $filepath }}", {{ end }}{{ end }}h.{{ .VarName }}){{ range .RequestPaths }}{{ if ne . $filepath }}){{ end}}{{ end }}
{{- else }}
{{- $filepath := addLeadingSlash .FilePath }}
{{- $filepath := addLeadingSlash (removeTrailingIndexHTML .FilePath) }}
{{ .MountHandler }}(mux, {{ range .RequestPaths }}{{if ne . $filepath }}goahttp.Replace("", "{{ $filepath }}", {{ end }}{{ end }}h.{{ .VarName }}){{ range .RequestPaths }}{{ if ne . $filepath }}){{ end}}{{ end }}
{{- end }}
{{- end }}
Expand Down

0 comments on commit b87eed9

Please sign in to comment.