package main

import (
	"sparrow/pkg/server"

	"github.com/iris-contrib/middleware/cors"
	"github.com/kataras/iris"
)

func main() {
	err := server.Init("fileaccess")
	if err != nil {
		server.Log.Fatal(err)
		return
	}
	app := iris.New()
	app.Use(iris.Gzip)
	iris.WithPostMaxMemory(int64(*conMaxSize))
	opts := cors.Options{
		AllowedOrigins: []string{"*"},
		AllowedHeaders: []string{"Content-Type"},
		AllowedMethods: []string{"POST"},
		ExposedHeaders: []string{"X-Header"},
	}
	app.Use(cors.New(opts))
	app.AllowMethods(iris.MethodOptions)
	app.StaticWeb("/upload", *conStaticPath)
	registerRouter(app)
	app.Build()
	// register a http handler
	err = server.RegisterHTTPHandler(app)
	if err != nil {
		server.Log.Errorf("RegisterHTTPHandler Error: %s", err)
		return
	}
	fileaccess := NewFileAccess()
	err = server.RegisterRPCHandler(fileaccess)
	if err != nil {
		server.Log.Errorf("RegisterRPCHandler Error: %s", err)
		return
	}
	// go
	err = server.Run()
	if err != nil {
		server.Log.Fatal(err)
	}
}
