Skip to content

Commit

Permalink
respond: Add http.ServeFile and http.ServeContent wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jille committed Dec 9, 2021
1 parent 9a5f842 commit ce53203
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions respond/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package respond

import (
"io"
"net/http"
"time"

"github.com/Jille/convreq/internal"
)

type respondFile struct {
name string
}

// Respond implements convreq.HttpResponse.
func (rf respondFile) Respond(w http.ResponseWriter, r *http.Request) error {
http.ServeFile(w, r, rf.name)
return nil
}

// ServeFile uses http.ServeFile() to serve a file.
func ServeFile(name string) internal.HttpResponse {
return respondFile{name}
}

type respondContent struct {
name string
modtime time.Time
content io.ReadSeeker
}

// Respond implements convreq.HttpResponse.
func (rc respondContent) Respond(w http.ResponseWriter, r *http.Request) error {
http.ServeContent(w, r, rc.name, rc.modtime, rc.content)
return nil
}

// ServeContent uses http.ServeContent() to serve content.
func ServeContent(name string, modtime time.Time, content io.ReadSeeker) internal.HttpResponse {
return respondContent{name, modtime, content}
}

0 comments on commit ce53203

Please sign in to comment.