How to create proxy for golang package sources - go

Let's say I have all source code for module hosted on github.com/example/my-module. But I want to create a proxy hosted on example.com/my-module to redirect all requests to github.com/example/my-module and use my custom domain name in imports:
package main
import "example.com/my-module"
instead of:
package main
import "github.com/example/my-module"
According to docs, I can host git repository on example.com/my-module, but it would be overhead for me.
How can I set up package source code proxy for Go on custom domain? Can I just use HTTP reverse proxy for that or it doesn't work?

If you just need it for yourself, or your package's users are open to an extra step to including the package in the go.mod, you could use the "replace" function of the mod file: https://thewebivore.com/using-replace-in-go-mod-to-point-to-your-local-module/

Related

How can you have two copies of same package that cannot be used for having different namespaces

Here is a quote from a blog post:
Enforce vanity URLs go get supports getting packages by a URL that is
different than the URL of the package's repo. These URLs are called
vanity URLs and require you to serve a page with specific meta tags
the Go tools recognize. You can serve a package with a custom domain
and path using vanity URLs.
For example,
$ go get cloud.google.com/go/datastore
checks out the source code from
https://code.googlesource.com/gocloud behind the scenes and puts it in
your workspace under $GOPATH/src/cloud.google.com/go/datastore.
Given code.googlesource.com/gocloud is already serving this package,
would it be possible to go get the package from that URL? The answer
is no, if you enforce the vanity URL.
To do that, add an import statement to the package. The go tool will
reject any import of this package from any other path and will display
a friendly error to the user. If you don't enforce your vanity URLs,
there will be two copies of your package that cannot work together due
to the different namespace.
package datastore // import "cloud.google.com/go/datastore"
Can someone explain what the last line means?
If you don't enforce your vanity URLs,
there will be two copies of your package that cannot work together due
to the different namespace.
Or demonstrate it in an example?
It's pretty simple.
If your package is hosted at, say github.com/foo/bar, and you have vanity URL foo.com/bar, then someone could do both:
import "github.com/foo/bar"
and
import "foo.com/bar"
This will be problematic if you have different files or packages that import the same package at different paths.
Imagine these two files in the same package:
// foo.go
package foo
import "github.com/foo/bar"
func frobnicate(x *bar.Something) { /* ... */ }
// bar.go
package foo
import "foo.com/bar"
func widget() {
x := *bar.Something{}
frobnicate(x) // compilation error: cannot use x (type foo.com/bar.Something) as type github.com/foo/bar.Something
}

How to set go module path of private repository

I want to set my go module path to example.com/myrepo instead of github.com/myusername/myrepo such that I am able to import in inside another repository.
for example, if my go.mod looks like this
module example.com/myrepo
go 1.13
how will I make go get example.com/myrepo work?
I am getting the following on go get example.com/myrepo
unrecognized import path "example.com/myrepo" (parse https://example.com/myrepo?go-get=1: no go-import meta tags ())
Given I am the owner of example.com how can I do this?
it is called vanity import paths.
In addition to the common hosting sites (GitHub, Bitbucket, etc) and custom VCS URLs (.git, .hg, etc) known to the go command, this mechanism can be used to point a custom URL to any of the services.
you must be looking for this https://sagikazarmark.hu/blog/vanity-import-paths-in-go/.

How to make a HTTP rest call in AWS lambda using python?

To make an http call using python my way was to use requests.
But requests is not installed in lambda context. Using import requests resulted in module is not found error.
The other way is to use the provided lib from botocore.vendored import requests. But this lib is deprecated by AWS.
I want to avoid to package dependencies within my lambda zip file.
What is the smartest solution to make a REST call in python based lambda?
Solution 1)
Since from botocore.vendored import requests is deprecated the recomended way is to install your dependencies.
$ pip install requests
import requests
response = requests.get('https://...')
See also. https://aws.amazon.com/de/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/
But you have to take care for packaging the dependencies within your lambda zip.
Solution 2)
My preferred solution is to use urllib. It's within your lambda execution context.
https://repl.it/#SmaMa/DutifulChocolateApplicationprogrammer
import urllib.request
import json
res = urllib.request.urlopen(urllib.request.Request(
url='http://asdfast.beobit.net/api/',
headers={'Accept': 'application/json'},
method='GET'),
timeout=5)
print(res.status)
print(res.reason)
print(json.loads(res.read()))
Solution 3)
Using http.client, it's also within your lambda execution context.
https://repl.it/#SmaMa/ExoticUnsightlyAstrophysics
import http.client
connection = http.client.HTTPSConnection('fakerestapi.azurewebsites.net')
connection.request('GET', '/api/Books')
response = connection.getresponse()
print(response.read().decode())

Workflow for Google Cloud Functions with Golang

When developing a small Google Cloud Function in Go. I noticed it will throw an error if you have everything in your package main - eg. import "<whatever>" is a program, not an importable package
So the solution is switch it out to its own package, then deploy. If something goes wrong, throw it back into a package main and work on it locally, then switch it back.
Is this the best workflow? The other option i see is possibly making the Cloud Function its own module and importing it into a main.go file.
I was able to create a cli folder in project's top level and then put main.go file using package main and main() function inside it. That allowed me to have separate file cloud_functions.go in root with different package name that has one or more google cloud functions in it.

import path issue when a package and the main use a common third package

I have several microservices written in golang, and wanted to provide some common services. In this example, I have a common go-restful filter that does authorization of API calls. The compilation error indicates that the auth package and the main package are importing the underlying go-restful from different places.
Is there a nice way to resolve this without collapsing the code into one repository?
./main.go:104: cannot use AUTHZ.AuthorizeFilter()
(type "github.com/Solinea/gsk-common/auth/vendor/github.com/emicklei/go-restful".FilterFunction) as type
"github.com/Solinea/gsk-analytics/vendor/github.com/emicklei/go-restful".FilterFunction
in argument to "github.com/Solinea/gsk-analytics/vendor/github.com/emicklei/go-restful".Filter

Resources