github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/infrastructure.go (about)

     1  // Copyright 2017-present Kirill Danshin and Gramework contributors
     2  // Copyright 2019-present Highload LTD (UK CN: 11893420)
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  
    11  package gramework
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/gramework/gramework/infrastructure"
    17  )
    18  
    19  // ServeInfrastructure serves Infrastructure info
    20  // It's an integration of our module
    21  //
    22  // Deprecated: will be moved to another package
    23  func (app *App) ServeInfrastructure(i *infrastructure.Infrastructure) {
    24  	app.GET("/infrastructure", func(ctx *Context) {
    25  		i.Lock.RLock()
    26  		i.CurrentTimestamp = time.Now().UnixNano()
    27  		ctx.CORS()
    28  		e := ctx.JSON(i)
    29  		_ = e
    30  		i.Lock.RUnlock()
    31  	})
    32  	app.POST("/infrastructure/register/service", func(ctx *Context) {
    33  		s := infrastructure.Service{
    34  			Addresses: make([]infrastructure.Address, 0),
    35  		}
    36  		_, err := ctx.UnJSONBytes(ctx.PostBody(), &s)
    37  		if err != nil {
    38  			if err := ctx.JSONError(err.Error()); err != nil {
    39  				// things really messed up.
    40  				// this panic is handled gracefully in the core
    41  				panic(err)
    42  			}
    43  			return
    44  		}
    45  		i.MergeService(s.Name, s)
    46  		e := ctx.JSON(s)
    47  		_ = e
    48  	})
    49  }