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 {
|
func (p *MediaURL) URL() string {
|
||||||
|
|
||||||
imageUrl := utils.ApiEndpointUrl()
|
imageURL := utils.ApiEndpointUrl()
|
||||||
if p.Purpose != VideoWeb {
|
if p.Purpose != VideoWeb {
|
||||||
imageUrl.Path = path.Join(imageUrl.Path, "photo", p.MediaName)
|
imageURL.Path = path.Join(imageURL.Path, "photo", p.MediaName)
|
||||||
} else {
|
} 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) {
|
func (p *MediaURL) CachedPath() (string, error) {
|
||||||
|
|
|
@ -2,10 +2,11 @@ package resolvers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/photoview/photoview/api/graphql/auth"
|
|
||||||
"github.com/photoview/photoview/api/utils"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"github.com/photoview/photoview/api/graphql/auth"
|
||||||
|
"github.com/photoview/photoview/api/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type geoMedia struct {
|
type geoMedia struct {
|
||||||
|
|
|
@ -73,12 +73,12 @@ func main() {
|
||||||
Directives: graphqlDirective,
|
Directives: graphqlDirective,
|
||||||
}
|
}
|
||||||
|
|
||||||
apiListenUrl := utils.ApiListenUrl()
|
apiListenURL := utils.ApiListenUrl()
|
||||||
|
|
||||||
endpointRouter := rootRouter.PathPrefix(apiListenUrl.Path).Subrouter()
|
endpointRouter := rootRouter.PathPrefix(apiListenURL.Path).Subrouter()
|
||||||
|
|
||||||
if devMode {
|
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 {
|
} else {
|
||||||
endpointRouter.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
endpointRouter.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||||
w.Write([]byte("photoview api endpoint"))
|
w.Write([]byte("photoview api endpoint"))
|
||||||
|
@ -108,15 +108,16 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if devMode {
|
if devMode {
|
||||||
log.Printf("🚀 Graphql playground ready at %s\n", apiListenUrl.String())
|
log.Printf("🚀 Graphql playground ready at %s\n", apiListenURL.String())
|
||||||
} else {
|
} 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()
|
apiEndpoint := utils.ApiEndpointUrl()
|
||||||
|
|
||||||
log.Printf("Photoview API public endpoint ready at %s\n", apiEndpoint.String())
|
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())
|
log.Printf("Photoview UI public endpoint ready at %s\n", uiEndpoint.String())
|
||||||
|
}
|
||||||
|
|
||||||
if !shouldServeUI {
|
if !shouldServeUI {
|
||||||
log.Printf("Notice: UI is not served by the the api (%s=0)", utils.EnvServeUI.GetName())
|
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 (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
@ -13,6 +13,23 @@ func CORSMiddleware(devMode bool) mux.MiddlewareFunc {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
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}
|
methods := []string{http.MethodGet, http.MethodPost, http.MethodOptions}
|
||||||
requestHeaders := []string{"authorization", "content-type", "content-length", "TokenPassword"}
|
requestHeaders := []string{"authorization", "content-type", "content-length", "TokenPassword"}
|
||||||
responseHeaders := []string{"content-length"}
|
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-Headers", strings.Join(requestHeaders, ", "))
|
||||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
w.Header().Set("Access-Control-Expose-Headers", strings.Join(responseHeaders, ", "))
|
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 {
|
if req.Method != http.MethodOptions {
|
||||||
|
|
|
@ -16,6 +16,9 @@ func WebsocketUpgrader(devMode bool) websocket.Upgrader {
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
uiEndpoint := utils.UiEndpointUrl()
|
uiEndpoint := utils.UiEndpointUrl()
|
||||||
|
if uiEndpoint == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
if r.Header.Get("origin") == "" {
|
if r.Header.Get("origin") == "" {
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -50,6 +50,9 @@ func ApiEndpointUrl() *url.URL {
|
||||||
shouldServeUI := ShouldServeUI()
|
shouldServeUI := ShouldServeUI()
|
||||||
if shouldServeUI {
|
if shouldServeUI {
|
||||||
apiEndpointStr = EnvPublicEndpoint.GetValue()
|
apiEndpointStr = EnvPublicEndpoint.GetValue()
|
||||||
|
if apiEndpointStr == "" {
|
||||||
|
apiEndpointStr = "/"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
apiEndpointUrl, err := url.Parse(apiEndpointStr)
|
apiEndpointUrl, err := url.Parse(apiEndpointStr)
|
||||||
|
@ -70,6 +73,9 @@ func UiEndpointUrl() *url.URL {
|
||||||
shouldServeUI := ShouldServeUI()
|
shouldServeUI := ShouldServeUI()
|
||||||
if shouldServeUI {
|
if shouldServeUI {
|
||||||
uiEndpointStr = EnvPublicEndpoint.GetValue()
|
uiEndpointStr = EnvPublicEndpoint.GetValue()
|
||||||
|
if uiEndpointStr == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uiEndpointUrl, err := url.Parse(uiEndpointStr)
|
uiEndpointUrl, err := url.Parse(uiEndpointStr)
|
||||||
|
|
|
@ -56,6 +56,7 @@ const RECOGNIZE_UNLABELED_FACES_MUTATION = gql`
|
||||||
|
|
||||||
const FaceDetailsButton = styled.button`
|
const FaceDetailsButton = styled.button`
|
||||||
color: ${({ labeled }) => (labeled ? 'black' : '#aaa')};
|
color: ${({ labeled }) => (labeled ? 'black' : '#aaa')};
|
||||||
|
width: 150px;
|
||||||
margin: 12px auto 24px;
|
margin: 12px auto 24px;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
display: block;
|
display: block;
|
||||||
|
|
|
@ -4,7 +4,7 @@ import PropTypes from 'prop-types'
|
||||||
const getProtectedUrl = url => {
|
const getProtectedUrl = url => {
|
||||||
if (url == null) return null
|
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]+)(\/?.*)$/)
|
const tokenRegex = location.pathname.match(/^\/share\/([\d\w]+)(\/?.*)$/)
|
||||||
if (tokenRegex) {
|
if (tokenRegex) {
|
||||||
|
|
Loading…
Reference in New Issue