From 1e155ed2c1f3f94a1de05723cf87f40c0302d3f6 Mon Sep 17 00:00:00 2001 From: Lennard Brinkhaus Date: Mon, 10 Jan 2022 17:31:57 +0100 Subject: [PATCH] Initial Commit with webdav server --- .editorconfig | 13 ++++ .gitignore | 146 ++++++++++++++++++++++++++++++++++++++++++++ .idea/.gitignore | 8 +++ .idea/modules.xml | 8 +++ .idea/webdav-go.iml | 9 +++ Dockerfile | 30 +++++++++ go.mod | 5 ++ go.sum | 7 +++ main.go | 50 +++++++++++++++ 9 files changed, 276 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/webdav-go.iml create mode 100644 Dockerfile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..98aa99d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +; http://editorconfig.org/ + +root = true + +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +trim_trailing_whitespace = true + +[*.go] +indent_style = tab +indent_size = 8 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d9bf6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,146 @@ +# ---> Go +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# ---> JetBrains +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +# ---> Eclipse + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Annotation Processing +.apt_generated/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet + +# ---> VisualStudioCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..fc7995a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/webdav-go.iml b/.idea/webdav-go.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/webdav-go.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..49e6723 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +# build stage +FROM golang:1.16-stretch AS build-env + +WORKDIR /src + +COPY go.mod /src/go.mod +RUN go mod download + +COPY . /src + +ENV GO113MODULE=on +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o app . + +# cert stage +FROM alpine:latest as certs +RUN apk --update add ca-certificates + + # final stage +FROM scratch +LABEL maintainer="Lennard Brinkhaus " + +COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt + +WORKDIR /app + +USER 101 + +COPY --from=build-env /src/app . + +ENTRYPOINT [ "./app" ] \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e68b5df --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.dragse.it/lennard.brinkhaus/webdav-go + +go 1.16 + +require golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..82091fc --- /dev/null +++ b/go.sum @@ -0,0 +1,7 @@ +golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d h1:62NvYBuaanGXR2ZOfwDFkhhl6X1DUgf8qg3GuQvxZsE= +golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..dec86d1 --- /dev/null +++ b/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "log" + "net/http" + "os" + + "golang.org/x/net/webdav" +) + +const ( + envPrefix = "WEBDAV_PREFIX" + pathRoot = "/webdav/root" + pathLog = "/webdav/log/webdav.log" +) + +func logger() func(*http.Request, error) { + return func(r *http.Request, err error) { + if err != nil { + log.Printf("ERROR %v\n", err) + } + } +} + +func filesystem() webdav.FileSystem { + if err := os.Mkdir(pathRoot, os.ModePerm); !os.IsExist(err) { + log.Fatalf("FATAL %v", err) + } + log.Printf("INFO using local filesystem at %s", pathRoot) + return webdav.Dir(pathRoot) +} + +func main() { + logFile, err := os.OpenFile(pathLog, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + if err != nil { + log.Fatalf("FATAL %v", err) + } + defer logFile.Close() + log.SetOutput(logFile) + + h := &webdav.Handler{ + Prefix: os.Getenv(envPrefix), + FileSystem: filesystem(), + LockSystem: webdav.NewMemLS(), + Logger: logger(), + } + + http.HandleFunc("/", h.ServeHTTP) + http.ListenAndServe(":8080", h) +}