Make photoview work without providing the PHOTOVIEW_PUBLIC_ENDPOINT environment variable
This commit is contained in:
parent
d8d8957499
commit
2c98827db5
|
@ -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) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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())
|
||||
log.Printf("Photoview UI public endpoint ready at %s\n", uiEndpoint.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)))
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package server
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"path"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
@ -13,17 +13,7 @@ func CORSMiddleware(devMode bool) mux.MiddlewareFunc {
|
|||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
methods := []string{http.MethodGet, http.MethodPost, http.MethodOptions}
|
||||
requestHeaders := []string{"authorization", "content-type", "content-length", "TokenPassword"}
|
||||
responseHeaders := []string{"content-length"}
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ", "))
|
||||
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")
|
||||
var uiEndpoint *url.URL = nil
|
||||
|
||||
if devMode {
|
||||
// Development environment
|
||||
|
@ -31,8 +21,23 @@ func CORSMiddleware(devMode bool) mux.MiddlewareFunc {
|
|||
w.Header().Set("Vary", "Origin")
|
||||
} else {
|
||||
// Production environment
|
||||
uiEndpoint := utils.UiEndpointUrl()
|
||||
w.Header().Set("Access-Control-Allow-Origin", uiEndpoint.Scheme+"://"+uiEndpoint.Host)
|
||||
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"}
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ", "))
|
||||
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, ", "))
|
||||
}
|
||||
|
||||
if req.Method != http.MethodOptions {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue