-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
60 lines (56 loc) · 1.69 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"strconv"
"io/ioutil"
"encoding/json"
"path/filepath"
"github.com/kataras/iris"
"github.com/kardianos/osext"
"github.com/legion-zver/shield"
"github.com/iris-contrib/middleware/cors"
irisConfig "github.com/kataras/iris/config"
)
// Config - config app
type Config struct {
Port int64 `json:"port"`
CurrentDir string `json:"-"`
}
func main() {
exeFile, _ := osext.Executable()
currentDir, err := filepath.Abs(filepath.Dir(exeFile))
if err != nil {
fmt.Println(err)
return
}
config := Config{Port: 1234}
if data, err := ioutil.ReadFile(currentDir+"/config.json"); err == nil {
err = json.Unmarshal(data, &config)
if err != nil {
fmt.Println(err)
}
}
config.CurrentDir = currentDir
// Init NN
sh := shield.New(shield.NewRussianTokenizer(), shield.NewLevelDBStore(currentDir+"/db"))
// Config iris
sessionsConfig := irisConfig.DefaultSessions()
sessionsConfig.DisableSubdomainPersistence = true
irisConfig := irisConfig.Iris{DisableBanner: true, IsDevelopment: true, Sessions: sessionsConfig }
// Create API Iris
api := iris.New(irisConfig)
api.Use(cors.Default())
api.UseFunc(func (c *iris.Context){
c.Set("config", &config)
c.Set("shield", sh)
c.Next()
})
// 404
api.OnError(iris.StatusNotFound, func(c *iris.Context){
c.JSON(iris.StatusNotFound, iris.Map{"error":NewSimpleAPIError(404)})
})
// InitAPI
InitAPI(api.Party("/api"))
// Run
api.Listen(":"+strconv.FormatInt(int64(config.Port),10))
}