This commit is contained in:
Gregory Salaun 2024-08-28 09:36:43 +07:00
parent cfc3a7cbe1
commit d976bb4a59
7 changed files with 61 additions and 51 deletions

View File

@ -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() {

View File

@ -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)
}

View File

@ -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"

View File

@ -0,0 +1 @@
package database

View File

@ -0,0 +1 @@
package database

View File

@ -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 {

53
server/server.go Normal file
View File

@ -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()
}