From d976bb4a59d6258bf8ea6f6e9a326923a1df5241 Mon Sep 17 00:00:00 2001 From: rouggy Date: Wed, 28 Aug 2024 09:36:43 +0700 Subject: [PATCH] update --- RaceBot.go | 5 ++-- api/server.go | 48 -------------------------------- config/config.toml | 2 +- internal/database/race.go | 1 + internal/database/release.go | 1 + models/release.go | 2 ++ server/server.go | 53 ++++++++++++++++++++++++++++++++++++ 7 files changed, 61 insertions(+), 51 deletions(-) delete mode 100644 api/server.go create mode 100644 internal/database/race.go create mode 100644 internal/database/release.go create mode 100644 server/server.go diff --git a/RaceBot.go b/RaceBot.go index 00450ec..ebd5de1 100644 --- a/RaceBot.go +++ b/RaceBot.go @@ -5,10 +5,10 @@ import ( "os" "path/filepath" - "git.rouggy.com/rouggy/RaceBot/api" "git.rouggy.com/rouggy/RaceBot/internal/config" "git.rouggy.com/rouggy/RaceBot/internal/database" "git.rouggy.com/rouggy/RaceBot/models" + "git.rouggy.com/rouggy/RaceBot/server" "github.com/jinzhu/gorm" ) @@ -30,11 +30,12 @@ func main() { SQLiteMigrate() - server := api.NewServer(cfg.Config.Host + ":" + cfg.Config.Port) + server := server.NewServer(cfg.Config.Host + ":" + cfg.Config.Port) server.Start(cfg.Config) if err != nil { fmt.Println("Could not start server") } + } func SQLiteMigrate() { diff --git a/api/server.go b/api/server.go deleted file mode 100644 index 5fcf5ca..0000000 --- a/api/server.go +++ /dev/null @@ -1,48 +0,0 @@ -package api - -import ( - "fmt" - - "git.rouggy.com/rouggy/RaceBot/controllers" - "git.rouggy.com/rouggy/RaceBot/internal/domain" - "github.com/gin-gonic/gin" -) - -type Server struct { - listenAddr string -} - -func NewServer(listenAddr string) *Server { - return &Server{ - listenAddr: listenAddr, - } -} - -func (s *Server) Start(cfg *domain.Config) error { - gin.SetMode(gin.ReleaseMode) - r := gin.Default() - s.SetupRoutes(r) - fmt.Println("[Server] Server is running on port", cfg.Port) - err := r.Run(s.listenAddr) - if err != nil { - return err - } - return nil -} - -func (s *Server) SetupRoutes(app *gin.Engine) { - - api := app.Group("/api") - v1 := api.Group("/v1") - - v1.GET("/races", controllers.GetAllRaces) - //v1.GET("/races/:id", controllers.GetReleaseById) - //v1.POST("/races", controllers.CreateRace) - //v1.PUT("/races/:id", controllers.UpdateRace) - //v1.DELETE("/races/:id", controllers.DeleteRace) - //v1.GET("/releases", controllers.GetReleases) - //v1.GET("/releases/:id", controllers.GetReleaseById) - v1.POST("/preraces", controllers.CreatePreRace) - //v1.PUT("/releases/:id", controllers.UpdateRelease) - //v1.DELETE("/releases/:id", controllers.DeleteRelease) -} diff --git a/config/config.toml b/config/config.toml index dac64e3..c959a6f 100644 --- a/config/config.toml +++ b/config/config.toml @@ -3,5 +3,5 @@ host = "127.0.0.1" port = "3000" # TMDbApiKey is required tmdbApiKey = "" -dbName = "racer.db" +dbName = "racer" uploadFolder = "/home/rouggy/torrents/rtorrent/Race" diff --git a/internal/database/race.go b/internal/database/race.go new file mode 100644 index 0000000..636bab8 --- /dev/null +++ b/internal/database/race.go @@ -0,0 +1 @@ +package database diff --git a/internal/database/release.go b/internal/database/release.go new file mode 100644 index 0000000..636bab8 --- /dev/null +++ b/internal/database/release.go @@ -0,0 +1 @@ +package database diff --git a/models/release.go b/models/release.go index 39236ac..443cf0f 100644 --- a/models/release.go +++ b/models/release.go @@ -25,6 +25,8 @@ type Release struct { Language string `gorm:"many2many:releases_languages;column:language;varchar(20)" form:"language" json:"language" binding:"max=20"` ReleaseType string `gorm:"column:release_type;varchar(20)" form:"release_type" json:"release_type" binding:"max=20"` Year int `gorm:"column:year;integer" form:"year" json:"year" binding:"max=4"` + TmDB int `gorm:"column:tmdb;integer" form:"tmdb" json:"tmdb" binding:"max=9"` + ImDB int `gorm:"column:imdb;integer" form:"imdb" json:"imdb" binding:"max=9"` } func NewRelease() *Release { diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..cba7eb5 --- /dev/null +++ b/server/server.go @@ -0,0 +1,53 @@ +package server + +import ( + "fmt" + "net" + + "git.rouggy.com/rouggy/RaceBot/internal/domain" +) + +type Server struct { + listenAddr string + listener net.Listener + Conn net.Conn +} + +func NewServer(listenAddr string) *Server { + return &Server{ + listenAddr: listenAddr, + } +} + +func (s *Server) Start(cfg *domain.Config) { + tcpAddr, _ := net.ResolveTCPAddr("tcp", cfg.Host+":"+cfg.Port) + + s.listener, _ = net.ListenTCP("tcp", tcpAddr) + + fmt.Println("[Server] Server is running on port", cfg.Port) + + for { + s.Conn, _ = s.listener.Accept() + go s.handleRequest() + } +} + +func (s *Server) handleRequest() { + //we make a buffer to hold the incoming data. + buf := make([]byte, 1024) + + // Read the incoming connection into the buffer. + reqLen, err := s.Conn.Read(buf) + if err != nil { + fmt.Println("Error reading:", err.Error()) + } + + // Print the message to the console. + fmt.Println("Received data:", string(buf[:reqLen])) + + // Write a response back to the client. + s.Conn.Write([]byte("Message received.")) + + // Close the connection when you're done with it. + s.Conn.Close() +}