mirror of
https://codeberg.org/scip/ephemerup.git
synced 2025-12-17 04:30:57 +01:00
changes:
- added unit tests
- put all subcmds into one file
- use io.Writer for output, better for testing
- added upload form support
- added api docs
- generalized db engine
- added mail notify support for forms
- enhanced server/SetupAuthStore() to also look up form ids
- added form template (put into .go file by Makefile
- renamed project
This commit is contained in:
102
cmd/formtemplate.go
Normal file
102
cmd/formtemplate.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package cmd
|
||||
|
||||
const formtemplate = `
|
||||
<!DOCTYPE html>
|
||||
<!-- -*-web-*- -->
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="description" content="upload form" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>File upload form</title>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-aFq/bzH65dt+w6FI2ooMVUpc+21e0SRygnTpmBvdBgSdnuTN7QbdgL+OapgHtvPp" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h4>Upload form {{ .Id }}</h4>
|
||||
<!-- Response -->
|
||||
<div class="statusMsg"></div>
|
||||
|
||||
<!-- File upload form -->
|
||||
<div class="col-lg-12">
|
||||
<form id="UploadForm" enctype="multipart/form-data" action="/v1/uploads" method="POST">
|
||||
<div class="mb-3 row">
|
||||
<p>
|
||||
Use this form to upload one or more files. The creator of the form will automatically get notified.
|
||||
</p>
|
||||
</div>
|
||||
<div class="mb-3 row">
|
||||
<label for="file" class="col-sm-2 col-form-label">Select</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="file" class="form-control" id="file" name="uploads[]" multiple
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3 row">
|
||||
<label for="display" class="col-sm-2 col-form-label">Selected Files</label>
|
||||
<div class="col-sm-10">
|
||||
<!-- <input type="textara" class="form-control" id="upload-file-info" readonly>-->
|
||||
<div id="upload-file-info"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<input type="submit" name="submit" class="btn btn-success submitBtn" value="Upload"/>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/js/bootstrap.bundle.min.js"
|
||||
integrity="sha384-qKXV1j0HvMUeCBQ+QVp7JcfGl760yU08IQ+GpUo5hlbpg51QRiuqHAJz8+BrxE/N" crossorigin="anonymous"></script>
|
||||
<script>
|
||||
$(document).ready(function(){
|
||||
// Submit form data via Ajax
|
||||
$("#UploadForm").on('submit', function(e){
|
||||
e.preventDefault();
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: '/v1/uploads',
|
||||
data: new FormData(this),
|
||||
dataType: 'json',
|
||||
contentType: false,
|
||||
cache: false,
|
||||
processData:false,
|
||||
beforeSend: function(xhr){
|
||||
$('.submitBtn').attr("disabled","disabled");
|
||||
$('#UploadForm').css("opacity",".5");
|
||||
xhr.setRequestHeader('Authorization', 'Bearer {{.Id}}');
|
||||
},
|
||||
success: function(response){
|
||||
$('.statusMsg').html('');
|
||||
if(response.success){
|
||||
$('#UploadForm')[0].reset();
|
||||
$('.statusMsg').html('<p class="alert alert-success">Your upload is available at <code>'
|
||||
+response.uploads[0].url+'</code> for download</p>');
|
||||
$('#UploadForm').hide();
|
||||
}else{
|
||||
$('.statusMsg').html('<p class="alert alert-danger">'+response.message+'</p>');
|
||||
}
|
||||
$('#UploadForm').css("opacity","");
|
||||
$(".submitBtn").removeAttr("disabled");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$("#file").on('change', function() {
|
||||
$("#upload-file-info").empty();
|
||||
for (var i = 0; i < $(this).get(0).files.length; ++i) {
|
||||
$("#upload-file-info").append('<i class="bi-check-lg"></i> ' + $(this).get(0).files[i].name + '<br>');
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
`
|
||||
44
cmd/root.go
44
cmd/root.go
@@ -29,8 +29,8 @@ import (
|
||||
flag "github.com/spf13/pflag"
|
||||
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/tlinden/cenophane/api"
|
||||
"github.com/tlinden/cenophane/cfg"
|
||||
"github.com/tlinden/ephemerup/api"
|
||||
"github.com/tlinden/ephemerup/cfg"
|
||||
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -59,19 +59,20 @@ func Execute() error {
|
||||
f.BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging")
|
||||
f.StringVarP(&conf.Listen, "listen", "l", ":8080", "listen to custom ip:port (use [ip]:port for ipv6)")
|
||||
f.StringVarP(&conf.StorageDir, "storagedir", "s", "/tmp", "storage directory for uploaded files")
|
||||
f.StringVarP(&conf.ApiPrefix, "apiprefix", "a", "/api", "API endpoint path")
|
||||
f.StringVarP(&conf.ApiPrefix, "apiprefix", "a", "", "API endpoint path")
|
||||
f.StringVarP(&conf.Url, "url", "u", "", "HTTP endpoint w/o path")
|
||||
f.StringVarP(&conf.DbFile, "dbfile", "D", "/tmp/uploads.db", "Bold database file to use")
|
||||
f.StringVarP(&conf.Super, "super", "", "", "The API Context which has permissions on all contexts")
|
||||
f.StringVarP(&conf.Frontpage, "frontpage", "", "welcome to upload api, use /api enpoint!",
|
||||
"Content or filename to be displayed on / in case someone visits")
|
||||
f.StringVarP(&conf.Formpage, "formpage", "", "", "Content or filename to be displayed for forms (must be a go template)")
|
||||
|
||||
// server settings
|
||||
f.BoolVarP(&conf.V4only, "ipv4", "4", false, "Only listen on ipv4")
|
||||
f.BoolVarP(&conf.V6only, "ipv6", "6", false, "Only listen on ipv6")
|
||||
|
||||
f.BoolVarP(&conf.Prefork, "prefork", "p", false, "Prefork server threads")
|
||||
f.StringVarP(&conf.AppName, "appname", "n", "cenod "+conf.GetVersion(), "App name to say hi as")
|
||||
f.StringVarP(&conf.AppName, "appname", "n", "ephemerupd "+conf.GetVersion(), "App name to say hi as")
|
||||
f.IntVarP(&conf.BodyLimit, "bodylimit", "b", 10250000000, "Max allowed upload size in bytes")
|
||||
|
||||
f.Parse(os.Args[1:])
|
||||
@@ -91,10 +92,10 @@ func Execute() error {
|
||||
configfiles = []string{configfile}
|
||||
} else {
|
||||
configfiles = []string{
|
||||
"/etc/cenod.hcl", "/usr/local/etc/cenod.hcl", // unix variants
|
||||
filepath.Join(os.Getenv("HOME"), ".config", "cenod", "cenod.hcl"),
|
||||
filepath.Join(os.Getenv("HOME"), ".cenod"),
|
||||
"cenod.hcl",
|
||||
"/etc/ephemerupd.hcl", "/usr/local/etc/ephemerupd.hcl", // unix variants
|
||||
filepath.Join(os.Getenv("HOME"), ".config", "ephemerupd", "ephemerupd.hcl"),
|
||||
filepath.Join(os.Getenv("HOME"), ".ephemerupd"),
|
||||
"ephemerupd.hcl",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,9 +109,9 @@ func Execute() error {
|
||||
}
|
||||
|
||||
// env overrides config file
|
||||
k.Load(env.Provider("CENOD_", ".", func(s string) string {
|
||||
k.Load(env.Provider("EPHEMERUPD_", ".", func(s string) string {
|
||||
return strings.Replace(strings.ToLower(
|
||||
strings.TrimPrefix(s, "CENOD_")), "_", ".", -1)
|
||||
strings.TrimPrefix(s, "EPHEMERUPD_")), "_", ".", -1)
|
||||
}), nil)
|
||||
|
||||
// command line overrides env
|
||||
@@ -142,6 +143,23 @@ func Execute() error {
|
||||
}
|
||||
}
|
||||
|
||||
// Formpage?
|
||||
if conf.Formpage != "" {
|
||||
if _, err := os.Stat(conf.Formpage); err == nil {
|
||||
// it's a filename, try to use it
|
||||
content, err := ioutil.ReadFile(conf.Formpage)
|
||||
if err != nil {
|
||||
return errors.New("error loading config: " + err.Error())
|
||||
}
|
||||
|
||||
// replace the filename
|
||||
conf.Formpage = string(content)
|
||||
}
|
||||
} else {
|
||||
// use builtin default
|
||||
conf.Formpage = formtemplate
|
||||
}
|
||||
|
||||
switch {
|
||||
case ShowVersion:
|
||||
fmt.Println(cfg.Getversion())
|
||||
@@ -157,11 +175,11 @@ func Execute() error {
|
||||
|
||||
Multiple env vars are supported in this format:
|
||||
|
||||
CENOD_CONTEXT_$(NAME)="<context>:<key>"
|
||||
EPHEMERUPD_CONTEXT_$(NAME)="<context>:<key>"
|
||||
|
||||
eg:
|
||||
|
||||
CENOD_CONTEXT_SUPPORT="support:tymag-fycyh-gymof-dysuf-doseb-puxyx"
|
||||
EPHEMERUPD_CONTEXT_SUPPORT="support:tymag-fycyh-gymof-dysuf-doseb-puxyx"
|
||||
^^^^^^^- doesn't matter.
|
||||
|
||||
Modifies cfg.Config directly
|
||||
@@ -171,7 +189,7 @@ func GetApicontextsFromEnv(conf *cfg.Config) {
|
||||
|
||||
for _, envvar := range os.Environ() {
|
||||
pair := strings.SplitN(envvar, "=", 2)
|
||||
if strings.HasPrefix(pair[0], "CENOD_CONTEXT_") {
|
||||
if strings.HasPrefix(pair[0], "EPHEMERUPD_CONTEXT_") {
|
||||
c := strings.SplitN(pair[1], ":", 2)
|
||||
if len(c) == 2 {
|
||||
contexts = append(contexts, cfg.Apicontext{Context: c[0], Key: c[1]})
|
||||
|
||||
Reference in New Issue
Block a user