Quick Start
Quick Start: Building an Application
This article will guide you through quickly building a minimal GMSSH application from scratch, without relying on preset templates, giving you a deep understanding of the basic structure and development process.
Applicable Scenarios
- Developers who want complete control over project structure
- Complex applications requiring custom configurations
- Learning GMSSH application development principles
Prerequisites
1. Create Application
First, you need to create a new application in the Developer Center and select an empty project template:
Creation Steps
- Log in to Developer Center
- Click "Create New Application"
- Select "Empty Project" template
- Fill in basic application information
If you are not familiar with the application creation process, please refer to Create Application Guide.
2. Project Directory Structure
After the application is created, you need to manually build the project directory structure. Here I choose Python for backend demonstration. The recommended standard structure is as follows:
project-root/
├── backend/ # Backend code directory
│ ├── build.sh # Backend build script
│ ├── main.py # Main program entry file
│ └── requirements.txt # Python dependency configuration
├── web/ # Frontend code directory
│ ├── build.sh # Frontend build script
│ └── index.html # Frontend entry page
├── .gitignore # Git ignore file configuration
└── makefile # Project build configuration fileCore Code Implementation
Backend Service Implementation
To meet different developers' tech stack preferences, we provide backend service implementation examples in multiple programming languages. Each example implements the same API interfaces, and you can choose the appropriate implementation based on your technical background.
Selection Recommendations
- Python: Suitable for rapid prototyping and data processing scenarios
- Go: Suitable for applications requiring high performance and concurrency
Python Implementation (Flask)
A lightweight implementation based on the Flask framework, suitable for rapid development and prototype validation:
Click to view complete Python code
File path: backend/main.py
import socket
from flask import Flask, jsonify
app = Flask(__name__)
@app.post("/hello")
def hello():
return jsonify(code=200,data="hello world!", msg="OK")
@app.get("/ping")
def ping():
return jsonify(code=200, data="ping", msg="OK")
def get_free_port():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 0)) # Port 0 lets the OS assign automatically
return s.getsockname()[1]
if __name__ == "__main__":
# port = get_free_port()
# For debugging convenience, specify port directly; use get_free_port() in production
app.run(host="0.0.0.0", port=8899, debug=True)How to run:
# Install dependencies
pip install flask
# Start service
python main.pyGo Implementation (Gin)
A high-performance implementation based on the Gin framework, suitable for production environments and high-concurrency scenarios:
Click to view complete Go code
File path: backend/main.go
package main
import (
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"net"
"net/http"
"os"
"os/signal"
"syscall"
)
// Use this file address for testing
const PORT_FILE = "app.port"
// For production release, use this address, replace with your app name
// const PORT_FILE = "/.__gmssh/plugin/{org-name}/{app-name}/tmp/app.port"
func getFreePort() (int, error) {
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
return 0, err
}
defer listener.Close()
addr := listener.Addr().(*net.TCPAddr)
return addr.Port, nil
}
// Write port number to file
func writePortToFile(port int) error {
return os.WriteFile(PORT_FILE, []byte(fmt.Sprintf("%d", port)), 0644)
}
// Delete port file
func removePortFile() {
if _, err := os.Stat(PORT_FILE); err == nil {
if err := os.Remove(PORT_FILE); err != nil {
fmt.Printf("Failed to delete port file: %v\n", err)
} else {
fmt.Println("Port file deleted successfully")
}
}
}
// Setup signal handler
func setupSignalHandler() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
sig := <-c
fmt.Printf("Received signal: %s, cleaning up resources...\n", sig)
removePortFile()
os.Exit(0)
}()
}
func main() {
setupSignalHandler()
defer func() {
if r := recover(); r != nil {
fmt.Printf("Program panicked: %v, cleaning up resources...\n", r)
removePortFile()
panic(r)
}
}()
r := gin.Default()
r.POST("/hello", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"data": "hello world!",
"msg": "OK",
})
})
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"data": "ping",
"msg": "OK",
})
})
port := 8899
if err := writePortToFile(port); err != nil {
panic(fmt.Sprintf("Failed to write port to file: %v", err))
}
fmt.Printf("Port %d written to file\n", port)
addr := fmt.Sprintf(":%d", port)
fmt.Printf("Server starting on port: %d\n", port)
if err := r.Run(addr); err != nil {
removePortFile()
panic(fmt.Sprintf("Server failed to start: %v", err))
}
removePortFile()
}How to run:
# Initialize module
go mod init backend
# Install dependencies
go get github.com/gin-gonic/gin
go get github.com/gin-contrib/cors
# Start service
go run main.goAPI Interface Description
Both implementations provide the same API interfaces:
POST /hello- Returns a greeting messageGET /ping- Health check endpoint
All interfaces return a unified JSON format: {"code": 200, "data": "...", "msg": "OK"}
Frontend Code Examples
HTML Project Template
Vue Project Template
React Project Template
Directory Description
- backend/: Contains all backend-related code and configuration
- web/: Contains frontend static resources and page files
- build.sh: Build script for each module, used for compiling and packaging
- makefile: Unified build management file, execute build tasks via
makecommand
Running and Testing
Local Development Debugging
- Upload backend code: Upload
main.pyto the server and start the service - Create debug application: Create a debug application in devtools, see Create Debug Application
- Start frontend debugging: Debug the frontend locally, see Debug Frontend
Quick Start
- Install dependencies:
cd backend && pip install -r requirements.txt - Start backend:
python backend/main.py - Access application: Access your application through the developer tools
Pre-Release Checklist
Before releasing the application, ensure:
- ✅ Application details are complete
- ✅ Frontend and backend
build.shscripts build correctly - ✅ All functionality tests pass
- ✅ Code complies with platform specifications
Notes
- Ensure build scripts run correctly, otherwise the application may not be listed
- Recommend thorough functional testing before release
- Configure appropriate server environment for production deployment
Next Steps
Congratulations! You have successfully created a basic GMSSH application. Next, you can use the advanced SDK to develop applications.
