Last active
April 26, 2024 09:49
-
-
Save seb-schulz/b350619e452479fa6d09f6979e894ca7 to your computer and use it in GitHub Desktop.
How to Quickly Get a Running Development Environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"name": "hello-devcontainers", | |
"dockerComposeFile": "docker-compose.yml", | |
"service": "devcontainer", | |
"features": { | |
"ghcr.io/devcontainers-contrib/features/apt-get-packages:1": { | |
"packages": "inotify-tools,bash-completion" | |
} | |
}, | |
"customizations": { | |
"vscode": { | |
"settings": {}, | |
"extensions": [ | |
"golang.go" | |
] | |
} | |
}, | |
"containerUser": "vscode" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.8" | |
services: | |
devcontainer: | |
image: mcr.microsoft.com/devcontainers/go:1 | |
userns_mode: keep-id | |
volumes: | |
- .:/workspaces/hello-devcontainers:cached | |
command: sleep infinity | |
depends_on: | |
- db | |
links: | |
- db | |
environment: | |
DB_DSN: "hello:.test.@tcp(db:3306)/hello?charset=utf8&parseTime=True" | |
db: | |
image: docker.io/library/mariadb:11 | |
restart: unless-stopped | |
environment: | |
- MARIADB_DATABASE=hello | |
- MARIADB_USER=hello | |
- MARIADB_PASSWORD=.test. | |
- MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1 | |
volumes: | |
- ../data:/var/lib/mysql | |
ports: | |
- 3306:3306 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module hello-devcontainers | |
go 1.22.1 | |
require github.com/go-sql-driver/mysql v1.8.1 | |
require filippo.io/edwards25519 v1.1.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= | |
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= | |
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= | |
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
func main() { | |
dsn := os.Getenv("DB_DSN") | |
if dsn == "" { | |
log.Fatal("DB_DSN environment variable is not set") | |
} | |
db, err := sql.Open("mysql", dsn) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer db.Close() | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello, World!\n") | |
var result int | |
err = db.QueryRow("SELECT 1").Scan(&result) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if result == 1 { | |
fmt.Fprintf(w, "MariaDB was responding!\n") | |
} | |
}) | |
if err := http.ListenAndServe(":9080", nil); err != nil { | |
fmt.Println("Error starting server:", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment