diff --git a/.dockerignore b/.dockerignore index 573040d..bb7301c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,2 @@ -Dockerfile -draft.toml -charts/ +charts +src/credentials.json \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5cde50f..4ceeeef 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,42 @@ src/credentials.json *.out # Dependency directories (remove the comment below to include it) -# vendor/ \ No newline at end of file +# vendor/ + +# General files for the project +pkg/* +*.pyc +bin/* +.project +/.bin +/_test/secrets/*.json + +# OSX leaves these everywhere on SMB shares +._* + +# OSX trash +.DS_Store + +# Files generated by JetBrains IDEs, e.g. IntelliJ IDEA +.idea/ +*.iml + +# Vscode files +.vscode + +# Emacs save files +*~ +\#*\# +.\#* + +# Vim-related files +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +*.un~ +Session.vim +.netrwhist + +# Chart dependencies +**/charts/*.tgz + +.history \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 9d274a3..1c8b283 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM golang:latest as builder LABEL maintainer="Siddharth " WORKDIR /app -COPY src/ app/ +COPY src/ . RUN go mod download RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . @@ -12,7 +12,12 @@ FROM alpine:latest RUN apk --no-cache add ca-certificates tzdata ENV TZ=Asia/Kolkata +ENV GIN_MODE=release + WORKDIR /root/ +COPY src/web web + COPY --from=builder /app/main . EXPOSE 8080 + CMD ["./main"] \ No newline at end of file diff --git a/README.md b/README.md index 9834c1d..8c38681 100644 --- a/README.md +++ b/README.md @@ -1,3 +1 @@ -TODO: -- [ ] rearrange folder -- [ ] add ui template \ No newline at end of file + kubectl create secret generic google_creds --from-file=src/credentials.json \ No newline at end of file diff --git a/src/go.mod b/src/go.mod index ce0c931..e1ff79b 100644 --- a/src/go.mod +++ b/src/go.mod @@ -6,7 +6,7 @@ require ( cloud.google.com/go v0.66.0 // indirect github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect github.com/coreos/go-oidc v2.2.1+incompatible - github.com/gin-contrib/pprof v1.3.0 + github.com/gin-contrib/cache v1.1.0 github.com/gin-gonic/contrib v0.0.0-20200913005814-1c32036e7ea4 github.com/gin-gonic/gin v1.6.3 github.com/gorilla/sessions v1.2.1 // indirect diff --git a/src/main.go b/src/main.go index f1287d8..6e8c711 100644 --- a/src/main.go +++ b/src/main.go @@ -2,8 +2,12 @@ package main import ( "fmt" + "github.com/gin-contrib/cache" + "github.com/gin-contrib/cache/persistence" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" + "net/http" + "time" ) func ErrorHandle() gin.HandlerFunc { @@ -22,6 +26,19 @@ func ErrorHandle() gin.HandlerFunc { } } +func health(ctx *gin.Context) { + if mongoClient != nil && config != nil { + ctx.JSON(http.StatusOK, + gin.H{ + "health": "ok", + }) + } + ctx.JSON(http.StatusBadGateway, + gin.H{ + "health": "lol", + }) +} + func main() { //setTimeZone @@ -31,19 +48,23 @@ func main() { } // mongodb connection - mongoURI := "mongodb://localhost:27017" + mongoURI := "mongodb://mongo:27017" err = setupMongo(mongoURI) if err != nil { return } // oauth connection - err = setupOAuthClientCredentials("./credentials.json") + err = setupOAuthClientCredentials("/etc/secret/credentials.json") if err != nil { return } router := gin.Default() + gin.SetMode(gin.ReleaseMode) + + // cache setup + cacheStore := persistence.NewInMemoryStore(time.Minute) // setup session cookie storage var store = sessions.NewCookieStore([]byte("secret")) @@ -59,14 +80,15 @@ func main() { router.StaticFile("/favicon.ico", "./web/favicon.ico") router.LoadHTMLFiles("web/index.html") - router.GET("/", index) // index page - router.GET("/list/json", list) // show information in json + router.GET("/", cache.CachePageWithoutQuery(cacheStore, 5*time.Minute, index)) // index page + router.GET("/list/json", list) // show information in json router.GET("/login", authoriseUserHandler) // to register router.GET("/auth", oAuthCallbackHandler) // oauth callback + router.GET("/health", health) // Add the pprof routes //pprof.Register(router) - _ = router.Run("127.0.0.1:9090") + _ = router.Run("0.0.0.0:8080") }