Google App Engine : import myfile gives 502 bad gateway - google-app-engine

App runs fine with just the app.yaml, main.py and requiremets.txt (Flask)
I also have a mymodule.py file.
If, in main.py, I do
import mymodule
or
from mymodule import myfunc
I get the 502 Bad Gateway
runs fine locally though..

Please follow the official documentation on :
Specifying Private Dependencies
To use private dependencies:
1.Run pip install -t lib my_module to copy dependencies into a local
folder named lib.
2.Add an empty __init__.py file to the lib directory to make it a
module.
3.Import the module in your app. For example:
import lib.my_module

Related

Service worker not found (404) on deployment: React-Django-Heroku

I'm trying to get my first service worker registered on a Heroku app. Front end has been built using create-react-app, backend uses Django on the same server. I've made a basic "hello world" service worker which works in development, but gives a 404 error in production. I think I might be misunderstanding the basic file system that I'm using in production.
My file system on my local directory essentially looks like this:
root
- public
- index.html
- sw.js
static
- manifest.json
- src
- app.js
- serviceWorker.js
...
serviceWorker.js checks the browser and then attempts to register the service worker:
...
navigator.serviceWorker
.register(`${process.env.PUBLIC_URL}/sw.js`)
.then(registration => {
...
//process.env.PUBLIC_URL returns http://localhost:3000/ in development, and https://www.mydomain.app/ in production
sw.js just contains dummy code:
self.addEventListener('install', function(event) {
console.log("Hello World")
})
When this is deployed to Heroku, yarn build packages everything into a build directory, which looks like:
build
- index.html
- sw.js
- service-worker.js // added automatically by web-pack
- asset-manifest.json // added automatically by web-pack
- precache-manifest.xyz.js // added automatically by web-pack
- static
- css
- js
- manifest.json
In production, this fails to find the file sw.js during the register() function. The same error is happening with the auto-generated service-worker.js, where it works in development but not in production (404):
"TypeError: Failed to register a ServiceWorker for scope ('https://www.mydomain.app/') with script ('https://www.mydomain.app/sw.js'): A bad HTTP response code (404) was received when fetching the script."
I think my use of PUBLIC_URL is correct, as it works in development and production to find manifest.json at<link rel="manifest" href="%PUBLIC_URL%/static/manifest.json" />. I've checked the file system by using heroku run bash and everything seems to be in the right place, what am I missing?
Found the answer here, maybe this will help someone else.
I needed to serve the sw.js file as a view with django urls:
from django.views.generic.base import TemplateView
urlpatterns = [
...
path('sw.js', TemplateView.as_view(template_name="sw.js",
content_type='application/javascript'), name='sw.js'),
...
]

Cannot GET index.html Azure Linux Web App

We created a Linux Web App in Microsoft Azure. The application is static written with React (html and Javascript).
We copied the code into the wwwroot folder, but the application only showing only hostingstart.html and when we try to get page index.html we have this error:
Cannot GET /index.html
We tried with a sample of Azure in GitHub (https://github.com/Azure-Samples/html-docs-hello-world) but the error is the same.
The url is this: https://consoleadmin.azurewebsites.net/index.html
Last week the application was running correctly.
We forget to do something?
MAY 2020 - You don't have to add any javascript files or config files anywhere. Let me explain.
I was facing this exact same issue and wasted 6 hours trying everything including the most popular answer to this question. While the accepted answer is a nice workaround (but requires more work than just adding the index.js file), there's something a simpler than that.
You see, when you just deploy an Azure Web App (or App Service as it is also called), two things happen:
The web app by default points to opt/startup/hostingstart.html
It also puts a hostingstart.html in home/site/wwwroot
When you deploy your code, it replaces hostingstart.html in home/site/wwwroot but the app is still pointing to opt/startup/hostingstart.html. If you want to verify this, try deleting opt/startup/hostingstart.html file and your web app will throw a "CANNOT GET/" error.
So how to change the default pointer? It's simpler than it looks:
Go to Configuration tab on your web app and add the following code to startup script:
pm2 serve /home/site/wwwroot --no-daemon
If this web app is a client-side single-page-app and you're having issues with routing, then add --spa to the above command as follows:
pm2 serve /home/site/wwwroot --no-daemon --spa
This will tell the web app to serve wwwroot folder. And that's it.
Image for reference:
Screenshot explaination
PS: If you only set the startup script without deploying your code, it will still show the hostingstart.html because by default that file lies in the wwwroot folder.
Ok you are gonna love this. This happened to me today also. Same exact thing.
I am pretty sure the azure team flipped a switch somewhere and we fell through a crack.
I found this obscure answer with no votes and it did the trick (with a little extra finagling)
BONUS! this also fixed my router issues I was having only on the deployed site (not local):
Credit: #stormwild: Default documents not serving on node web app hosted on Azure
From #stormwild's post see here:
https://blogs.msdn.microsoft.com/waws/2017/09/08/things-you-should-know-web-apps-and-linux/#NodeHome
Steps:
Go to your azure portal, select your app service and launch ssh
In ssh terminal, navigate via command line to /home/site/wwwroot
create index.js there with the following code:
var express = require('express');
var server = express();
var options = {
index: 'index.html'
};
server.use('/', express.static('/home/site/wwwroot', options));
server.listen(process.env.PORT);
NOTE: Be sure to run npm install --save express also in this folder else your app service will crash on startup
Be sure to restart your app service if it doesn't do so automagically
A workaround, I changed the webapp stack to PHP 7
Another solution would be to add a file called ecoysystem.config.js right next to your index.html file.
module.exports = {
apps: [
{
script: "npx serve -s"
}
]
};
This will tell pm2 to associate all requests to index.html as your app service starts up.
Very helpful information here: https://burkeholland.github.io/posts/static-site-azure/

How to use bunch in golang for Google App Engine or referencing Custom Dependancies

Currently I'm using the bunch to do npm style type builds as well as using symbolic links in my web project to be able to do builds without have to pull from a git repository.
For example my directory structure is like the following
testapp/
.vendor
-controllers
--user_controller.go
-routers
--router.go
-models
--user.go
server.go
Bunchfile
so inside the .vendor/src directory
I also have
.vendor/src/example.com/tgo/testapp/routers
So if i don't want to have to duplicate my folders in the .vendor directory I will use a symbolic link - this works great when I do
ln -s ~/Documents/dev/go/testapp/ ~/Documents/dev/go/testapp/.vendor/src/example.com/tgo/
bunch go build
However for Google App Engine - Trying to see if this will work, haven't been able to figure it out yet.
Here is the code for server.go
package main
import (
"example.com/tgo/testapp/routers"
"github.com/codegangsta/negroni"
"net/http"
"log"
)
func init(){
//For Google App Engine
//settings.Init()
router := routers.InitRoutes()
n := negroni.Classic()
n.UseHandler(router)
http.Handle("/", n)
}
func main() {
router := routers.InitRoutes()
n := negroni.Classic()
n.UseHandler(router)
log.Println("Listening......")
http.ListenAndServe(":3001", n)
}
I actually figured it out the issue has to do with Assigning $GOPATH before you run the upload to App Engine.
So I just have a script that sets the environment Variable $GOPATH to the .vendor directory then run goapp serve or goapp deploy and everything works!
Look forward to moving everything over to app engine!

Angular and Go on heroku

I am trying to put my app on Heroku. I am using angular on the front-end and Go on the backend.
I followed this tutorial http://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku.html
However, when I go to the domain of my heroku app, I get the directory of my app (everything in the git). When I navigate to the /app folder, (where my angular app lives) it shows the app.
I don't want my app to be at
foobar.herokuapp.com/app/#/
I want it to be at
foobar.herokuapp.com
A simplified version of my app directory is:
foobar
- /app
- /server/server.go
- .godir // contains "app"
- Procfile // contains "web: server"
I ran "go get" from inside my /server folder
These work:
$ PORT=5000 demoapp
$ curl -i http://127.0.0.1:5000/
Here is my simple server.go
package main
import (
"github.com/gorilla/handlers"
"log"
"net/http"
"os"
)
func main() {
log.Println("Starting Server")
http.Handle("/", logHandler(http.FileServer(http.Dir("../app/"))))
log.Println("Listening...")
panic(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}
func logHandler(h http.Handler) http.Handler {
return handlers.LoggingHandler(os.Stdout, h)
}
Changing your FileServer directory from "../app/" to "app/" (relative) or "/app/app/" (absolute) should solve the issue.
http.Handle("/", logHandler(http.FileServer(http.Dir("app/"))))
Your project root is the work folder when Heroku executes the Procfile command. It has the absolute path /app which is why ../app brings you back to your project root.
Although your server.go is stored in the ./server subfolder it is still compiled into the project root with package main.

GAE-Sessions: Where is settings.py?

I'm using GAE-Sessions with Google App Engine. In the readme file it says "If you want to gae-sessions with Django, add 'gaesessions.DjangoSessionMiddleware' to your list of MIDDLEWARE_CLASSES in your settings.py file." Problem is I don't have a "settings.py" file, nor is one created when a App Engine project is created. What settings.py file is GAE-Sessions referring to?
I am using gae-sessions with google appengine django. What I have is a file in the same level as app.yaml which I call it appengine config.
The contents are
from gaesessions import SessionMiddleware
import logging
def webapp_add_wsgi_middleware(app):
app = SessionMiddleware(app, cookie_key="ExampleofarandomKEYforcookieswritewhatyouwant")
return app
In the same level I have also placed the gaesessions folder with the __init__.py file.
Download django-nonrel and use the django-admin.py helper to create all the boilerplate project, including settings.py file.
Documentation here.

Resources