Golang private modules conf: meta tags, git+ssh, nginx: Difference between revisions

From wikinotes
Line 3: Line 3:
by setting dynamic html meta tags using nginx.
by setting dynamic html meta tags using nginx.


= Go Package =
= Workstation =
<blockquote>
<blockquote>
== Add Private URL ==
<blockquote>
First, add the target URL to your <code>$GOPRIVATE</code> environment variable using one of the following methods:
<syntaxhighlight lang="bash">
# ~/.config/go/env
GOPRIVATE="go.example.com/x/*"
</syntaxhighlight>
<syntaxhighlight lang="bash">
# interactive/cli
go env -w 'GOPRIVATE=go.example.com/x/*"'
</syntaxhighlight>
</blockquote><!-- Add Private URL -->
== Create Repo ==
<blockquote>
<syntaxhighlight lang="bash">
mkdir hello && cd hello
git init
git remote add origin foo:/repos/hello
</syntaxhighlight>
<syntaxhighlight lang="go">
# main.go
package main
import "fmt"
func main() {
fmt.Println("hello")
}
</syntaxhighlight>
<syntaxhighlight lang="go">
# go.mod


</blockquote><!-- Go Package -->
module willpittman.net/x/hello
go 1.18
</syntaxhighlight>


<syntaxhighlight lang="bash">
git commit -m 'init'
git push
</syntaxhighlight>
</blockquote><!-- Create Repo -->
</blockquote><!-- Workstation -->


= Package Consumers =
= Package Consumers =

Revision as of 21:46, 19 June 2022

This configuration abstracts golang module path,
to provide access to private go modules over git+ssh,
by setting dynamic html meta tags using nginx.

Workstation

Add Private URL

First, add the target URL to your $GOPRIVATE environment variable using one of the following methods:

# ~/.config/go/env

GOPRIVATE="go.example.com/x/*"
# interactive/cli
go env -w 'GOPRIVATE=go.example.com/x/*"'

Create Repo

mkdir hello && cd hello
git init
git remote add origin foo:/repos/hello
# main.go

package main
import "fmt"
func main() {
	fmt.Println("hello")
}
# go.mod

module willpittman.net/x/hello
go 1.18
git commit -m 'init'
git push

Package Consumers

# ~/.ssh/config

Host foo
  Hostname example.com
  IdentityFile ~/.ssh/user
  User user
go install example.com/x/hello

WebServer

# /usr/local/etc/nginx/nginx.conf

http {
    location / {
      root /usr/local/www/example.com;
      index index.html;
    }

    location ~ ^/x/(?<go_project>[^/]+)$ {
      root /usr/local/www/example.com;
      rewrite ^ /gopkg.html;
      sub_filter '{TARGET_PROJECT}' '$go_project'
      sub_filter_once off;
    }
}
<!-- /usr/local/www/example.com/gopkg.html -->

<html>
  <head>
    <meta name="go-import" content="example.com/x/{TARGET_PROJECT} git git+ssh://foo:/repos/{TARGET_PROJECT}">
  </head>
</html>