diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..3375dc0 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,72 @@ +name: Build And Push Docker Image + +on: + push: + # Sequence of patterns matched against refs/tags + tags: + - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 + workflow_dispatch: + +jobs: + build: + + runs-on: ubuntu-latest + + permissions: + packages: write + contents: read + + steps: + - uses: actions/checkout@v2 + + - name: Set time zone + uses: szenius/set-timezone@v1.0 + with: + timezoneLinux: "Asia/Shanghai" + timezoneMacos: "Asia/Shanghai" + timezoneWindows: "China Standard Time" + + # # 如果有 dockerhub 账户,可以在github的secrets中配置下面两个,然后取消下面注释的这几行,并在meta步骤的images增加一行 ${{ github.repository }} + # - name: Login to DockerHub + # uses: docker/login-action@v1 + # with: + # username: ${{ secrets.DOCKERHUB_USERNAME }} + # password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Login to GHCR + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v3 + with: + images: | + ghcr.io/${{ github.repository }} + # generate Docker tags based on the following events/attributes + # nightly, master, pr-2, 1.2.3, 1.2, 1 + tags: | + type=schedule,pattern=nightly + type=edge + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..95765b3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,41 @@ +FROM alpine:latest AS builder + +RUN apk update && apk add gcc g++ cmake automake autoconf libtool make linux-headers git + +WORKDIR /build + +RUN git clone --depth=1 https://github.com/fumiama/simple-crypto.git \ + && cd simple-crypto \ + && mkdir build \ + && cd build \ + && cmake .. \ + && make install + +RUN rm -rf * + +RUN git clone --depth=1 https://github.com/fumiama/simple-protobuf.git \ + && cd simple-protobuf \ + && mkdir build \ + && cd build \ + && cmake .. \ + && make install + +RUN rm -rf * + +COPY ./ . + +RUN mkdir build \ + && cd build \ + && cmake .. \ + && make + +FROM alpine:latest + +COPY --from=builder /build/build/simple-dict-server /usr/bin/simple-dict-server +COPY --from=builder /usr/local/lib/libspb.so /usr/local/lib/libspb.so +COPY --from=builder /usr/local/lib/libscrypto.so /usr/local/lib/libscrypto.so +RUN chmod +x /usr/bin/simple-dict-server + +WORKDIR /data + +ENTRYPOINT [ "/usr/bin/simple-dict-server" ] \ No newline at end of file diff --git a/README.md b/README.md index 6ba49a0..bd37f5f 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Start server on localhost using the commands below. chmod +x simple-dict-server simple-dict-client ./simple-dict-server -d 7777 1 ./dict.sp ./cfg.sp # use -d to start as daemon ``` -`cfg.sp`is generated by `cfgwriter`. +`cfg.sp`is generated by `cfgwriter`, or you can pass config by setting env `SDS_PWD` and `SDS_SPS`. Open another shell to connect to it. ```bash diff --git a/dict.c b/dict.c index b92ccda..75e645d 100644 --- a/dict.c +++ b/dict.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include "dict.h" #include "server.h" @@ -21,6 +21,13 @@ static FILE* thread_fp[THREADCNT]; int fill_md5() { size_t size = get_dict_size(); + if(!size) { + if(dict_md5) free(dict_md5); + dict_md5 = malloc(16); + memset(dict_md5, 0, 16); + puts("Dict is empty, use all zero md5"); + return 1; + } uint8_t* dict_buff = (uint8_t*)malloc(size); if(dict_buff) { rewind(fp5); diff --git a/server.c b/server.c index 1ec9d25..2567d54 100644 --- a/server.c +++ b/server.c @@ -514,18 +514,37 @@ int main(int argc, char *argv[]) { if(!fp) fp = fopen(argv[as_daemon?4:3], "wb+"); if(fp) { fclose(fp); - init_dict(argv[as_daemon?4:3]); - fp = NULL; - fp = fopen(argv[as_daemon?5:4], "rb"); - if(fp) { - SIMPLE_PB* spb = get_pb(fp); - cfg = (CONFIG*)spb->target; - fclose(fp); - items_len = align_struct(sizeof(DICT), 2, d.key, d.data); - if(items_len) { - if(bind_server(port, times)) if(listen_socket(times)) accept_client(); - } else puts("Align struct error."); - } else printf("Error opening config file: %s\n", argv[as_daemon?5:4]); + if(init_dict(argv[as_daemon?4:3])) { + fp = NULL; + if(argv[as_daemon?5:4][0] == '-') { // use env + fp = (FILE*)1; + cfg = (CONFIG*)malloc(sizeof(CONFIG)); + puts("Read config from env."); + char* pwd = getenv("SDS_PWD"); + if(pwd) { + char* sps = getenv("SDS_SPS"); + if(sps) { + strncpy(cfg->pwd, pwd, 64); + strncpy(cfg->sps, sps, 64); + cfg->pwd[63] = 0; + cfg->sps[63] = 0; + fp = (FILE*)-1; + } else puts("Env SDS_SPS is null."); + } else puts("Env SDS_PWD is null."); + } + if(!fp) fp = fopen(argv[as_daemon?5:4], "rb"); + if(fp && ((int)fp-1)) { + if(~((int)fp)) { + SIMPLE_PB* spb = get_pb(fp); + cfg = (CONFIG*)spb->target; + fclose(fp); + } + items_len = align_struct(sizeof(DICT), 2, d.key, d.data); + if(items_len) { + if(bind_server(port, times)) if(listen_socket(times)) accept_client(); + } else puts("Align struct error."); + } else printf("Error opening config file: %s\n", argv[as_daemon?5:4]); + } } else printf("Error opening dict file: %s\n", argv[as_daemon?4:3]); } else puts("Start daemon error"); } else printf("Error times: %d\n", times);