Skip to content

Commit

Permalink
feat: Support custom port
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxuan6 committed Oct 22, 2024
1 parent 8ef5bb7 commit f8857aa
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 27 deletions.
51 changes: 26 additions & 25 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
FROM golang:1.22.5-alpine3.20 AS build-dev

WORKDIR /go/src/app

COPY . .

RUN go env -w GO111MODULE=on && \
go env -w GOPROXY=https://goproxy.cn,direct && \
go mod download && \
apk add --no-cache upx && \
go build -trimpath -ldflags="-s -w" -o holiday . && \
[ -e /usr/bin/upx ] && upx holiday || echo

FROM alpine

RUN apk update && \
apk add --no-cache tzdata

ENV TZ=Asia/Shanghai

COPY --from=build-dev /go/src/app/holiday ./holiday

EXPOSE 80

CMD ["./holiday"]
FROM golang:1.22.5-alpine3.20 AS build-dev

WORKDIR /go/src/app

COPY . .

RUN go env -w GO111MODULE=on && \
go env -w GOPROXY=https://goproxy.cn,direct && \
go mod download && \
apk add --no-cache upx && \
go build -trimpath -ldflags="-s -w" -o holiday . && \
[ -e /usr/bin/upx ] && upx holiday || echo

FROM alpine

RUN apk update && \
apk add --no-cache tzdata

ENV TZ=Asia/Shanghai
ENV HOLIDAY_PORT=80

COPY --from=build-dev /go/src/app/holiday ./holiday

EXPOSE $HOLIDAY_PORT

CMD ["./holiday"]
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,35 @@

提供中国休假或者工作日查询

## Run

### 直接运行

```shell
go run main.go
```

### 自定义端口

```shell
export HOLIDAY_PORT=9002
go run main.go
```

## Docker

### 直接运行

```docker
docker run --name=holidays -p 80:80 -d ghcr.io/xiaoxuan6/chinese-holidays-api/chinese-holidays-api:latest
```

### 自定义端口

```docker
docker run --name=holiday -e HOLIDAY_PORT=9002 -p 9002:9002 -d ghcr.io/xiaoxuan6/chinese-holidays-api/chinese-holidays-api:latest
```

## Api

查询 `2024-08-06` 是否是工作日
Expand All @@ -19,7 +42,7 @@ curl http://127.0.0.1/api/holidays?date=2024-08-06

{
"code": 200,
"date": "2024-08-05", <- version v0.0.2
"date": "2024-08-06", <- version v0.0.2
"is_holiday": false,
"is_working_day": true,
"lunar_date": "七月初二日", <- version v0.0.3
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ require (
github.com/gorilla/mux v1.8.1
github.com/huandu/xstrings v1.5.0
)

require github.com/kelseyhightower/envconfig v1.4.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
25 changes: 24 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
package main

import (
"github.com/kelseyhightower/envconfig"
"github.com/xiaoxuan6/chinese-holidays-api/router"
"log"
"net/http"
"strconv"
)

type env struct {
PORT int
}

var e env

func init() {
if err := envconfig.Process("holiday", &e); err != nil {
log.Println("envconfig load fail:", err.Error())
}

if e.PORT == 0 {
e.PORT = 80
}
}

func main() {
r := router.InitRouter()
log.Panic(http.ListenAndServe(":80", r))

port := strconv.Itoa(e.PORT)
log.Println("已启动服务:127.0.0.1:" + port)
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Panic(err)
}
}

0 comments on commit f8857aa

Please sign in to comment.