-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Go tutorial and reference #723
base: main
Are you sure you want to change the base?
Changes from 7 commits
e74cf0c
90de295
5e9bcc8
f00280e
26731a0
c454a84
cd58c67
6215c1d
db4666b
42166ce
cbce355
c810f0f
4a97758
87db8a2
73b9e13
8e4f4b3
fb1062a
a529914
5527e67
a4fc3d1
f1a3f93
5784740
19d83ea
db56bc7
3c8603c
e0ba7da
6c7da80
d845408
0d69b10
12750a2
4f61ced
e84fe5f
7a92aa6
b7f5653
b22fabf
1e9d175
16eb379
79d8765
7aca5d7
d5fb848
4e5f93f
ef88a60
b83026d
6993978
e61d3df
1e343c5
c6759cd
c548bd2
a686246
f48f04a
82746c9
f489aeb
581a7c3
5a27628
2a8e6a8
4666ce7
a0b3214
5960832
c0c3293
60a8503
72dd047
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
.. _go-framework-reference: | ||
|
||
go-framework | ||
---------------- | ||
|
||
The Go extension streamlines the process of building Go application | ||
rocks. | ||
|
||
The extension builds and copies the Go binary file to the rock, | ||
inside the ``/app`` directory. By default, the base ``bare`` is used, | ||
that generates a lightweight image. | ||
javierdelapuente marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
.. note:: | ||
The Go extension is compatible with the ``bare`` and ``[email protected]`` | ||
bases. | ||
|
||
Project requirements | ||
==================== | ||
|
||
To use the ``go-framework`` extension, there must be a ``go.mod`` file | ||
in the root directory of the project. | ||
|
||
|
||
``parts`` > ``go-framework/install-app`` > ``organize`` | ||
========================================================= | ||
|
||
If the main package is in the base directory and the rockcraft name | ||
attribute is equal to the go module name, the name of the binary will | ||
be selected correctly, otherwise you can adjust it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Seems like I HAVE to change it with that part, otherwise it doesn't build. go.modmodule foo
go 1.23.0 rockcraft.yamlname: hello
version: '0.2'
summary: TODO
description: TODO
base: bare
build-base: [email protected]
platforms:
amd64:
extensions:
- go-framework Failure logs
But if I add the Changed rockcraft.yamlname: hello
version: '0.2'
summary: TODO
description: TODO
base: bare
build-base: [email protected]
platforms:
amd64:
extensions:
- go-framework
parts:
go-framework/install-app:
organize:
bin/foo: usr/local/bin/hello There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, |
||
|
||
You can use this field to specify a different binary to be used as the | ||
main application, without having to override the service command. For example, | ||
if your Go application container a ``main`` package in the directory | ||
javierdelapuente marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``cmd/anotherserver``, the name of the binary will be ``anotherserver`` | ||
and you can override the main application to use the binary with the | ||
next snippet: | ||
|
||
.. code-block:: yaml | ||
|
||
parts: | ||
go-framework/install-app: | ||
organize: | ||
bin/anotherserver: usr/local/bin/<rockcraft project name> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If there are several Go binaries, and we want to include several of them in the rock, someway we have to choose the main one, and it will be the one in |
||
|
||
|
||
``parts`` > ``go-framework/assets`` > ``stage`` | ||
========================================================= | ||
|
||
|
||
Some files, if they exist, are included by default. These include: | ||
``migrate``, ``migrate.sh``, ``templates/`` and ``static/``. | ||
|
||
You can customise the files to include by overriding the ``stage`` property | ||
of the ``go-framework/assets`` part: | ||
|
||
.. code-block:: yaml | ||
|
||
parts: | ||
go-framework/assets: | ||
stage: | ||
- app/migrate | ||
- app/migrate.sh | ||
- app/static | ||
- app/another_file_or_directory | ||
|
||
|
||
Useful links | ||
============ | ||
|
||
- :ref:`build-a-rock-for-a-go-application` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ initiating a new rock. | |
|
||
flask-framework | ||
django-framework | ||
go-framework |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
parts: | ||
go-framework/install-app: | ||
organize: | ||
bin/anotherserver: usr/local/bin/go-hello-world |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func helloWorld(w http.ResponseWriter, req *http.Request) { | ||
log.Printf("anotherserver hello world request") | ||
fmt.Fprintf(w, "Hello World from anotherserver") | ||
} | ||
|
||
func main() { | ||
log.Printf("starting anotherserver hello world application") | ||
http.HandleFunc("/", helloWorld) | ||
http.ListenAndServe(":8000", nil) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func helloWorld(w http.ResponseWriter, req *http.Request) { | ||
log.Printf("new hello world request") | ||
fmt.Fprintf(w, "Hello World") | ||
javierdelapuente marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func main() { | ||
log.Printf("starting hello world application") | ||
http.HandleFunc("/", helloWorld) | ||
http.ListenAndServe(":8000", nil) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
########################################### | ||
# IMPORTANT | ||
# Comments matter! | ||
# The docs use the wrapping comments as | ||
# markers for including said instructions | ||
# as snippets in the docs. | ||
########################################### | ||
summary: Getting started with Go tutorial | ||
|
||
environment: | ||
|
||
execute: | | ||
set -x | ||
# [docs:install-go] | ||
sudo snap install go --classic | ||
# [docs:install-go-end] | ||
|
||
# [docs:mod-init-go] | ||
go mod init go-hello-world | ||
go mod tidy | ||
javierdelapuente marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# [docs:mod-init-go-end] | ||
|
||
# [docs:go-build] | ||
go build . | ||
# [docs:go-build-end] | ||
|
||
./go-hello-world & | ||
retry -n 5 --wait 2 curl --fail localhost:8000 | ||
|
||
# [docs:curl-go] | ||
curl --fail localhost:8000 | ||
# [docs:curl-go-end] | ||
|
||
kill $! | ||
|
||
# [docs:create-rockcraft-yaml] | ||
rockcraft init --profile go-framework | ||
# [docs:create-rockcraft-yaml-end] | ||
sed -i "s/name: .*/name: go-hello-world/g" rockcraft.yaml | ||
|
||
# [docs:pack] | ||
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack | ||
# [docs:pack-end] | ||
|
||
# [docs:ls-rock] | ||
ls *.rock -l --block-size=MB | ||
# [docs:ls-rock-end] | ||
|
||
# [docs:skopeo-copy] | ||
sudo rockcraft.skopeo --insecure-policy \ | ||
copy oci-archive:go-hello-world_0.1_amd64.rock \ | ||
docker-daemon:go-hello-world:0.1 | ||
# [docs:skopeo-copy-end] | ||
|
||
# [docs:docker-images] | ||
sudo docker images go-hello-world:0.1 | ||
# [docs:docker-images-end] | ||
|
||
# [docs:docker-run] | ||
sudo docker run --rm -d -p 8000:8000 \ | ||
--name go-hello-world go-hello-world:0.1 | ||
# [docs:docker-run-end] | ||
|
||
retry -n 5 --wait 2 curl --fail localhost:8000 | ||
# [docs:curl-go-rock] | ||
curl --fail localhost:8000 | ||
# [docs:curl-go-rock-end] | ||
[ "$(curl localhost:8000)" = "Hello World" ] | ||
|
||
# [docs:get-logs] | ||
sudo docker exec go-hello-world pebble logs go | ||
# [docs:get-logs-end] | ||
|
||
# [docs:stop-docker] | ||
sudo docker stop go-hello-world | ||
sudo docker rmi go-hello-world:0.1 | ||
# [docs:stop-docker-end] | ||
|
||
## Part 2 of the tutorial | ||
sed -i "s/version: .*/version: 0.2/g" rockcraft.yaml | ||
cat anotherserver_part >> rockcraft.yaml | ||
|
||
# [docs:docker-run-update] | ||
ROCKCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=true rockcraft pack | ||
sudo rockcraft.skopeo --insecure-policy \ | ||
copy oci-archive:go-hello-world_0.2_amd64.rock \ | ||
docker-daemon:go-hello-world:0.2 | ||
sudo docker images go-hello-world:0.2 | ||
sudo docker run --rm -d -p 8000:8000 \ | ||
--name go-hello-world go-hello-world:0.2 | ||
# [docs:docker-run-update-end] | ||
retry -n 5 --wait 2 curl --fail localhost:8000 | ||
|
||
# [docs:curl-anotherserver] | ||
curl --fail localhost:8000 | ||
# [docs:curl-anotherserver-end] | ||
[ "$(curl localhost:8000)" = "Hello World from anotherserver" ] | ||
|
||
# [docs:stop-docker-updated] | ||
sudo docker stop go-hello-world | ||
sudo docker rmi go-hello-world:0.2 | ||
# [docs:stop-docker-updated-end] | ||
|
||
# [docs:cleanup] | ||
# delete all the files created during the tutorial | ||
rm go.mod rockcraft.yaml main.go go-hello-world \ | ||
go-hello-world_0.1_amd64.rock \ | ||
cmd/anotherserver/main.go \ | ||
go-hello-world_0.2_amd64.rock | ||
rmdir -p cmd/anotherserver | ||
# [docs:cleanup-end] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the final image, it looks like the binary is in
/usr/local/bin
and/app
is empty though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch,
/app
has the assets, not the binaries. Removed that sentence and updated the part of the assets to say that they will be in the/app
directory.