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() app := iris.New() crs := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts. AllowCredentials: false, }) 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("./%s/%v", curTime.Format("2006-01-02"), curTime.UnixNano())+"_data.json", file, 0644) if err != nil { t := time.Now().UTC() newpath := filepath.Join(".", t.Format("2006-01-02")) os.MkdirAll(newpath, os.ModePerm) _ = ioutil.WriteFile(fmt.Sprintf("./%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, ) }