Sobes.tech
Junior — Middle

What server architecture is used in your projects?

sobes.tech AI

Answer from AI

In my Go projects, I usually apply microservices architecture or modular monolithic architecture, depending on the scale and requirements.

Main principles:

  • Clear separation of responsibilities between components.
  • Use of REST or gRPC for communication between services.
  • Dependency injection for ease of testing and extensibility.
  • Use of context (context.Context) to manage request lifetimes and cancellations.

Example of a simple server architecture in Go:

package main

import (
    "context"
    "net/http"
)

// Service with business logic
type UserService struct {}

func (s *UserService) GetUser(ctx context.Context, id string) (string, error) {
    // logic to get user
    return "User " + id, nil
}

// HTTP handler
func userHandler(s *UserService) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        id := r.URL.Query().Get("id")
        user, err := s.GetUser(r.Context(), id)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        w.Write([]byte(user))
    }
}

func main() {
    userService := &UserService{}
    http.HandleFunc("/user", userHandler(userService))
    http.ListenAndServe(":8080", nil)
}

This approach allows easy scaling and maintenance of the server side.

What server architecture is used in your projects… - sobes.tech