package main

import (
	"github.com/gorilla/websocket"
	"github.com/ssgo/log"
	"github.com/ssgo/s"
)

var conn *websocket.Conn

type Message struct {
	Type      string
	Url       string
	ImageData string
	Text      string
}

func main() {
	s.Config.Listen = "9000"
	s.Static("/", "../web/")
	s.RegisterSimpleWebsocket(0, "/socket", func(connect *websocket.Conn) {
		log.DefaultLogger.Info("Socket connected")
		conn = connect
		for {
			data := &Message{}
			err := conn.ReadJSON(data)
			if err != nil {
				log.DefaultLogger.Error("Read WS error", "err", err.Error())
				return
			}
			go onMessage(data)
		}
	}, "")
	s.Start()
}

func onMessage(data *Message) {
	switch data.Type {
	case "checkPhishing":
		checkUrl(data.Url)
	case "checkImage":
		checkImg(data.ImageData)
	case "checkScript":
		checkText(data.Text)

	}
}
