How to import local Go package in GAE - google-app-engine

How to import local packages in Golang + GAE?
I wanna something like this:
app/
-app.yaml
-/my_app
--my_app.go
--/package1
---package1.go
Listing of my_app.go:
package my_app
import (
"http"
"./package1"
)
func init() {
http.HandleFunc("/", package1.index)
}
Listing of package1.go:
package package1
import (
"http"
"fmt"
)
func index (w http.ResponseWriter, r * http.Request) {
fmt.Fprint(w, "I'm index page =) ")
}
I this case I have an error like:
/path/to/project/my_app/my_app.go:5: can't find import: ./package1
2011/11/03 10:50:51 go-app-builder: Failed building app: failed running 6g: exit status 1
Thanks for help.

As noted in the comments to dupoxy's answer, the way to import a local package in the given situation is to import as "my_app/package1":
import (
"http"
"my_app/package1"
)

You either need to link or copy the packages to your application directory. The path relative to the root of the application directory should match the import path. To use package1, you should configure your app directory to look like this:
app.yaml
yourapp/yourapp.go
package1/package1.go
from https://groups.google.com/d/msg/golang-nuts/coEvrWIJGTs/75GzcefKVcIJ

Related

Vite error with optimized info should be defined when use GLTFLoader in React(ThreeJS)

I use vite to build one react environment. When I import GLTFLoader from the module, and vite commandline mentioned that:
Vite Error, /node_modules/.vite/deps/three_examples_jsm_loaders_GLTFLoader.js?v=9d4ee121 optimized info should be defined
Here's my code:
import * as Three from "three"
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
[...]
const gltfLoader = new GLTFLoader()
gltfLoader.setPath('public/')
gltfLoader.load('000.hdr', gltf => {
gltf.scene.scale.set(0.2,0.2,0.2)
scenes.add(gltf.scene)
})
Here's the error message:
For others in the future, YMMV: deleting node_modules and pnpm-lock.yaml solved the issue.
Already solve this question.
When the commandline mentioned vite error, just need to close dev-server, then reload. Vite would clean the cache tmp files in .vite.
this is usually caused by lockfiles or node_modules/.cache/vite (or maybe in your case node_modules/.vite)

ERROR TS6054: File '~lib/#tensorflow/tfjs-node.ts' not found

I'm trying to use AssemblyScript to build a WebAssembly inference engine for a TensorFlow.js model that I have.
I started with essentially the quickstart AssemblyScript app (which works great) and then simply added #tensorflow/tfjs to my dependencies:
$ npm install --save #tensorflow/tfjs
and added an import to assembly/index.ts:
import * as tf from "#tensorflow/tfjs";
Full code here on Github
This results in an error when I build it:
$ npm run asbuild
> test-assemblyscript#1.0.0 asbuild
> npm run asbuild:untouched && npm run asbuild:optimized
> test-assemblyscript#1.0.0 asbuild:untouched
> asc assembly/index.ts --target debug
ERROR TS6054: File '~lib/#tensorflow/tfjs.ts' not found.
import * as tf from "#tensorflow/tfjs";
~~~~~~~~~~~~~~~~~~
in assembly/index.ts(1,21)
FAILURE 1 parse error(s)
Am I misunderstanding the import syntax? I am puzzled why it would be looking in ~lib for this versus node_modules.
If you are sure the module you are trying to import is assemblyscript files, you can import with something like this ./node_modules/#tenderflow/tfjs.
I am not sure if #tensorflow has assembly files built, but I did that on assemblyscript-json for assemblyscript#0.19.8 (0.25.2 doesn't have that problem).
For example
assemblyscript-json has exported assemblyscript files from its package, so I can do this (ref)
import { JSON } from './node_modules/assemblyscript-json/assembly';
export function formatJsonString(jsonString: string): string {
const jsonObj: JSON.Obj = <JSON.Obj>JSON.parse(jsonString);
return jsonObj.stringify();
}

Context value changed when passed out of main.go on Google App Engine

