Move code to go
This commit is contained in:
parent
d737e29c6f
commit
86756b7eb8
13 changed files with 37 additions and 100 deletions
20
Dockerfile
20
Dockerfile
|
|
@ -1,15 +1,15 @@
|
||||||
FROM python:3.9-rc-alpine
|
FROM golang:1.17-alpine AS build
|
||||||
|
|
||||||
|
WORKDIR /code
|
||||||
|
|
||||||
|
COPY time.go /code
|
||||||
|
RUN go build ./time.go
|
||||||
|
|
||||||
|
FROM alpine:latest AS final
|
||||||
|
|
||||||
MAINTAINER Yehuda Deutsch <yeh@uda.co.il>
|
MAINTAINER Yehuda Deutsch <yeh@uda.co.il>
|
||||||
|
|
||||||
RUN apk add tzdata \
|
|
||||||
&& apk add --virtual .build-deps gcc libc-dev make \
|
|
||||||
&& pip install fastapi uvicorn \
|
|
||||||
&& rm -Rf ~/.cache \
|
|
||||||
&& apk --purge del .build-deps gcc libc-dev make
|
|
||||||
|
|
||||||
WORKDIR /code
|
WORKDIR /code
|
||||||
ENV PYTHONPATH=/code
|
COPY --from=build /code/time /code/time
|
||||||
COPY time_app/ /code/time_app/
|
|
||||||
|
|
||||||
CMD ["python", "time_app"]
|
CMD ["/code/time"]
|
||||||
|
|
|
||||||
27
time.go
Normal file
27
time.go
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getTime(location string) time.Time {
|
||||||
|
loc, _ := time.LoadLocation("UTC")
|
||||||
|
return time.Now().In(loc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, getTime("UTC").Format("2006-01-02 15:04:05"))
|
||||||
|
})
|
||||||
|
|
||||||
|
http.HandleFunc("/diy", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
now := getTime("UTC")
|
||||||
|
fmt.Fprintf(w, "%s%d", string(now.Format("06")[1]), now.YearDay())
|
||||||
|
})
|
||||||
|
|
||||||
|
listen := ":8000"
|
||||||
|
fmt.Println("Listening on " + listen)
|
||||||
|
http.ListenAndServe(listen, nil)
|
||||||
|
}
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
from time_app.main import run
|
|
||||||
|
|
||||||
run()
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
from datetime import datetime
|
|
||||||
from zoneinfo import ZoneInfo, available_timezones
|
|
||||||
|
|
||||||
ALLOWED_TIMEZONES = available_timezones()
|
|
||||||
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
|
|
||||||
|
|
||||||
|
|
||||||
def tz_aware_now(zone: str) -> datetime:
|
|
||||||
"""
|
|
||||||
:param str zone: Zone name in DB
|
|
||||||
:return: A timezone aware datetime object
|
|
||||||
:raises: ValueError When zone name is not supported
|
|
||||||
"""
|
|
||||||
# ZoneInfo uses a weak cache, no need to implement a local cache
|
|
||||||
if zone not in ALLOWED_TIMEZONES:
|
|
||||||
raise ValueError(f'Unsupported timezone "{zone}"')
|
|
||||||
return datetime.now(tz=ZoneInfo(zone))
|
|
||||||
|
|
||||||
|
|
||||||
def real_utc_now() -> datetime:
|
|
||||||
return tz_aware_now('UTC')
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
from fastapi import HTTPException
|
|
||||||
|
|
||||||
from .datetime import tz_aware_now
|
|
||||||
|
|
||||||
|
|
||||||
def get_now_in_view(zone: str = 'UTC'):
|
|
||||||
try:
|
|
||||||
now = tz_aware_now(zone=zone)
|
|
||||||
except ValueError:
|
|
||||||
raise HTTPException(status_code=400, detail=f'Zone "{zone}" is unsupported')
|
|
||||||
return now
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
import uvicorn
|
|
||||||
from fastapi import FastAPI
|
|
||||||
from fastapi.responses import PlainTextResponse
|
|
||||||
|
|
||||||
from .views import main, api
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
app.include_router(main.router, tags=['text'], default_response_class=PlainTextResponse)
|
|
||||||
app.include_router(api.router, prefix='/api/v1', tags=['api'])
|
|
||||||
|
|
||||||
|
|
||||||
def run():
|
|
||||||
uvicorn.run("time_app.main:app", host="0.0.0.0", port=8000, log_level="info")
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
|
|
||||||
class Now(BaseModel):
|
|
||||||
time: str
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
from fastapi import APIRouter
|
|
||||||
|
|
||||||
from time_app.helpers.views import get_now_in_view
|
|
||||||
from time_app.models.main import Now
|
|
||||||
|
|
||||||
router = APIRouter()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get('/now', name='Now', response_model=Now)
|
|
||||||
def now_view(zone: str = 'UTC') -> dict:
|
|
||||||
return {"time": f'{get_now_in_view(zone):%Y-%m-%d %H:%M:%S}'}
|
|
||||||
|
|
||||||
|
|
||||||
@router.get('/diy', name='Day in year', response_model=Now)
|
|
||||||
def day_in_year_view(zone: str = 'UTC') -> dict:
|
|
||||||
return {"time": f'{get_now_in_view(zone):%y%j}'[-4:]}
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
from fastapi import APIRouter
|
|
||||||
|
|
||||||
from time_app.helpers.views import get_now_in_view
|
|
||||||
|
|
||||||
router = APIRouter()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get('/', name='Now')
|
|
||||||
def now_view() -> str:
|
|
||||||
return f'{get_now_in_view():%Y-%m-%d %H:%M:%S}'
|
|
||||||
|
|
||||||
|
|
||||||
@router.get('/now', name='Now')
|
|
||||||
def now_view(zone: str = 'UTC') -> str:
|
|
||||||
return f'{get_now_in_view(zone):%Y-%m-%d %H:%M:%S}'
|
|
||||||
|
|
||||||
|
|
||||||
@router.get('/diy', name='Day in year')
|
|
||||||
def day_in_year_view(zone: str = 'UTC') -> str:
|
|
||||||
return f'{get_now_in_view(zone):%y%j}'[-4:]
|
|
||||||
Loading…
Add table
Reference in a new issue