mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 17:22:01 +08:00

* [v2] Consolidate AssetServers * [v2] Support starturl for webview on linux and darwin * [v2] Add support for frontend DevServer * [v2] Activate frontend DevServer in svelte template * [website] Add bleeding edge guide for PRs * DoNotMerge: Bump Version for testing Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
|
|
)
|
|
|
|
type RequestRespone struct {
|
|
Body []byte
|
|
MimeType string
|
|
StatusCode int
|
|
}
|
|
|
|
func (r RequestRespone) StatusText() string {
|
|
return http.StatusText(r.StatusCode)
|
|
}
|
|
|
|
func (r RequestRespone) String() string {
|
|
return fmt.Sprintf("Body: '%s', StatusCode: %d", string(r.Body), r.StatusCode)
|
|
}
|
|
|
|
func ProcessRequest(uri string, assets *assetserver.AssetServer, expectedScheme string, expectedHosts ...string) (RequestRespone, error) {
|
|
// Translate URI to file
|
|
file, err := translateUriToFile(uri, expectedScheme, expectedHosts...)
|
|
if err != nil {
|
|
if err == ErrUnexpectedHost {
|
|
body := fmt.Sprintf("expected host one of \"%s\"", strings.Join(expectedHosts, ","))
|
|
return textResponse(body, http.StatusInternalServerError), err
|
|
}
|
|
|
|
return RequestRespone{StatusCode: http.StatusInternalServerError}, err
|
|
}
|
|
|
|
content, mimeType, err := assets.Load(file)
|
|
if err != nil {
|
|
statusCode := http.StatusInternalServerError
|
|
if os.IsNotExist(err) {
|
|
statusCode = http.StatusNotFound
|
|
}
|
|
return RequestRespone{StatusCode: statusCode}, err
|
|
}
|
|
|
|
return RequestRespone{Body: content, MimeType: mimeType, StatusCode: http.StatusOK}, nil
|
|
}
|
|
|
|
func textResponse(body string, statusCode int) RequestRespone {
|
|
if body == "" {
|
|
return RequestRespone{StatusCode: statusCode}
|
|
}
|
|
return RequestRespone{Body: []byte(body), MimeType: "text/plain;charset=UTF-8", StatusCode: statusCode}
|
|
}
|