feat: add http server

This commit is contained in:
lab 2023-11-09 10:53:39 +08:00
parent 08ff9fa671
commit 88f5f43377
5 changed files with 95 additions and 0 deletions

31
.gitea/workflow/ci.yaml Normal file
View File

@ -0,0 +1,31 @@
name: ci
on:
push:
branches:
- "main"
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
platforms: linux/amd64
tags: |
esinio/gitea-runner-ci-demo:latest

20
Dockerfile Normal file
View File

@ -0,0 +1,20 @@
FROM golang:alpine as build
WORKDIR /go/src
ADD . .
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories \
&& apk --no-cache add ca-certificates \
&& go env -w GOPROXY='https://goproxy.cn,direct' \
&& go mod tidy \
&& go build -o /go/bin/app .
FROM scratch
# copy the ca-certificate.crt from the build stage
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /go/bin/app /app
EXPOSE 80
ENTRYPOINT ["/app"]

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/gitea/runner-ci-demo
go 1.18
require github.com/gorilla/mux v1.8.1

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=

37
main.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/now", TimeHandler)
http.Handle("/", r)
srv := &http.Server{
Handler: r,
Addr: "0.0.0.0:80",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello world")
}
func TimeHandler(w http.ResponseWriter, r *http.Request) {
now := time.Now()
res := now.GoString()
fmt.Fprint(w, res)
}