Skip to content

Commit

Permalink
Merge pull request #117 from ZeruLight/feature/screenshot-api
Browse files Browse the repository at this point in the history
Feature/screenshot api
  • Loading branch information
stratic-dev authored Mar 20, 2024
2 parents fc13b1b + 295ff65 commit 449c3e4
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 78 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ savedata/*/
*.exe
*.lnk
*.bat
/docker/db-data
/docker/db-data
screenshots/*
12 changes: 9 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
],
"PatchServerManifest": "",
"PatchServerFile": "",
"ScreenshotAPIURL": "",
"Screenshots":{
"Enabled":true,
"Host":"127.0.0.1",
"Port":8080,
"OutputDir":"screenshots",
"UploadQuality":100
},
"DeleteOnSaveCorruption": false,
"ClientMode": "ZZ",
"QuestCacheExpiry": 300,
Expand Down Expand Up @@ -189,8 +195,8 @@
"Enabled": true,
"Port": 53312
},
"SignV2": {
"Enabled": false,
"API": {
"Enabled": true,
"Port": 8080,
"PatchServer": "",
"Banners": [],
Expand Down
47 changes: 28 additions & 19 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ type Config struct {
LoginNotices []string // MHFML string of the login notices displayed
PatchServerManifest string // Manifest patch server override
PatchServerFile string // File patch server override
ScreenshotAPIURL string // Destination for screenshots uploaded to BBS
DeleteOnSaveCorruption bool // Attempts to save corrupted data will flag the save for deletion
ClientMode string
RealClientMode Mode
Expand All @@ -87,16 +86,18 @@ type Config struct {
EarthID int32
EarthMonsters []int32
SaveDumps SaveDumpOptions
DebugOptions DebugOptions
GameplayOptions GameplayOptions
Discord Discord
Commands []Command
Courses []Course
Database Database
Sign Sign
SignV2 SignV2
Channel Channel
Entrance Entrance
Screenshots ScreenshotsOptions

DebugOptions DebugOptions
GameplayOptions GameplayOptions
Discord Discord
Commands []Command
Courses []Course
Database Database
Sign Sign
API API
Channel Channel
Entrance Entrance
}

type SaveDumpOptions struct {
Expand All @@ -105,6 +106,14 @@ type SaveDumpOptions struct {
OutputDir string
}

type ScreenshotsOptions struct {
Enabled bool
Host string // Destination for screenshots uploaded to BBS
Port uint32 // Port for screenshots API
OutputDir string
UploadQuality int //Determines the upload quality to the server
}

// DebugOptions holds various debug/temporary options for use while developing Erupe.
type DebugOptions struct {
CleanDB bool // Automatically wipes the DB on server reset.
Expand Down Expand Up @@ -228,29 +237,29 @@ type Sign struct {
Port int
}

// SignV2 holds the new sign server config
type SignV2 struct {
// API holds server config
type API struct {
Enabled bool
Port int
PatchServer string
Banners []SignV2Banner
Messages []SignV2Message
Links []SignV2Link
Banners []APISignBanner
Messages []APISignMessage
Links []APISignLink
}

type SignV2Banner struct {
type APISignBanner struct {
Src string `json:"src"` // Displayed image URL
Link string `json:"link"` // Link accessed on click
}

type SignV2Message struct {
type APISignMessage struct {
Message string `json:"message"` // Displayed message
Date int64 `json:"date"` // Displayed date
Kind int `json:"kind"` // 0 for 'Default', 1 for 'New'
Link string `json:"link"` // Link accessed on click
}

type SignV2Link struct {
type APISignLink struct {
Name string `json:"name"` // Displayed name
Icon string `json:"icon"` // Displayed icon. It will be cast as a monochrome color as long as it is transparent.
Link string `json:"link"` // Link accessed on click
Expand Down
22 changes: 11 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"syscall"
"time"

"erupe-ce/server/api"
"erupe-ce/server/channelserver"
"erupe-ce/server/discordbot"
"erupe-ce/server/entranceserver"
"erupe-ce/server/signserver"
"erupe-ce/server/signv2server"

"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
Expand Down Expand Up @@ -181,21 +181,21 @@ func main() {
}

// New Sign server
var newSignServer *signv2server.Server
if config.SignV2.Enabled {
newSignServer = signv2server.NewServer(
&signv2server.Config{
var ApiServer *api.APIServer
if config.API.Enabled {
ApiServer = api.NewAPIServer(
&api.Config{
Logger: logger.Named("sign"),
ErupeConfig: _config.ErupeConfig,
DB: db,
})
err = newSignServer.Start()
err = ApiServer.Start()
if err != nil {
preventClose(fmt.Sprintf("SignV2: Failed to start, %s", err.Error()))
preventClose(fmt.Sprintf("API: Failed to start, %s", err.Error()))
}
logger.Info("SignV2: Started successfully")
logger.Info("API: Started successfully")
} else {
logger.Info("SignV2: Disabled")
logger.Info("API: Disabled")
}

var channels []*channelserver.Server
Expand Down Expand Up @@ -273,8 +273,8 @@ func main() {
signServer.Shutdown()
}

if config.SignV2.Enabled {
newSignServer.Shutdown()
if config.API.Enabled {
ApiServer.Shutdown()
}

if config.Entrance.Enabled {
Expand Down
22 changes: 12 additions & 10 deletions server/signv2server/signv2_server.go → server/api/api_server.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package signv2server
package api

import (
"context"
"erupe-ce/config"
_config "erupe-ce/config"
"fmt"
"net/http"
"os"
Expand All @@ -21,8 +21,8 @@ type Config struct {
ErupeConfig *_config.Config
}

// Server is the MHF custom launcher sign server.
type Server struct {
// APIServer is Erupes Standard API interface
type APIServer struct {
sync.Mutex
logger *zap.Logger
erupeConfig *_config.Config
Expand All @@ -31,9 +31,9 @@ type Server struct {
isShuttingDown bool
}

// NewServer creates a new Server type.
func NewServer(config *Config) *Server {
s := &Server{
// NewAPIServer creates a new Server type.
func NewAPIServer(config *Config) *APIServer {
s := &APIServer{
logger: config.Logger,
erupeConfig: config.ErupeConfig,
db: config.DB,
Expand All @@ -43,7 +43,7 @@ func NewServer(config *Config) *Server {
}

// Start starts the server in a new goroutine.
func (s *Server) Start() error {
func (s *APIServer) Start() error {
// Set up the routes responsible for serving the launcher HTML, serverlist, unique name check, and JP auth.
r := mux.NewRouter()
r.HandleFunc("/launcher", s.Launcher)
Expand All @@ -52,9 +52,11 @@ func (s *Server) Start() error {
r.HandleFunc("/character/create", s.CreateCharacter)
r.HandleFunc("/character/delete", s.DeleteCharacter)
r.HandleFunc("/character/export", s.ExportSave)
r.HandleFunc("/api/ss/bbs/upload.php", s.ScreenShot)
r.HandleFunc("/api/ss/bbs/{id}", s.ScreenShotGet)
handler := handlers.CORS(handlers.AllowedHeaders([]string{"Content-Type"}))(r)
s.httpServer.Handler = handlers.LoggingHandler(os.Stdout, handler)
s.httpServer.Addr = fmt.Sprintf(":%d", s.erupeConfig.SignV2.Port)
s.httpServer.Addr = fmt.Sprintf(":%d", s.erupeConfig.API.Port)

serveError := make(chan error, 1)
go func() {
Expand All @@ -74,7 +76,7 @@ func (s *Server) Start() error {
}

// Shutdown exits the server gracefully.
func (s *Server) Shutdown() {
func (s *APIServer) Shutdown() {
s.logger.Debug("Shutting down")

s.Lock()
Expand Down
18 changes: 9 additions & 9 deletions server/signv2server/dbutils.go → server/api/dbutils.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package signv2server
package api

import (
"context"
Expand All @@ -10,7 +10,7 @@ import (
"golang.org/x/crypto/bcrypt"
)

func (s *Server) createNewUser(ctx context.Context, username string, password string) (uint32, uint32, error) {
func (s *APIServer) createNewUser(ctx context.Context, username string, password string) (uint32, uint32, error) {
// Create salted hash of user password
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
Expand All @@ -32,7 +32,7 @@ func (s *Server) createNewUser(ctx context.Context, username string, password st
return id, rights, err
}

func (s *Server) createLoginToken(ctx context.Context, uid uint32) (uint32, string, error) {
func (s *APIServer) createLoginToken(ctx context.Context, uid uint32) (uint32, string, error) {
loginToken := token.Generate(16)
var tid uint32
err := s.db.QueryRowContext(ctx, "INSERT INTO sign_sessions (user_id, token) VALUES ($1, $2) RETURNING id", uid, loginToken).Scan(&tid)
Expand All @@ -42,7 +42,7 @@ func (s *Server) createLoginToken(ctx context.Context, uid uint32) (uint32, stri
return tid, loginToken, nil
}

func (s *Server) userIDFromToken(ctx context.Context, token string) (uint32, error) {
func (s *APIServer) userIDFromToken(ctx context.Context, token string) (uint32, error) {
var userID uint32
err := s.db.QueryRowContext(ctx, "SELECT user_id FROM sign_sessions WHERE token = $1", token).Scan(&userID)
if err == sql.ErrNoRows {
Expand All @@ -53,7 +53,7 @@ func (s *Server) userIDFromToken(ctx context.Context, token string) (uint32, err
return userID, nil
}

func (s *Server) createCharacter(ctx context.Context, userID uint32) (Character, error) {
func (s *APIServer) createCharacter(ctx context.Context, userID uint32) (Character, error) {
var character Character
err := s.db.GetContext(ctx, &character,
"SELECT id, name, is_female, weapon_type, hr, gr, last_login FROM characters WHERE is_new_character = true AND user_id = $1 LIMIT 1",
Expand All @@ -78,7 +78,7 @@ func (s *Server) createCharacter(ctx context.Context, userID uint32) (Character,
return character, err
}

func (s *Server) deleteCharacter(ctx context.Context, userID uint32, charID uint32) error {
func (s *APIServer) deleteCharacter(ctx context.Context, userID uint32, charID uint32) error {
var isNew bool
err := s.db.QueryRow("SELECT is_new_character FROM characters WHERE id = $1", charID).Scan(&isNew)
if err != nil {
Expand All @@ -92,7 +92,7 @@ func (s *Server) deleteCharacter(ctx context.Context, userID uint32, charID uint
return err
}

func (s *Server) getCharactersForUser(ctx context.Context, uid uint32) ([]Character, error) {
func (s *APIServer) getCharactersForUser(ctx context.Context, uid uint32) ([]Character, error) {
var characters []Character
err := s.db.SelectContext(
ctx, &characters, `
Expand All @@ -107,7 +107,7 @@ func (s *Server) getCharactersForUser(ctx context.Context, uid uint32) ([]Charac
return characters, nil
}

func (s *Server) getReturnExpiry(uid uint32) time.Time {
func (s *APIServer) getReturnExpiry(uid uint32) time.Time {
var returnExpiry, lastLogin time.Time
s.db.Get(&lastLogin, "SELECT COALESCE(last_login, now()) FROM users WHERE id=$1", uid)
if time.Now().Add((time.Hour * 24) * -90).After(lastLogin) {
Expand All @@ -124,7 +124,7 @@ func (s *Server) getReturnExpiry(uid uint32) time.Time {
return returnExpiry
}

func (s *Server) exportSave(ctx context.Context, uid uint32, cid uint32) (map[string]interface{}, error) {
func (s *APIServer) exportSave(ctx context.Context, uid uint32, cid uint32) (map[string]interface{}, error) {
row := s.db.QueryRowxContext(ctx, "SELECT * FROM characters WHERE id=$1 AND user_id=$2", cid, uid)
result := make(map[string]interface{})
err := row.MapScan(result)
Expand Down
Loading

0 comments on commit 449c3e4

Please sign in to comment.