Compare commits

..

5 commits

Author SHA1 Message Date
9fd6116858
ci: remove staticcheck install using go install in favor of alpine's
Some checks reported errors
continuous-integration/drone/pr Build was killed
continuous-integration/drone/push Build was killed
continuous-integration/drone Build is passing
2022-06-28 15:27:16 +02:00
33cc395882
ci: fix staticcheck job by adding libc and gcc deps
Some checks reported errors
continuous-integration/drone/push Build was killed
continuous-integration/drone/pr Build was killed
2022-06-28 15:21:22 +02:00
e7a9ddc460
ci: add staticcheck job
Some checks failed
continuous-integration/drone/push Build is failing
2022-06-28 15:09:00 +02:00
3dceae4be0
chore: add build badge
All checks were successful
continuous-integration/drone/push Build is passing
2022-06-28 14:25:21 +02:00
f3ea851dd8
ci: add docker upload job 2022-06-28 14:23:42 +02:00
5 changed files with 45 additions and 20 deletions

25
.drone.yml Normal file
View file

@ -0,0 +1,25 @@
---
kind: pipeline
type: docker
name: default
steps:
- name: staticcheck
image: golang:1.18.3-alpine3.16
commands:
- apk add --no-cache git~=2 staticcheck~=2022 gcc~=11 libc-dev~=0
- staticcheck
- name: build
image: plugins/docker
settings:
username:
from_secret: DOCKER_USERNAME
password:
from_secret: DOCKER_PASSWORD
repo: 185504a9/sfu
tags: latest
target: run_prod
depends_on:
- staticcheck

View file

@ -6,7 +6,7 @@ ARG uid=1000
RUN mkdir -p "$APP_ROOT" \ RUN mkdir -p "$APP_ROOT" \
&& addgroup --system sfu -g $gid \ && addgroup --system sfu -g $gid \
&& adduser -h "$APP_ROOT" --disabled-password --system -u $uid --ingroup sfu sfu \ && adduser -h "$APP_ROOT" --disabled-password --system -u $uid --ingroup sfu sfu \
&& apk add curl~=7 && apk add --no-cache curl~=7
WORKDIR "$APP_ROOT" WORKDIR "$APP_ROOT"
USER sfu:sfu USER sfu:sfu

View file

