forked from opsre/go-ldap-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 将静态内容调整为embed嵌入,减少外部依赖文件 (opsre#325)
* feat: 将静态内容调整为embed嵌入,减少外部依赖文件
- Loading branch information
Showing
27 changed files
with
179 additions
and
176 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
tmp | ||
logs | ||
public/static/dist |
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,29 @@ | ||
FROM golang:1.18.10-alpine3.16 AS builder | ||
FROM registry.cn-hangzhou.aliyuncs.com/ali_eryajf/golang:1.18.10-alpine3.17 AS builder | ||
|
||
# ENV GOPROXY https://goproxy.io | ||
WORKDIR /app | ||
|
||
RUN mkdir /app && apk add --no-cache --virtual .build-deps \ | ||
ca-certificates \ | ||
gcc \ | ||
g++ | ||
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories \ | ||
&& apk upgrade && apk add --no-cache --virtual .build-deps \ | ||
ca-certificates gcc g++ curl | ||
|
||
ADD . /app/ | ||
ADD . . | ||
|
||
WORKDIR /app | ||
RUN release_url=$(curl -s https://api.github.com/repos/eryajf/go-ldap-admin-ui/releases/latest | grep "browser_download_url" | grep -v 'dist.zip.md5' | cut -d '"' -f 4); wget $release_url && unzip dist.zip && rm dist.zip && mv dist public/static | ||
|
||
RUN sed -i 's@localhost:389@openldap:389@g' /app/config.yml \ | ||
&& sed -i 's@host: localhost@host: mysql@g' /app/config.yml && go build -o go-ldap-admin . | ||
|
||
### build final image | ||
FROM alpine:3.16 | ||
|
||
# we set the timezone `Asia/Shanghai` by default, you can be modified | ||
# by `docker build --build-arg="TZ=Other_Timezone ..."` | ||
ARG TZ="Asia/Shanghai" | ||
FROM registry.cn-hangzhou.aliyuncs.com/ali_eryajf/alpine:3.19 | ||
|
||
ENV TZ ${TZ} | ||
|
||
RUN mkdir /app | ||
LABEL maintainer [email protected] | ||
|
||
WORKDIR /app | ||
|
||
COPY --from=builder /app/ . | ||
|
||
|
||
RUN apk upgrade \ | ||
&& apk add bash tzdata sqlite vim \ | ||
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \ | ||
&& echo ${TZ} > /etc/timezone | ||
COPY --from=builder /app/wait . | ||
COPY --from=builder /app/LICENSE . | ||
COPY --from=builder /app/config.yml . | ||
COPY --from=builder /app/go-ldap-admin . | ||
|
||
RUN chmod +x wait go-ldap-admin | ||
|
||
|
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
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
File renamed without changes.
File renamed without changes.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package middleware | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
"net/http" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const INDEX = "index.html" | ||
|
||
type ServeFileSystem interface { | ||
http.FileSystem | ||
Exists(prefix string, path string) bool | ||
} | ||
|
||
type localFileSystem struct { | ||
http.FileSystem | ||
root string | ||
indexes bool | ||
} | ||
|
||
func LocalFile(root string, indexes bool) *localFileSystem { | ||
return &localFileSystem{ | ||
FileSystem: gin.Dir(root, indexes), | ||
root: root, | ||
indexes: indexes, | ||
} | ||
} | ||
|
||
func (l *localFileSystem) Exists(prefix string, filepath string) bool { | ||
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) { | ||
name := path.Join(l.root, p) | ||
stats, err := os.Stat(name) | ||
if err != nil { | ||
return false | ||
} | ||
if stats.IsDir() { | ||
if !l.indexes { | ||
index := path.Join(name, INDEX) | ||
_, err := os.Stat(index) | ||
if err != nil { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func ServeRoot(urlPrefix, root string) gin.HandlerFunc { | ||
return Serve(urlPrefix, LocalFile(root, false)) | ||
} | ||
|
||
// Static returns a middleware handler that serves static files in the given directory. | ||
func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc { | ||
fileserver := http.FileServer(fs) | ||
if urlPrefix != "" { | ||
fileserver = http.StripPrefix(urlPrefix, fileserver) | ||
} | ||
return func(c *gin.Context) { | ||
if fs.Exists(urlPrefix, c.Request.URL.Path) { | ||
fileserver.ServeHTTP(c.Writer, c.Request) | ||
c.Abort() | ||
} | ||
} | ||
} | ||
|
||
type embedFileSystem struct { | ||
http.FileSystem | ||
} | ||
|
||
func (e embedFileSystem) Exists(prefix string, path string) bool { | ||
_, err := e.Open(path) | ||
return err == nil | ||
} | ||
|
||
func EmbedFolder(fsEmbed embed.FS, targetPath string) ServeFileSystem { | ||
fsys, err := fs.Sub(fsEmbed, targetPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return embedFileSystem{ | ||
FileSystem: http.FS(fsys), | ||
} | ||
} |
Oops, something went wrong.