Files
backend-server/main.go
Josef Fröhle 40d569158f remove dumps and trash
add some new files and services

add hwtools that shows the current and last GWs - not yet included the user stats (if any available)
2020-07-07 19:00:49 +02:00

111 lines
3.3 KiB
Go

package main
import (
"encoding/json"
"fmt"
"hwcollector/server/services"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/iris-contrib/middleware/cors"
"github.com/kataras/iris/v12"
)
//DEBUG is debug?
const DEBUG bool = true
func main() {
srvs := services.MainServices(DEBUG)
//go srvs.DataPreProcessing.RunPath()
_ = srvs
app := iris.New()
crs := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts.
AllowCredentials: false,
})
app.Get("/getsource", func(ctx iris.Context) {
limit := 500.0 * iris.KB
burst := 750 * iris.KB
ctx.SendFileWithRate("./files_golang_db.tar.gz", "files_golang_db.tar.gz", limit, burst)
})
app.HandleDir("/hwtools/", "./hwtools/", iris.DirOptions{
// Defaults to "/index.html", if request path is ending with **/*/$IndexName
// then it redirects to **/*(/) which another handler is handling it,
// that another handler, called index handler, is auto-registered by the framework
// if end developer does not managed to handle it by hand.
IndexName: "/index.html",
// When files should served under compression.
Gzip: true,
// List the files inside the current requested directory if `IndexName` not found.
ShowList: false,
// If `ShowList` is true then this function will be used instead of the default one to show the list of files of a current requested directory(dir).
// DirList: func(ctx iris.Context, dirName string, dir http.File) error { ... }
//
// Optional validator that loops through each requested resource.
// AssetValidator: func(ctx iris.Context, name string) bool { ... }
})
v1 := app.Party("/", crs).AllowMethods(iris.MethodOptions) // <- important for the preflight.
{
v1.Post("/", func(ctx iris.Context) {
var data map[string]interface{}
err := ctx.ReadJSON(&data)
if err != nil {
ctx.StopWithStatus(iris.StatusBadRequest)
ctx.WriteString(err.Error())
ctx.WriteString(err.Error())
return
}
file, _ := json.MarshalIndent(data, "", " ")
curTime := time.Now().UTC()
err = ioutil.WriteFile(fmt.Sprintf("./_data_/%s/%v", curTime.Format("2006-01-02"), curTime.UnixNano())+"_data.json", file, 0644)
if err != nil {
t := time.Now().UTC()
newpath := filepath.Join(".", "_data_", t.Format("2006-01-02"))
os.MkdirAll(newpath, os.ModePerm)
_ = ioutil.WriteFile(fmt.Sprintf("./_data_/%s/%v", curTime.Format("2006-01-02"), curTime.UnixNano())+"_data.json", file, 0644)
}
ctx.StopWithJSON(iris.StatusAccepted, iris.Map{
"success": true,
})
})
v1.Post("/data", func(ctx iris.Context) {
data, err := ctx.GetBody()
if err != nil {
ctx.StopWithJSON(iris.StatusUnprocessableEntity, iris.Map{
"error": true,
"errormessage": err,
})
return
}
err = srvs.DataPreProcessing.RunBody(data)
if err != nil {
ctx.StopWithJSON(iris.StatusUnprocessableEntity, iris.Map{
"error": true,
"errormessage": err,
})
return
}
ctx.StopWithJSON(iris.StatusAccepted, iris.Map{
"success": true,
})
})
}
app.Listen(":80",
iris.WithCharset("UTF-8"),
iris.WithoutBodyConsumptionOnUnmarshal,
iris.WithFireMethodNotAllowed,
iris.WithEmptyFormError,
iris.WithPostMaxMemory(5*iris.MB),
iris.WithOptimizations,
//iris.WithoutPathCorrection,
)
}