1
Fork 0

Make photoview work without providing the PHOTOVIEW_PUBLIC_ENDPOINT environment variable

This commit is contained in:
viktorstrate 2021-03-01 22:03:57 +01:00
parent d8d8957499
commit 2c98827db5
No known key found for this signature in database
GPG Key ID: 3F855605109C1E8A
8 changed files with 47 additions and 30 deletions

View File

@ -74,14 +74,14 @@ type MediaURL struct {
func (p *MediaURL) URL() string {
imageUrl := utils.ApiEndpointUrl()
imageURL := utils.ApiEndpointUrl()
if p.Purpose != VideoWeb {
imageUrl.Path = path.Join(imageUrl.Path, "photo", p.MediaName)
imageURL.Path = path.Join(imageURL.Path, "photo", p.MediaName)
} else {
imageUrl.Path = path.Join(imageUrl.Path, "video", p.MediaName)
imageURL.Path = path.Join(imageURL.Path, "video", p.MediaName)
}
return imageUrl.String()
return imageURL.String()
}
func (p *MediaURL) CachedPath() (string, error) {

View File

@ -2,10 +2,11 @@ package resolvers
import (
"context"
"github.com/photoview/photoview/api/graphql/auth"
"github.com/photoview/photoview/api/utils"
"os"
"path"
"github.com/photoview/photoview/api/graphql/auth"
"github.com/photoview/photoview/api/utils"
)
type geoMedia struct {

View File

@ -73,12 +73,12 @@ func main() {
Directives: graphqlDirective,
}
apiListenUrl := utils.ApiListenUrl()
apiListenURL := utils.ApiListenUrl()
endpointRouter := rootRouter.PathPrefix(apiListenUrl.Path).Subrouter()
endpointRouter := rootRouter.PathPrefix(apiListenURL.Path).Subrouter()
if devMode {
endpointRouter.Handle("/", handler.Playground("GraphQL playground", path.Join(apiListenUrl.Path, "/graphql")))
endpointRouter.Handle("/", handler.Playground("GraphQL playground", path.Join(apiListenURL.Path, "/graphql")))
} else {
endpointRouter.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("photoview api endpoint"))
@ -108,15 +108,16 @@ func main() {
}
if devMode {
log.Printf("🚀 Graphql playground ready at %s\n", apiListenUrl.String())
log.Printf("🚀 Graphql playground ready at %s\n", apiListenURL.String())
} else {
log.Printf("Photoview API endpoint listening at %s\n", apiListenUrl.String())
log.Printf("Photoview API endpoint listening at %s\n", apiListenURL.String())
uiEndpoint := utils.UiEndpointUrl()
apiEndpoint := utils.ApiEndpointUrl()
log.Printf("Photoview API public endpoint ready at %s\n", apiEndpoint.String())
if uiEndpoint := utils.UiEndpointUrl(); uiEndpoint != nil {
log.Printf("Photoview UI public endpoint ready at %s\n", uiEndpoint.String())
}
if !shouldServeUI {
log.Printf("Notice: UI is not served by the the api (%s=0)", utils.EnvServeUI.GetName())
@ -124,5 +125,5 @@ func main() {
}
log.Panic(http.ListenAndServe(":"+apiListenUrl.Port(), handlers.CompressHandler(rootRouter)))
log.Panic(http.ListenAndServe(":"+apiListenURL.Port(), handlers.CompressHandler(rootRouter)))
}

View File

@ -2,7 +2,7 @@ package server
import (
"net/http"
"path"
"net/url"
"strings"
"github.com/gorilla/mux"
@ -13,6 +13,23 @@ func CORSMiddleware(devMode bool) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var uiEndpoint *url.URL = nil
if devMode {
// Development environment
w.Header().Set("Access-Control-Allow-Origin", req.Header.Get("origin"))
w.Header().Set("Vary", "Origin")
} else {
// Production environment
uiEndpoint = utils.UiEndpointUrl()
if uiEndpoint != nil {
// Only allow CORS if UI endpoint is defined
w.Header().Set("Access-Control-Allow-Origin", uiEndpoint.Scheme+"://"+uiEndpoint.Host)
}
}
corsEnabled := devMode || uiEndpoint != nil
if corsEnabled {
methods := []string{http.MethodGet, http.MethodPost, http.MethodOptions}
requestHeaders := []string{"authorization", "content-type", "content-length", "TokenPassword"}
responseHeaders := []string{"content-length"}
@ -21,18 +38,6 @@ func CORSMiddleware(devMode bool) mux.MiddlewareFunc {
w.Header().Set("Access-Control-Allow-Headers", strings.Join(requestHeaders, ", "))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Expose-Headers", strings.Join(responseHeaders, ", "))
endpoint := utils.ApiEndpointUrl()
endpoint.Path = path.Join(endpoint.Path, "graphql")
if devMode {
// Development environment
w.Header().Set("Access-Control-Allow-Origin", req.Header.Get("origin"))
w.Header().Set("Vary", "Origin")
} else {
// Production environment
uiEndpoint := utils.UiEndpointUrl()
w.Header().Set("Access-Control-Allow-Origin", uiEndpoint.Scheme+"://"+uiEndpoint.Host)
}
if req.Method != http.MethodOptions {

View File

@ -16,6 +16,9 @@ func WebsocketUpgrader(devMode bool) websocket.Upgrader {
return true
} else {
uiEndpoint := utils.UiEndpointUrl()
if uiEndpoint == nil {
return true
}
if r.Header.Get("origin") == "" {
return true

View File

@ -50,6 +50,9 @@ func ApiEndpointUrl() *url.URL {
shouldServeUI := ShouldServeUI()
if shouldServeUI {
apiEndpointStr = EnvPublicEndpoint.GetValue()
if apiEndpointStr == "" {
apiEndpointStr = "/"
}
}
apiEndpointUrl, err := url.Parse(apiEndpointStr)
@ -70,6 +73,9 @@ func UiEndpointUrl() *url.URL {
shouldServeUI := ShouldServeUI()
if shouldServeUI {
uiEndpointStr = EnvPublicEndpoint.GetValue()
if uiEndpointStr == "" {
return nil
}
}
uiEndpointUrl, err := url.Parse(uiEndpointStr)

View File

@ -56,6 +56,7 @@ const RECOGNIZE_UNLABELED_FACES_MUTATION = gql`
const FaceDetailsButton = styled.button`
color: ${({ labeled }) => (labeled ? 'black' : '#aaa')};
width: 150px;
margin: 12px auto 24px;
text-align: center;
display: block;

View File

@ -4,7 +4,7 @@ import PropTypes from 'prop-types'
const getProtectedUrl = url => {
if (url == null) return null
const imgUrl = new URL(url)
const imgUrl = new URL(url, location.origin)
const tokenRegex = location.pathname.match(/^\/share\/([\d\w]+)(\/?.*)$/)
if (tokenRegex) {