func (s *ApiServer) GetArtists(res http.ResponseWriter, req *http.Request) {
    var err error
    var sr  = NewSubsonicResponse()
    sr.Artists, err = s.db.GetIndexedArtists()
    if err != nil {
        s.logger.Print("failed to build indexed artist list:", err)
        s.writeSubsonicResponse(res, req, NewSubsonicError(0, "internal server error"))
    } else {
        s.writeSubsonicResponse(res, req, sr)
    }
    return
}
handles getArtists.view requests
package main
import (
    "log"
    "fmt"
    "net/http"
    "time"
    "io"
    "os"
    "encoding/hex"
)
type ApiServer struct {
    logger      *log.Logger
    db          *SubsonicDB
    server      *http.Server
    port        int
    certFile    string
    keyFile     string
}
func NewServer(logger *log.Logger, db *SubsonicDB, port int, certFile string, keyFile string) (s *ApiServer) {
    var mux *http.ServeMux
    if logger == nil || db == nil {
        panic("nil logger or db")
    }
    s = &ApiServer{}
    s.logger    = logger
    s.db        = db
    s.port      = port
    s.certFile  = certFile
    s.keyFile   = keyFile
    mux         = http.NewServeMux()
    s.server    = &http.Server{
        Addr:           fmt.Sprintf(":%v", s.port),
        Handler:        mux,
        ErrorLog:       logger,
        ReadTimeout:    10 * time.Second,
        WriteTimeout:   300 * time.Second,
    }
    mux.HandleFunc("/rest/", s.apiReqHandler)
    return
}
func (s *ApiServer) Listen() (err error) {
    if s.certFile != "" && s.keyFile != "" {
        err = s.server.ListenAndServeTLS(s.certFile, s.keyFile)
    } else {
        err = s.server.ListenAndServe()
    }
    return
}
func (s *ApiServer) apiReqHandler(res http.ResponseWriter, req *http.Request) {
    var username    string
    var err         error
    if req.Method != "GET" {
        http.Error(res, "method not allowed", http.StatusMethodNotAllowed)
        return
    }
	err = req.ParseForm(); if err != nil {
        http.Error(res, "internal server error", http.StatusInternalServerError)
        s.logger.Print("failed to parse request:", err)
        return
    }
    if  len(req.Form["u"]) != 1 || len(req.Form["p"]) != 1 {
        s.writeSubsonicResponse(res, req, NewSubsonicError(10, "credentials missing"))
        return
    } else {
        var password    string
        var ok          bool
        username    = req.Form["u"][0]
        password    = req.Form["p"][0]
        if len(password) > 4 && password[0:4] == "enc:" {
            var decodedPassword []byte
            decodedPassword, err = hex.DecodeString(password[4:]); if err != nil {
                s.writeSubsonicResponse(res, req, NewSubsonicError(40, "wrong username or password"))
                s.logger.Print("failed to check auth:", err)
                return
            }
            password    = string(decodedPassword)
        }
        ok, err = s.db.CheckPassword(username, password); if err != nil {
            s.writeSubsonicResponse(res, req, NewSubsonicError(40, "wrong username or password"))
            s.logger.Print("failed to check auth:", err)
            return
        }
        if !ok {
            s.writeSubsonicResponse(res, req, NewSubsonicError(40, "wrong username or password"))
            s.logger.Printf("auth failure for user '%s'", username)
            return
        }
    }
    switch req.URL.Path {
        case "/rest/ping.view":
            s.Ping(res, req)
        case "/rest/getArtists.view":
            s.GetArtists(res, req)
        case "/rest/getArtist.view":
            s.GetArtist(res, req)
        case "/rest/getAlbum.view":
            s.GetAlbum(res, req)
        case "/rest/getSong.view":
            s.GetSong(res, req)
        case "/rest/getCoverArt.view":
            s.GetCoverArt(res, req)
        case "/rest/getIndexes.view":
            s.GetIndexes(res, req)
        case "/rest/stream.view", "/rest/download.view":
            s.Stream(res, req)
        default:
            http.NotFound(res, req)
    }
    s.logger.Printf("%s %s %s %s", req.RemoteAddr, username, req.Method, req.URL.Path)
    return
}
func (s *ApiServer) writeSubsonicResponse(res http.ResponseWriter, req *http.Request, sr *SubsonicResponse) (err error) {
    var payload     string
    if len(req.Form["f"]) == 1 && req.Form["f"][0] == "json" {
        res.Header().Set("Content-Type", "text/json")
        payload, err = sr.MarshalJson();
    } else {
        res.Header().Set("Content-Type", "text/xml")
        payload, err = sr.MarshalXml();
    }
    if err != nil {
        s.logger.Print("failed to serialize subsonic response:", err)
        http.Error(res, "internal server error", http.StatusInternalServerError)
    } else {
        _, err = io.WriteString(res, payload); if err != nil {
            s.logger.Print("failed to write http response body:", err)
        }
    }
    return
}
func (s *ApiServer) Ping(res http.ResponseWriter, req *http.Request) {
    var sr  = NewSubsonicResponse()
    s.writeSubsonicResponse(res, req, sr)
    return
}