Static files not serving when uploaded in Google app engine (SDK Go) - google-app-engine

I was trying to use google app engine cloud space. My program is running without any problem in localhost, but when I tries to host it in google app engine, static files are not served. The app.yaml is as follows:
application: myTestApp-nn34322
version: 1
runtime: go
threadsafe: true
api_version: go1
handlers:
- url: /static/css
static_dir: ../static/css
mime_type: "text/css"
- url: /static/js
static_dir: ../static/js
mime_type: "text/javascript"
- url: /static/images
static_dir: ../static/images
- url: /.*
script: _go_app

You should put every static resources under the same folder where you put your app.yaml file, else they won't get uploaded to AppEngine.
So you should put your static folder next to app.yaml, and then of course the correct path is simply static/xxx, e.g.:
- url: /static/css
static_dir: static/css
mime_type: "text/css"
- url: /static/js
static_dir: static/js
mime_type: "text/javascript"
- url: /static/images
static_dir: static/images
Note:
If you intend to use these static files from your Go app too (e.g. you want to read their content), that requires special handling because resources matched by static file patterns will not be copied to the application after uploading (static files are served by separate servers). For details, see Google App Engine Golang no such file or directory. Basically you either have to duplicate the static files, or provide the application_readable option to the static file handler.

Related

How to define envs for GAE?

I created web app that consist with two separate parts: server and client. And I want to deploy it to google cloud with app engine.
I already did it, but the main problem is unworking env_variables that I need for correct work of application that I defined in .yaml files.
I don't understand why the env variables doesn't work, If I do it in accordance to docs.
I wrote .yaml file to each part. Here the file structure:
./
--client/
----source_files...
----client.yaml
--server/
----source_files...
----api.yaml
--dispatch.yaml
Here the content of each file.
client.yaml:
runtime: nodejs16
service: default
handlers:
- url: /(.*\..+)$
static_files: build/\1
upload: build/(.*\..+)$
- url: /.*
static_files: build/index.html
upload: build/index.html
env_variables:
API_LINK: "https://gcloudezample-11111.lm.r.appsport.com"
api.yaml:
runtime: nodejs16
service: api
env: standard
env_variables:
MONGO_DB_PWD: "db_password"
dispatch.yaml:
dispatch:
- url: '*/api'
service: api
Then I deployed this parts in .yaml files order above.
The result is working frontend but failed api requests, because the wrong request URL: http://localhost:3001/v1/my-route
Screenshot
It's a common misunderstanding of how a website works. Take 2 sec and think about it: What are doing your frontend part?
Let me help
handlers:
- url: /(.*\..+)$
static_files: build/\1
upload: build/(.*\..+)$
- url: /.*
static_files: build/index.html
upload: build/index.html
It serves only static files. Therefore, only static file means no processing, no instances, no variable definition in the runtime context because only static files are served.
The runtime is on the client browser that run the JS code.
Note: the counterpart great advantage, that the static page serving is free (in term of resource usage because there is no instance up and running to serve your files; the egress is billed)
So now, how to solve this?
Simply build your static files with the API_LINK value INSIDE the static code.

Unable to look up other files in appspot.com - problem with app.yaml