@ -1,4 +1,5 @@
# sfu # sfu
[![Build Status](https://qa.roboces.dev/api/badges/catalin/sfu/status.svg)](https://qa.roboces.dev/catalin/sfu)
simple requirementsless, authenticationless file upload server simple requirementsless, authenticationless file upload server

View file

@ -10,7 +10,7 @@ import (
) )
func downloadFile(w http.ResponseWriter, req *http.Request, fileName string) { func downloadFile(w http.ResponseWriter, req *http.Request, fileName string) {
Info.Println(fmt.Sprintf("server: will download %v", fileName)) Info.Printf("server: will download %v\n", fileName)
file, err := os.Open(fmt.Sprintf("%v/%v", SFU_FILES_DIR, fileName)) file, err := os.Open(fmt.Sprintf("%v/%v", SFU_FILES_DIR, fileName))
if err != nil { if err != nil {
Error.Println(err) Error.Println(err)
@ -34,9 +34,9 @@ func fileExists(fileName string) bool {
} }
func deleteFile(w http.ResponseWriter, req *http.Request, fileName string) { func deleteFile(w http.ResponseWriter, req *http.Request, fileName string) {
Info.Println(fmt.Sprintf("server: will delete %v", fileName)) Info.Printf("server: will delete %v\n", fileName)
if !fileExists(fileName) { if !fileExists(fileName) {
Error.Println(fmt.Sprintf("%v does not exist", fileName)) Error.Printf("%v does not exist\n", fileName)
http.Error(w, "File not found", http.StatusNotFound) http.Error(w, "File not found", http.StatusNotFound)
return return
} }
@ -59,12 +59,12 @@ func uploadFile(w http.ResponseWriter, req *http.Request) {
fileExists := fileExists(fileHeader.Filename) fileExists := fileExists(fileHeader.Filename)
forceOverwrite := req.FormValue("force") forceOverwrite := req.FormValue("force")
if fileExists && forceOverwrite != "true" { if fileExists && forceOverwrite != "true" {
Error.Println(fmt.Sprintf("file %v already exists", fileHeader.Filename)) Error.Printf("file %v already exists\n", fileHeader.Filename)
http.Error(w, "File already exists", http.StatusConflict) http.Error(w, "File already exists", http.StatusConflict)
return return
} }
if !fileExists || (forceOverwrite == "true" && fileExists) { if !fileExists || (forceOverwrite == "true" && fileExists) {
Info.Println(fmt.Sprintf("server: will upload %v", fileHeader.Filename)) Info.Printf("server: will upload %v\n", fileHeader.Filename)
out, err := os.Create(fmt.Sprintf("%v/%v", SFU_FILES_DIR, fileHeader.Filename)) out, err := os.Create(fmt.Sprintf("%v/%v", SFU_FILES_DIR, fileHeader.Filename))
if err != nil { if err != nil {
Error.Println(err) Error.Println(err)
@ -83,15 +83,14 @@ func uploadFile(w http.ResponseWriter, req *http.Request) {
return return
} }
http.Error(w, "internal server error", http.StatusInternalServerError) http.Error(w, "internal server error", http.StatusInternalServerError)
return
} }
func listFiles(w http.ResponseWriter, req *http.Request) { func listFiles(w http.ResponseWriter, req *http.Request) {
Info.Println(fmt.Sprintf("server: will list uploaded files on %v", SFU_FILES_DIR)) Info.Printf("server: will list uploaded files on %v\n", SFU_FILES_DIR)
files, err := ioutil.ReadDir(SFU_FILES_DIR) files, err := ioutil.ReadDir(SFU_FILES_DIR)
if err != nil { if err != nil {
Warning.Println(fmt.Sprintf("%v does not exist", SFU_FILES_DIR)) Warning.Printf("%v does not exist\n", SFU_FILES_DIR)
Info.Println(fmt.Sprintf("will create %v", SFU_FILES_DIR)) Info.Printf("will create %v\n", SFU_FILES_DIR)
_ = os.Mkdir(SFU_FILES_DIR, os.ModePerm) _ = os.Mkdir(SFU_FILES_DIR, os.ModePerm)
} }
fmt.Fprint(w, "<html><body><ol>") fmt.Fprint(w, "<html><body><ol>")

20
main.go
View file

@ -12,10 +12,10 @@ var SFU_FILES_DIR string = getEnvvar("SFU_FILES_DIR")
var SFU_PORT string = getEnvvar("SFU_PORT") var SFU_PORT string = getEnvvar("SFU_PORT")
func getEnvvar(envvar_name string) string { func getEnvvar(envvar_name string) string {
Info.Printf("getting envvar %v", envvar_name) Info.Printf("getting envvar %v\n", envvar_name)
envvar_value, isSet := os.LookupEnv(envvar_name) envvar_value, isSet := os.LookupEnv(envvar_name)
if !isSet { if !isSet {
Error.Println(fmt.Sprintf("%v is not set", envvar_name)) Error.Printf("%v is not set\n", envvar_name)
os.Exit(1) os.Exit(1)
} }
return envvar_value return envvar_value
@ -33,9 +33,9 @@ func emptyArray(s []string) []string {
func routeFiles(w http.ResponseWriter, r *http.Request) { func routeFiles(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
Info.Println(fmt.Sprintf("received %v on %v", r.Method, path)) Info.Printf("received %v on %v", r.Method, path)
paths := emptyArray(strings.Split(path, "/")) paths := emptyArray(strings.Split(path, "/"))
Info.Println(fmt.Sprintf("paths %v", paths)) Info.Printf("paths %v\n", paths)
switch len(paths) { switch len(paths) {
case 1: case 1:
switch r.Method { switch r.Method {
@ -45,8 +45,8 @@ func routeFiles(w http.ResponseWriter, r *http.Request) {
uploadFile(w, r) uploadFile(w, r)
default: default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
Warning.Println(fmt.Sprintf("%v not allowed", r.Method)) Warning.Printf("%v not allowed\n", r.Method)
Error.Println(fmt.Sprintf("will return %v", http.StatusMethodNotAllowed)) Error.Printf("will return %v\n", http.StatusMethodNotAllowed)
} }
return return
case 2: case 2:
@ -57,8 +57,8 @@ func routeFiles(w http.ResponseWriter, r *http.Request) {
deleteFile(w, r, paths[1]) deleteFile(w, r, paths[1])
default: default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
Warning.Println(fmt.Sprintf("%v not allowed", r.Method)) Warning.Printf("%v not allowed\n", r.Method)
Error.Println(fmt.Sprintf("will return %v", http.StatusMethodNotAllowed)) Error.Printf("will return %v\n", http.StatusMethodNotAllowed)
} }
default: default:
http.Error(w, "Not found", http.StatusNotFound) http.Error(w, "Not found", http.StatusNotFound)
@ -87,10 +87,10 @@ func main() {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", route) mux.HandleFunc("/", route)
port := fmt.Sprintf(":%v", SFU_PORT) port := fmt.Sprintf(":%v", SFU_PORT)
Info.Println(fmt.Sprintf("running SFU on port %v", port)) Info.Printf("running SFU on port %v\n", port)
err := http.ListenAndServe(port, mux) err := http.ListenAndServe(port, mux)
if err != nil { if err != nil {
Error.Println(fmt.Sprintf("%v port may not be available", port)) Error.Printf("%v port may not be available\n", port)
os.Exit(1) os.Exit(1)
} }
} }