Server Config
Dokumentasi konfigurasi server HTTP.
Overview
Package config menyediakan function untuk konfigurasi server HTTP.
ServerConfiguration Struct
type ServerConfiguration struct {
Port string
Secret string
LimitCountPerRequest int64
}
ServerConfig
Mendapatkan alamat server (host:port).
func ServerConfig() string
Implementation
func ServerConfig() string {
viper.SetDefault("SERVER_HOST", "0.0.0.0")
viper.SetDefault("SERVER_PORT", "8000")
appServer := fmt.Sprintf("%s:%s",
viper.GetString("SERVER_HOST"),
viper.GetString("SERVER_PORT"),
)
log.Print("Server Running at :", appServer)
return appServer
}
Return Value
Format: HOST:PORT
Example: 0.0.0.0:8000
Environment Variables
| Variable | Type | Default | Deskripsi |
|---|---|---|---|
SERVER_HOST | string | 0.0.0.0 | Host binding address |
SERVER_PORT | string | 8000 | Port number |
Host Options
| Value | Deskripsi |
|---|---|
0.0.0.0 | Listen on all interfaces (recommended for container) |
127.0.0.1 | Listen only on localhost |
192.168.1.100 | Listen on specific IP |
Usage
func main() {
config.SetupConfig()
router := gin.Default()
// Setup routes...
// Get server address
serverAddr := config.ServerConfig()
// Start server
router.Run(serverAddr)
}
Output
Saat aplikasi start:
Server Running at :0.0.0.0:8000
Production Configuration
.env.production
SERVER_HOST=0.0.0.0
SERVER_PORT=8000
Docker
EXPOSE 8000
ENV SERVER_PORT=8000
Kubernetes
apiVersion: v1
kind: Service
metadata:
name: api-gateway
spec:
ports:
- port: 8000
targetPort: 8000
Extended Configuration
Untuk konfigurasi server yang lebih lengkap:
type ServerConfiguration struct {
Host string
Port string
ReadTimeout time.Duration
WriteTimeout time.Duration
MaxHeaderBytes int
Secret string
LimitCountPerRequest int64
}
func GetServerConfig() *http.Server {
return &http.Server{
Addr: config.ServerConfig(),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20, // 1 MB
}
}
Graceful Shutdown
func main() {
router := gin.Default()
srv := &http.Server{
Addr: config.ServerConfig(),
Handler: router,
}
// Start server in goroutine
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
// Give outstanding requests 5 seconds to complete
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown:", err)
}
log.Println("Server exiting")
}
Port Configuration
Common Ports
| Port | Usage |
|---|---|
| 80 | HTTP (requires root/admin) |
| 443 | HTTPS (requires root/admin) |
| 8000-8999 | Development servers |
| 3000 | Common for Node.js |
Port Conflicts
Jika port sudah digunakan:
# Check what's using the port (Linux/Mac)
lsof -i :8000
# Check what's using the port (Windows)
netstat -ano | findstr :8000
Change Port
SERVER_PORT=8001
Best Practices
- Use 0.0.0.0 in Containers - Untuk accessibility dari luar container
- Use Environment Variables - Jangan hardcode port
- Use High Ports in Development - Avoid needing root privileges
- Implement Graceful Shutdown - Untuk clean termination
- Set Timeouts - Prevent resource exhaustion