Proving Tietze's theorem on metric spaces Let $(X,d)$ be a metric space and $A$ be a non-empty closed subset of $X$. If $f\colon A\to \mathbb{R}$ is an application continuous and bounded, then there exists a continuous map $g\colon X\to \mathbb{R}$ such that $g(x)=f(x)$ for all $x\in A$ and
$$\inf_{x\in X}g(x)=\inf_{y\in A}f(y) \quad \text{ y } \quad \sup_{x\in X}g(x)=\sup_{ y\in A}f(y).$$
My attempt
We can decompose $X = A^{\circ} \cup (X\smallsetminus A) \cup \partial A$. Let $m=\inf_{y\in A}f(y)$ and $M = \sup_{y\in A}f(y)$. I first consider the case when $m=1$ and $M=2$. Let us define $h\colon X\smallsetminus A \to \mathbb{R}$ by $h(x)=\inf\{f(y)d(x,y)\colon y\in
A\}$ and $g\colon X\to \mathbb{R}$ by
\begin{equation}\label{def1}
g(x)=
\left\{
\begin{array}{ccl}
\dfrac{h(x)}{d(x,A)} & \text{si} & x\in X\smallsetminus A,\\
f(x) & \text{if} & x \in A.
\end{array}
\right.
\end{equation}
It is easy to prove that $1 \leq g(x) \leq 2$ for all $x \in X\smallsetminus A$ and $g$ extends $f$ to all $X$. It remains for me to prove that $g$ is continuous on $X$; however, I have proven that $g$ is continuous on $A^{\circ}$ and on $X\smallsetminus A$. It only remains for me to show that it is continuous on $\partial A$. To do this, suppose $x_0\in \partial A=A\cap \overline{X\smallsetminus A}$ and let
$\varepsilon \in (0,1)$; since $f$ is continuous on $A$ and $x_0\in A$, there exists $\eta>0$ such that
\begin{equation}
 y\in A\cap B(x_0, \eta) \quad \text{implies} \quad |f(y)-f(x_0)|\le \varepsilon. \quad (*)
\end{equation}
Let $\delta\colon=\eta/3$ and $x\in B(x_0, \delta)$. There are two cases.
Case 1: If $x\in A$, it follows from $(*)$ that
$|g(x)-g(x_0)|=|f(x)-f(x_0)|\le \varepsilon$.
Case 2: If  $x\notin A$. Choose $a\in A$ such that
$d\left(x,a\right)\leq 2d\left(x,A\right)$,
and note that
$$
d\left(a,x_{0}\right)\leq d\left(a,x\right)+d\left(x,x_{0}\right)\leq2 d\left(x,A\right)+d\left(x,x_{0}\right)\leq3\cdot d\left(x,x_{0}\right)\leq\eta.
$$
Therefore, $\left|f\left(a\right)-f\left(x_{0}\right)\right|\leq\frac{\varepsilon}{2}$,
and thus
$$
g\left(x\right)\leq \dfrac{f(a)d(x,a)}{d(x,A)} \leq \dfrac{(\frac{\varepsilon}{2}+f(x_0))d(x,a)}{d(x,A)} \leq \dfrac{(\frac{\varepsilon}{2}+f(x_0))2d(x,A)}{d(x,A)} = 2f(x_0) + \varepsilon.
$$
Precisely here is my mistake, because I need to prove that $ g(x) \leq f(x_0) + \varepsilon$ but I got $ g(x) \leq 2f(x_0) + \varepsilon$. So I want to know where I am failing. I think if I can show this, the inequality $f(x_0) -\varepsilon \leq g(x) $ would be analogous. Finally, for the general case $m<M$, I suspect that I should define the function $F(x) = \alpha f(x) + \beta$, but I'm not sure. I need help with this exercise, please. Any help is appreciated.

Bad idea to choose $a$ such that $d(x,A) \leq 2 d(x,A)$. Choose $a\in A$ such that $d(x,a) <d(x,A)+\epsilon$ and you will be able to complete the proof easily.
