golang.org/x/playground@v0.0.0-20230418134305-14ebe15bcd59/version.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"go/build"
    10  	"net/http"
    11  	"runtime"
    12  )
    13  
    14  func (s *server) handleVersion(w http.ResponseWriter, req *http.Request) {
    15  	w.Header().Set("Access-Control-Allow-Origin", "*")
    16  
    17  	tag := build.Default.ReleaseTags[len(build.Default.ReleaseTags)-1]
    18  	var maj, min int
    19  	if _, err := fmt.Sscanf(tag, "go%d.%d", &maj, &min); err != nil {
    20  		code := http.StatusInternalServerError
    21  		http.Error(w, http.StatusText(code), code)
    22  		return
    23  	}
    24  
    25  	version := struct {
    26  		Version, Release, Name string
    27  	}{
    28  		Version: runtime.Version(),
    29  		Release: tag,
    30  	}
    31  
    32  	if s.gotip {
    33  		version.Name = "Go dev branch"
    34  	} else {
    35  		version.Name = fmt.Sprintf("Go %d.%d", maj, min)
    36  	}
    37  
    38  	s.writeJSONResponse(w, version, http.StatusOK)
    39  }