There's a problem where I don't know why a context.Context was changed once I pass it to a different package on Google App Engine.
The following code works fine when running on App Engine:
package main
import (
"net/http"
"log"
"google.golang.org/appengine"
)
func main() {
http.HandleFunc("/", myHandler)
appengine.Main()
}
func myHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
account, err := appengine.ServiceAccount(ctx)
if err != nil {
log.Println("[myHandler] error:", err)
} else {
log.Println("[myHandler] ServiceAccount:", account)
}
w.Write([]byte("ok"))
}
I could retrieve the ServiceAccount successfully when accessing /, and everything was good.
However, when I passed the context from main.go to another package, the function call didn't work. The following was added to main.go:
import (
// other stuff
"github.com/adlerhsieh/q_context/handlers"
)
func main() {
http.HandleFunc("/", myHandler)
appengine.Main()
}
func myHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
account, err := appengine.ServiceAccount(ctx)
if err != nil {
log.Println("[myHandler] error:", err)
} else {
log.Println("[myHandler] ServiceAccount:", account)
}
handlers.AnotherFunc(ctx) // <--- added this
w.Write([]byte("ok"))
}
Another package:
package handlers
import (
"log"
"context"
"google.golang.org/appengine"
)
func AnotherFunc(ctx context.Context) {
account, err := appengine.ServiceAccount(ctx)
if err != nil {
log.Println("[AnotherFunc] error:", err)
} else {
log.Println("[AnotherFunc] ServiceAccount:", account)
}
}
When I ran it on App Engine, the log said:
2019/09/04 09:36:30 [myHandler] ServiceAccount: myaccount#gmail.com
2019/09/04 09:36:30 [AnotherFunc] error: not an App Engine context
The function calls are the same, but just in different packages. I dug in the package itself and found that it uses the key here (which leads to here) to setup the context. And here to check whether that value was setup properly. However, that value seem to be modified/changed so that the second function call couldn't get it. Even if I omitted the first function call and went straight to the second one, it still has the same error.
Any idea why context object was modified when passing to another package?
The following is my app.yaml:
runtime: go111
service: default
instance_class: F1
automatic_scaling:
min_idle_instances: 0
max_idle_instances: automatic
min_pending_latency: automatic
max_pending_latency: automatic
max_concurrent_requests: 30
handlers:
- url: /.*
script: auto
login: admin
nobuild_files:
- vendor
env_variables:
ENV: 'dev'
GO111MODULE: 'off'
Here is the GitHub repo link.
Thank you!
It turns out that my code actually worked. It's because of some other operation error.
However, I'll just post the issue that actually caused it so it can help those who have the same issue.
With the new go111 runtime, it treats packages from non-root directory or its subdirectories as a different type of package. This caused the problem with "not an App Engine context". I'll just call it an "outcast" package for now (cause I'm not entirely sure why's that).
For example:
- appengine
- main.go
- handlers
- handlers.go <-- this is a regular package
- appengine
- main.go
- handlers
- handlers.go < -- this is an outcast package
An outcast package would have issues handling context.Context generated from App Engine, as pointed out in my question.
The mechanism of App Engine knowing that the context is created from App Engine, is using a built-in value that can only be retrieved from its internal package (with an un-exported pointer-string key). When passing the context to an outcast package, we can no longer retrieve the value from the context. It's still a mystery for me that why the value disappeared, but it's probably because of some Go compiling mechanism.
The solution would be moving the main.go to the top-level directory in the project, so that there would be no outcast package anywhere.

Google App Engine cant build

Im having problem with building my project, i.e, "goapp serve".
I get this message:
Failed to build Go application: (Executed command: /Users/simon/go_appengine/goroot/bin/go-app-builder -app_base /Users/simon/Programming/golang_projects/src/googleglass_backend -arch 6 -dynamic -goroot /Users/simon/go_appengine/goroot -gopath /Users/simon/Programming/golang_projects -nobuild_files ^^$ -incremental_rebuild -unsafe -print_extras_hash handlers/mainpage.go types/user.go types/data.go handlers/utils.go server.go)
2017/01/16 14:04:30 go-app-builder: Failed parsing input: app file data.go conflicts with same file imported from GOPATH
My project structure looks like this:
golang_projects
bin
...
pkg
...
src
googleglass_backend
app.yaml
handlers
utils.go
mainpage.go
index.yaml
server.go
types
data.go
user.go
My GOPATH looks like this:
/Users/simon/Programming/golang_projects
I dont know what data.go could be conflicting with?
My server.go file:
package main
import (
"googleglass_backend/handlers"
"googleglass_backend/types"
"github.com/gorilla/mux"
"encoding/gob"
"net/http"
"github.com/rs/cors"
)
func init() {
router := mux.NewRouter()
gob.Register(types.User{})
//For all includes in html files
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("web/static/"))))
router.HandleFunc("/", handlers.IndexTemplate)
handler := cors.Default().Handler(router)
http.Handle("/", handler)
}
Contents of data.go:
package types
import (
"time"
)
type Data struct {
xaxis int
yaxis int
beacon_1 int
beacon_2 int
beacon_3 int
RegisterTime time.Time
}

Querying Google AppEngine's Datastore using PHP (through Quercus) and low-level API isn't working

When I query Google AppEngine's datastore using PHP(through Quercus) and the low-level data-access API for an entity, I get an error that the entity doesn't exist, even though I've put it in the datastore previously.
The specific error is "com.caucho.quercus.QuercusException: com.google.appengine.api.datastore.DatastoreService.get: No entity was found matching the key: Test(value1)"
Here's the relevant code -
<?php
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
$testkey = KeyFactory::createKey("Test", "value1");
$ent = new Entity($testkey);
$ent->setProperty("field1", "value2");
$ent->setProperty("field2", "value3");
$dataService = DatastoreServiceFactory::getDatastoreService();
$dataService->put($ent);
echo "Data entered";
try
{
$ent = $dataService->get($testkey);
echo "Data queried - the results are \n";
echo "Field1 has value ".$ent->getProperty("field1")."\n";
echo "Field2 has value ".$ent->getProperty("field2")."\n";
}
catch(EntityNotFoundException $e)
{
echo("<br/>Entity test not found.");
echo("<br/>Stack Trace is:\n");
echo($e);
}
And here's the detailed stack-trace - link.
This same code runs fine in Java (of course after changing the syntax). I wonder what's wrong.
Thanks.
I have found the solution to my problem. It was caused by missing dependencies and I solved it by using the prepackaged PHP Wordpress application available here.
One thing is to be noted. The package overlooked a minor issue in that all files other than the src/ directory need to be in a war/ directory which stays alongside the src/ directory (this as per appengine conventions as mentioned on its documentation). So I organized the files thus myself, put the above PHP file in the war/ directory, and it's working fine on the appengine.

Resources