I have these files: index.php, filecallup.js, image.png, userprofile.php deployed in my xxx.appspot.com.
The index.php file is expected to collect some URL query variables and open the userprofile.php.
My problem is that when I click on the link (https://xxx.appspot.com/?user=peter) it still points to the index.php file and doesn't load userprofile.php.
I think it's a problem with my app.yaml file because it renders well in other servers except for GAE.
Here is the app.yaml code:
#Use the PHP 7.3 runtime (BETA) by replacing "php72" below with "php73"
runtime: php72
#entrypoint: serve main.php
service: default
#threadsafe: true
handlers:
# Serve a directory as a static resource.
#- url: /stylesheets
# static_dir: stylesheets
# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
static_files: \1
upload: .+\.(gif|png|jpg)$
- url: /(.+\.(htm|html|css|js))$
static_files: \1
upload: .+\.(htm|html|css|js)$
# Serve your app through a front controller at index.php or public/index.php.
- url: /mail.php
script: auto
Here is the index.php code:
<?php
some codes here`
header('Location:userprofile.php')
>?

Access static json file in google app engine 2.7 standard environment - IOError(2, 'No such file or directory')

We are trying to access .json file inside our python code in Google App Engine.
Here is our app.yaml
service: worker
instance_class: F2
runtime: python27
api_version: 1
threadsafe: true
automatic_scaling:
max_instances: 100
min_pending_latency: 200ms # default value
max_pending_latency: automatic
handlers:
- url: /stylesheets
static_dir: stylesheets
- url: /images
static_dir: images
- url: /javascripts
static_dir: javascripts
- url: /(.*\.(gif|png|jpg|json))$
static_files: static/\1
upload: static/.*\.(gif|png|jpg|json)$
application_readable: true
- url: /static
static_dir: static
application_readable: true
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /.*
script: worker.app
login: admin
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
We are using it in our application like this.
path = os.path.join(os.path.dirname(__file__)[0], '/static/google_web_app_script_credentials.json')
flow = InstalledAppFlow.from_client_secrets_file(path,self.GOOGLESHEETS_SCOPES )
But we are receiving the error.
IOError(2, 'No such file or directory')
We have tried various options of changing path including path = os.path.join(os.path.dirname(file)[0], '../static/google_web_app_script_credentials.json')
When we go to the GCP console -> Debug and review the current code we are able to see the .json file under static folder. This means that the file is there, we are not able to get the application to read it. This service is as "worker".
From your App Engine app, you don't have access to your static files. Move the file to another location within your project, and you will then be able to access it.
Even more importantly, you don't want to store credentials in your static folder! Anyone on the web will be able to access them via the URL!

I am hosting my website (php files) in GAE. The app.yaml file is not routing the pages of my site. Error: No Found

My site has 8 pages and it is written in php: https://intercultural.appspot.com After deploying in GAE, it can load and dislpay the index.php page. However, does not display the other pages when I click on other links: Error: No Found The requested URL /file.php was not found on this server.
Here is the directory of my file: hightlighted in blue are the pages of my site.
enter image description here
Here is my app.yaml file:
runtime: php72 </b>
handlers:
# Serve a directory as a static resource.
- url: /styles
static_dir: styles
# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
static_files: \1
upload: .+\.(gif|png|jpg)$
# Serve your app through a front controller at index.php or public/index.php.
- url: /(.+\.php)$
static_files: static/\1
upload: /(.+\.php)$
# I add the "/" to url bellow as suggested but still get same error
- url: /.*
script: auto
secure: always
Although it displays the site address in the browser, it says: Error: No Found The requested URL /file.php was not found on this server.
What is missing in the app.yaml file to route properly to the other pages of my site?
The URL for your final entry in the app.yaml should be url: /.* (with slash). This is necessary to match URLs of https://intercultural.appspot.com/*. You can see an example in the app.yaml reference.
I already fixed it.
The main problem was in the php version. I thought my code was written in php7.2 but actually it was php5.5, hence, the app.yaml didn't work properly.
Here is the app.yaml file:
This is my app.yaml file
runtime: php55
api_version: 1
handlers:
- url: /styles
static_dir: styles
- url: /(.+\.(gif|png|jpg))$
static_files: \1
upload: .+\.(gif|png|jpg)$
application_readable: true
- url: /
script: index.php
secure: always
- url: /(.+\.php)$
script: \1
secure: always

Static file referenced by handler not found: index.html

After deploying my first app engine app, I'm receiving Static file referenced by handler not found: index.html as an error in the logs. Here is my yaml file:
application: section-14
version: v1
api_version: 1
runtime: python27
threadsafe: yes
handlers:
- url: /bower_components
static_dir: bower_components
- url: /general
static_dir: general
- url: /projects
static_dir: projects
- url: /js
static_dir: js
- url: /styles
static_dir: styles
- url: /elements
static_dir: elements
- url: /images
static_dir: images
#here's the problem
- url: /
static_files: index.html
upload: /
#------------------
- url: /elements.html
static_files: elements.html
upload: /
I can travel to any of the other directories, and files located in those directories, without any problem. Also, if you look below the index entry, the elements.html route works also.
I noticed in other projects that people are defining a /static directory. Is that a requirement? My local environment serves this app without any issues as is.
Naming the directory static isn't a requirement. I do it because it makes the significance of the layout obvious (to me, at least).
Here's an app.yaml fragment from one of my apps that has a static home page and static assets.
handlers:
- url: /style/
static_dir: static/style
- url: /js/
static_dir: static/js
- url: /favicon.ico
static_files: static/favicon.ico
upload: static/favicon.ico
mime_type: image/x-icon
- url: /.+
script: main.app
- url: /
static_files: static/index.html
upload: static/index.html
The order is significant, as is the use of /.+ instead of /.* With the latter, requests for / would get routed to the main.app
Edited to add: Having a url mapping for a static favicon.ico is useful to prevent the request getting routed to your app, since this'll cause App Engine to spin up an instance when one isn't needed.

Resources