init
This commit is contained in:
36
.devcontainer/Dockerfile
Normal file
36
.devcontainer/Dockerfile
Normal file
@@ -0,0 +1,36 @@
|
||||
FROM golang:1.21-bullseye
|
||||
|
||||
# 安装基本工具
|
||||
RUN apt-get update && apt-get install -y \
|
||||
git \
|
||||
curl \
|
||||
wget \
|
||||
vim \
|
||||
sudo \
|
||||
&& apt-get clean \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# 创建一个非root用户
|
||||
ARG USERNAME=vscode
|
||||
ARG USER_UID=1000
|
||||
ARG USER_GID=$USER_UID
|
||||
|
||||
RUN groupadd --gid $USER_GID $USERNAME \
|
||||
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
|
||||
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
|
||||
&& chmod 0440 /etc/sudoers.d/$USERNAME
|
||||
|
||||
# Go工具设置
|
||||
ENV GOPATH=/go
|
||||
ENV PATH=$GOPATH/bin:/usr/local/go/bin:$PATH
|
||||
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" "$GOPATH/pkg" \
|
||||
&& chmod -R 777 "$GOPATH" \
|
||||
&& go install -v golang.org/x/tools/gopls@latest \
|
||||
&& go install -v github.com/go-delve/delve/cmd/dlv@latest \
|
||||
&& go install -v honnef.co/go/tools/cmd/staticcheck@latest
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /workspace
|
||||
|
||||
# 使用创建的非root用户
|
||||
USER $USERNAME
|
23
.devcontainer/devcontainer.json
Normal file
23
.devcontainer/devcontainer.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "Go Dev Environment",
|
||||
"build": {
|
||||
"dockerfile": "Dockerfile",
|
||||
"context": ".."
|
||||
},
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"settings": {
|
||||
"go.toolsManagement.checkForUpdates": "local",
|
||||
"go.useLanguageServer": true,
|
||||
"go.gopath": "/go"
|
||||
},
|
||||
"extensions": [
|
||||
"golang.go",
|
||||
"ms-vscode.go",
|
||||
]
|
||||
}
|
||||
},
|
||||
"forwardPorts": [8080],
|
||||
"postCreateCommand": "go mod tidy",
|
||||
"remoteUser": "vscode"
|
||||
}
|
26
README.md
Normal file
26
README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# DevContainer测试应用
|
||||
|
||||
这是一个简单的Go应用,用于测试DevContainer功能。
|
||||
|
||||
## 功能
|
||||
|
||||
- 提供一个基本的HTTP服务器
|
||||
- 显示服务器时间和请求信息
|
||||
- 包含健康检查端点(/health)
|
||||
|
||||
## 使用方法
|
||||
|
||||
1. 在DevStar中启动该DevContainer
|
||||
2. 容器启动后,访问映射的端口(默认8080)
|
||||
3. 你应该能看到一个简单的问候页面
|
||||
|
||||
## 开发
|
||||
|
||||
DevContainer已经配置好了Go开发环境,包括:
|
||||
|
||||
- Go语言支持
|
||||
- 代码补全
|
||||
- 调试工具
|
||||
- 常用命令行工具
|
||||
|
||||
只需在VS Code中打开,或通过DevStar提供的编辑器接口访问。
|
36
main.go
Normal file
36
main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 获取环境变量或使用默认值
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
// 创建一个简单的HTTP服务器
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
hostname, _ := os.Hostname()
|
||||
fmt.Fprintf(w, "Hello from DevContainer!\n")
|
||||
fmt.Fprintf(w, "Server time: %s\n", time.Now().Format(time.RFC1123))
|
||||
fmt.Fprintf(w, "Hostname: %s\n", hostname)
|
||||
fmt.Fprintf(w, "Remote Address: %s\n", r.RemoteAddr)
|
||||
fmt.Fprintf(w, "User-Agent: %s\n", r.UserAgent())
|
||||
})
|
||||
|
||||
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "Healthy")
|
||||
})
|
||||
|
||||
// 启动服务器
|
||||
log.Printf("Starting server on port %s", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||
}
|
Reference in New Issue
Block a user