Singo: Simple Single Golang Web Service
go-crud has officially been renamed to Singo!
Build web services with Singo: use the simplest architecture to implement a practical framework that can serve massive numbers of users.
https://github.com/Gourouting/singo
- API testing is now supported.
- Go 1.25 is now supported. Please install this Go version before using this project.
Let's Build a G Site! Golang Full-Stack Programming Live Tutorial
Giligili, a Bilibili-like G site: https://github.com/Gourouting/giligili
Token-based mobile login example built with Singo: https://github.com/bydmm/singo-token-exmaple
This project uses a series of popular Golang components. You can use it as a foundation to quickly build RESTful web APIs.
This project integrates many components required for API development:
- Gin: a lightweight web framework that claims to have the fastest routing in Golang.
- GORM: an ORM tool. This project is intended to be used with MySQL.
- Gin-Session: a session management tool for the Gin framework.
- Go-Redis: a Golang Redis client.
- godotenv: an environment variable tool for development environments, making environment variables easier to use.
- Gin-Cors: a CORS middleware for the Gin framework.
- httpexpect: an API testing tool.
- Basic internationalization (i18n) functionality implemented in this project.
- This project stores login state with cookie-based sessions. You can switch to token authentication if needed.
This project has already implemented some common code for reference and reuse:
- A user model.
- The
/api/v1/user/registeruser registration endpoint. - The
/api/v1/user/loginuser login endpoint. - The
/api/v1/user/meuser profile endpoint, which requires a session after login. - The
/api/v1/user/logoutuser logout endpoint, which requires a session after login.
This project has also pre-created a series of folders for the following modules:
- The
apifolder is the controller layer of the MVC framework. It coordinates all parts to complete each task. - The
modelfolder stores database models and database operation code. - The
servicefolder handles more complex business logic. Modeling business logic can effectively improve business code quality, such as user registration, top-ups, and order placement. - The
serializerfolder stores common JSON models and converts database models frommodelinto JSON objects required by the API. - The
cachefolder contains Redis cache-related code. - The
authfolder contains permission control code. - The
utilfolder contains common utility helpers. - The
conffolder stores static configuration files. Translation-related configuration files are placed inlocales.
The project depends on the following environment variables at startup. You can also create a .env file in the project root to set environment variables more conveniently. This is recommended for development environments.
DB_USER="db_user" # MySQL user
DB_PASSWORD="db_password" # MySQL password
DB_HOST="127.0.0.1" # MySQL host
DB_PORT="3306" # MySQL port
DB_NAME="db_name" # MySQL database name
DB_CHARSET="utf8" # MySQL charset
DB_PARSE_TIME="True" # Parse MySQL time values
DB_LOC="Local" # MySQL time zone
REDIS_ADDR="127.0.0.1:6379" # Redis port and address
REDIS_PW="" # Redis connection password
REDIS_DB="" # Redis database, from 0 to 10
SESSION_SECRET="setOnProducation" # Session secret. It must be set and must not be leaked.
GIN_MODE="debug"This project uses Go Mod to manage dependencies.
go mod init singo
export GOPROXY=http://mirrors.aliyun.com/goproxy/
go run main.go // automatically installs dependenciesgo run main.goAfter the project starts, it runs on port 3000. You can change this; see the Gin documentation for details.
[New] This project includes built-in API tests.
- Make sure you are in the project root directory.
- Create a test-specific environment variable file in the
testdirectory.
cp test/.env.example test/.env- Modify the environment variables in
test/.envto ensure that MySQL and Redis can be connected normally. - Run tests from the project root and enable
-vto check whether the tests are running correctly.
go test -v ./test- After confirming that the tests run correctly, remove the
-vflag and check whether the tests pass.
go test ./test
ok singo/test (cached)