1
0
mirror of https://github.com/fumiama/paper-manager.git synced 2026-06-10 10:50:23 +08:00

front: finish docx & back: init

This commit is contained in:
源文雨
2023-03-16 14:03:58 +08:00
parent 6c823457b9
commit a72afdbb5e
26 changed files with 453 additions and 31 deletions

15
backend/api/main.go Normal file
View File

@@ -0,0 +1,15 @@
package api
import (
"net/http"
"github.com/fumiama/paper-manager/backend/utils"
)
// Handler serves all backend /api call
func Handler(w http.ResponseWriter, r *http.Request) {
if !utils.IsMethod("GET", w, r) {
return
}
http.Error(w, "404 Not Found", http.StatusNotFound)
}

26
backend/file/provider.go Normal file
View File

@@ -0,0 +1,26 @@
package file
import (
"net/http"
"strings"
"github.com/fumiama/paper-manager/backend/global"
"github.com/fumiama/paper-manager/backend/utils"
"github.com/sirupsen/logrus"
)
// Handler serves contents in global.FileFolder
func Handler(w http.ResponseWriter, r *http.Request) {
if !utils.IsMethod("GET", w, r) {
return
}
i := strings.LastIndex(r.URL.Path, "/")
fn := r.URL.Path[i+1:]
if fn == "" {
http.Error(w, "400 Bad Request: empty path", http.StatusBadRequest)
return
}
name := global.FileFolder + fn
logrus.Infoln("[file.Handler]\t serve", name)
http.ServeFile(w, r, name)
}

36
backend/global/base.go Normal file
View File

@@ -0,0 +1,36 @@
package global
import (
"os"
"runtime"
"github.com/sirupsen/logrus"
)
const (
// DataFolder stores all backend data in
DataFolder = "./data/"
// FileFolder stores all blob files
FileFolder = DataFolder + "file/"
)
func init() {
initdir(DataFolder)
initdir(FileFolder)
}
func initdir(folder string) {
err := os.MkdirAll(folder, 0755)
if err != nil {
logrus.Errorln("[os.MkdirAll]\t", err)
os.Exit(line())
}
}
func line() int {
_, _, fileLine, ok := runtime.Caller(2)
if ok {
return fileLine
}
return -1
}

27
backend/utils/method.go Normal file
View File

@@ -0,0 +1,27 @@
package utils
import (
"net/http"
"github.com/sirupsen/logrus"
)
// IP gets ip from r.Header's X-FORWARDED-FOR or r.RemoteAddr
func IP(r *http.Request) string {
forwarded := r.Header.Get("X-FORWARDED-FOR")
if forwarded != "" {
return forwarded
}
return r.RemoteAddr
}
// IsMethod check if the method meets the requirement
// and response 405 Method Not Allowed if not matched
func IsMethod(m string, w http.ResponseWriter, r *http.Request) bool {
logrus.Infoln("[utils.IsMethod]\t accept", IP(r), r.Method, r.URL)
if r.Method != m {
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
return false
}
return true
}