...

Source file src/gitlab.hexacode.org/go-libs/microservice/config/config_template.go

Documentation: gitlab.hexacode.org/go-libs/microservice/config

     1  /*
     2   * Micro Service
     3   * Package: gitlab.hexacode.org/go-libs/microservice
     4   * Maintainer: Azzis Arswendo <azzis@hexacode.org>
     5   *
     6   * Copyright (C) 2023 Hexacode Teknologi Indonesia
     7   * All Rights Reserved
     8   */
     9  
    10  package config
    11  
    12  import (
    13  	"net/http"
    14  	"strconv"
    15  
    16  	"github.com/gin-gonic/gin"
    17  	"gitlab.hexacode.org/go-libs/hctypes"
    18  	"gitlab.hexacode.org/go-libs/microservice/validate"
    19  )
    20  
    21  type ConfigTemplateContext struct {
    22  	Url            string              `json:"url,omitempty"`
    23  	Config         *Config             `json:"config,omitempty"`
    24  	ConfigTemplate *ConfigTemplate     `json:"config_template,omitempty"`
    25  	URLParams      hctypes.Dict        `json:"url_params,omitempty"`
    26  	Headers        hctypes.Dict        `json:"headers,omitempty"`
    27  	Request        *http.Request       `json:"-"`
    28  	Writer         http.ResponseWriter `json:"-"`
    29  }
    30  
    31  type ConfigTemplate struct {
    32  	Name      string                  `json:"name,omitempty"`
    33  	Path      string                  `json:"path,omitempty"`
    34  	FilePath  string                  `json:"file_path,omitempty"`
    35  	Callback  *ConfigTemplateCallback `json:"callback,omitempty"`
    36  	URLParams []*validate.Options     `json:"url_params,omitempty"`
    37  	config    *Config                 `json:"-"`
    38  }
    39  
    40  func (config_template *ConfigTemplate) HTTPHandler(ctx *gin.Context) {
    41  	url_params := hctypes.Dict{}
    42  	headers := hctypes.Dict{}
    43  
    44  	raw_headers := ctx.Request.Header.Clone()
    45  
    46  	for k, v := range raw_headers {
    47  		headers[k] = hctypes.String(v[0])
    48  	}
    49  
    50  	for _, param := range ctx.Params {
    51  		url_params[param.Key] = hctypes.String(param.Value)
    52  	}
    53  
    54  	ret := config_template.Callback.function(&ConfigTemplateContext{
    55  		Url:            ctx.Request.URL.Path,
    56  		Config:         config_template.config,
    57  		ConfigTemplate: config_template,
    58  		URLParams:      url_params,
    59  		Headers:        headers,
    60  		Request:        ctx.Request,
    61  		Writer:         ctx.Writer,
    62  	})
    63  
    64  	meta := "null"
    65  
    66  	if ret != nil {
    67  		meta = string(ret.ToJson())
    68  	}
    69  
    70  	template, err := NewTemplateFromFile(config_template.FilePath)
    71  
    72  	if err != nil {
    73  		ctx.String(http.StatusInternalServerError, "500 Internal Server Error")
    74  		return
    75  	}
    76  
    77  	render, err := template.Render(hctypes.Dict{
    78  		"meta": hctypes.String(meta),
    79  	})
    80  
    81  	if err != nil {
    82  		ctx.String(http.StatusInternalServerError, "500 Internal Server Error")
    83  		return
    84  	}
    85  
    86  	ctx.Writer.WriteHeader(http.StatusOK)
    87  	ctx.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
    88  	ctx.Writer.Header().Set("Content-Length", strconv.Itoa(len(render)))
    89  	ctx.Writer.WriteHeaderNow()
    90  	ctx.Writer.Write([]byte(render))
    91  }
    92  

View as plain text